简体   繁体   English

使用WCF服务的数据合同创建程序集

[英]Create assembly with datacontracts of WCF service

Here is my issue: in a project I have to consume 3 third-party wcf services. 这是我的问题:在一个项目中,我必须使用3个第三方wcf服务。 2 of those contain the same objects and largely the same methods. 其中的2个包含相同的对象,并且基本上具有相同的方法。

The user's role within the application determines which service to use. 应用程序中的用户角色确定要使用的服务。 For example: Let's say the 2 services are ServiceRoleA en ServiceRoleB. 例如:假设这两个服务是ServiceRoleA和ServiceRoleB。 Both services contain the method GetInfo() end return the InfoDetails object. 这两个服务都包含方法GetInfo(),最后返回InfoDetails对象。 The InfoDetails object has the exact same signature for both services. 这两个服务的InfoDetails对象具有完全相同的签名。

If I just add 2 service references to my project I'll get the objects ServiceRolaA.InfoDetails and ServiceRoleB.InfoDetails. 如果仅向项目添加2个服务引用,则将获得对象ServiceRolaA.InfoDetails和ServiceRoleB.InfoDetails。 Instead, I need just one object InfoDetails. 相反,我只需要一个对象InfoDetails。 I only want to write just one routine to handle the InfoDetails etc. 我只想编写一个例程来处理InfoDetails等。

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. 为此,我已经使用svcutil和wsdl创建了datacontract类,但是遇到错误时会出错。

When I try the following: 当我尝试以下操作时:

svcutil *.wsdl /dataContractOnly /n:*,DataContracts /language:C# /out:XxxData.cs svcutil * .wsdl / dataContractOnly / n:*,DataContracts /语言: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. “错误:无法导入名称空间' http://schemas.datacontract.org/2004/07/xxx '中的类型'AuthenticationBase'。它引用了名称空间' http://schemas.datacontract.org/2004/中的 'KindOfModule' 07 / yyy ',但是模式不包含适当的语句,请更改模式,以便这些类型可以映射到数据协定类型,或者使用ImportXmlType或使用其他序列化器。

If you are using the /dataContractOnly option to import data contract types andare getting this error message, consider using xsd.exe instead. 如果您使用/ dataContractOnly选项导入数据合同类型并收到此错误消息,请考虑改用xsd.exe。 Types generatedby xsd.exe may be used in the Windows Communication Foundation after applying the XmlSerializerFormatAttribute attribute on your service contract. 在服务合同上应用XmlSerializerFormatAttribute属性后,可以在Windows Communication Foundation中使用xsd.exe生成的类型。 Alternatively, consider using the /importXmlTypes option to import these types as XML types to use with DataContractFormatAttribute attribute on your service contract." 或者,考虑使用/ importXmlTypes选项将这些类型导入为XML类型,以与服务合同上的DataContractFormatAttribute属性一起使用。”

Exporting the datacontracts as XML types is no option for my so the next thing to try was: 对于我来说,将数据合同导出为XML类型不是我的选择,所以接下来要尝试的是:

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

Which resulted in the exact same error. 导致完全相同的错误。 So I decided to try the other mentioned option to use XSD.exe. 因此,我决定尝试其他提到的选项来使用XSD.exe。 But that is also nog working since I only have a WSDL and XSD.exe requires a XSD file. 但这也无法正常工作,因为我只有WSDL,并且XSD.exe需要XSD文件。 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. 您无需添加服务引用或使用svcutil来创建客户端代理,这很容易手动完成。

  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) 从服务器和客户端都引用该程序集(请确保更新web.config和.svc文件以反映这些更改)
  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. 唯一的缺点是,您必须自己维护app.config文件中的system.serviceModel节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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