Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

c# - How do you supply a type that's defined in a web service to another web service (share types)?

I've got 2 WCF services. Service A contains the definition of the type MyEntity. Service B contains a service reference to Service A and therefore can use the type for MyEntity. So I have a method that looks like this:

protected void Update (ServiceA.MyEntity entity)
{
    //Do stuff
}

Now I want to use this method in Service A, so I added a service reference for Service B and tried:

protected UpdateServiceB(MyEntity entity)
{
    using(ServiceB.ServiceClient client =  new ServiceB.ServiceClient())
    {
        client.Update(entity);
    }
}

This didn't work and complained that the types were not the same, even though Service B is using the type defined in Service A. How can I solve this?

UPDATE

I avoided the issue due to time constraints and passed the Guid of MyEntity from Service A to Service B instead. I then used an existing method called 'GetMyEntity(Guid entityId)' in Service A to retrieve the entity in Service B:

protected void Update (Guid entityId)
{
    MyEntity entity = new MyEntity();        

    using (ServiceAClient client = new ServiceAClient())
    {
        entity = client.GetMyEntity(entityId);
    }

    //Do stuff
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sounds like you are using service references by way of Visual Studio's Add Service Reference command. While adequate, it can arguably become troublesome in medium to large projects due to:

  1. Service types are re-defined in the client rather than using a common library (as you have discovered).

  2. When a service contract changes, this will generally cause the service to be updated but not so in the client because of point 1. Client proxies become stale overtime as the schema evolves. You have to refresh reference

My best suggestion is not to use Add Service Reference and roll your client proxies by hand.

Once performed you will have additional libraries:

  1. A.Contracts.dll - here you define all the service interfaces and data models for service A
  2. B.Contracts.dll - here you define all the service interfaces and data models for service B
  3. Common.Contracts.dll - If A & B have common types, place them here. (canonical data models)
  4. A.Service.dll - service A implementation
  5. B.Serivce.dll - service B implementation
  6. ClientProxies.dll - roll-your-own client proxies for all your services

You don't have to implement all of the above now. All that is required is the manual ClientProxies.dll and you can always iterate to the rest later as required.

More


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...