简体   繁体   中英

Create assembly with datacontracts of WCF service

Here is my issue: in a project I have to consume 3 third-party wcf services. 2 of those contain the same objects and largely the same methods.

The user's role within the application determines which service to use. For example: Let's say the 2 services are ServiceRoleA en ServiceRoleB. Both services contain the method GetInfo() end return the InfoDetails object. The InfoDetails object has the exact same signature for both services.

If I just add 2 service references to my project I'll get the objects ServiceRolaA.InfoDetails and ServiceRoleB.InfoDetails. Instead, I need just one object InfoDetails. I only want to write just one routine to handle the InfoDetails etc.

My initial thought was to create an assembly with the datacontracts of the services and reference the assembly in my project. This way the service references can use the common set of objects. For this to work I have create the datacontract classes using svcutil and the wsdl, but I get error upon error.

When I try the following:

svcutil *.wsdl /dataContractOnly /n:*,DataContracts /language:C# /out:XxxData.cs

I get the following error:

"Error: Type 'AuthenticationBase' in namespace ' http://schemas.datacontract.org/2004/07/xxx ' cannot be imported. It references 'KindOfModule' from namespace ' http://schemas.datacontract.org/2004/07/yyy ' but schema does not contain appropriate statement. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer.

If you are using the /dataContractOnly option to import data contract types andare getting this error message, consider using xsd.exe instead. Types generatedby xsd.exe may be used in the Windows Communication Foundation after applying the XmlSerializerFormatAttribute attribute on your service contract. Alternatively, consider using the /importXmlTypes option to import these types as XML types to use with DataContractFormatAttribute attribute on your service contract."

Exporting the datacontracts as XML types is no option for my so the next thing to try was:

svcutil *.wsdl /dataContractOnly /n:*,DataContracts /serializer:XmlSerializer /language:C# /out:XxxData.cs

Which resulted in the exact same error. So I decided to try the other mentioned option to use XSD.exe. But that is also nog working since I only have a WSDL and XSD.exe requires a XSD file. Are there any more options I can try? Please help!

You don't need to add service references or using svcutil to create the client proxies, that is easily done by hand.

  1. Create a contracts assembly that contains all service interfaces and data contracts.
  2. Reference that assembly from both the server(s) and the client (be sure to update the web.config and .svc files to reflect these changes)
  3. Create client proxy classes for your services.

A client proxy is as simple as adding a class like

public class ServiceRoleAClient : ClientBase<IServiceRoleA>, IServiceRoleA
{
   public InfoDetails GetInfo(GetInfoRequest request)
   {
      return Channel.GetInfo(request);
   }
}

The only drawback is that you'll have to maintain the system.serviceModel node in the app.config file yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM