visual-studio – WCF在WSDL中共享类型

我必须使用来自同一提供商的几个单独的Web服务.基本上每个函数都有自己的服务(wsdl).对于interop,每个wsdl都有对共享类型的引用(例如:xs:import namespace =“http://generic.type.com”/>).

在VS中添加Service Referances会将服务命名空间添加到这些类型的前缀.
添加两个服务将生成两个独立但相同的类:

var context = new Service1.GenericContext();

var contex2 = new Service2.GenericContext();

我如何将这些一起映射/投射?我有20个这样的服务.

尝试在Reference.svcmap中使用NamespaceMappings,但是faild.我不知道要使用什么TargetNamespace和ClrNamespace.

TY!

最佳答案 您应该使用svcutil.exe为端点生成一个服务代理文件,而不是添加服务引用.

然后,所有服务代理类一起位于您使用命令行开关/ n指定的相同命名空间中.

然后svcutil.exe调用有许多参数.因此,我建议您将其存储在批处理文件中,或者更加舒适:将Visual Studio中的Build Events下的命令调用放入“Pre-build event command line”.

这是我的客户端的svcutil调用,它将所有代理类放在ServiceProxy.cs中.您很可能必须修改svcutil.exe的路径,当然还有服务URL:

"%PROGRAMFILES%\Microsoft SDKs\Windows\v7.0A\bin\svcutil.exe" /noLogo /noConfig /out:"$(ProjectDir)ServiceProxy.cs" /t:code /i /l:cs /tcv:Version35 /ser:DataContractSerializer /ct:System.Collections.Generic.List`1 /n:*,Oe.Corporate.CRMFacade.Service.Test http://localhost:3615/Client010/MasterDataService.svc http://localhost:3615/Client010/BusinessPartnerService.svc http://localhost:3615/Client010/MarketingAttrService.svc http://localhost:3615/Client010/ProductTransactionService.svc http://localhost:3615/Client010/ProductDataService.svc http://localhost:3615/Client010/ActivityManagementService.svc http://localhost:3615/Client010/PromotionService.svc

更新:
我忘了提到pre-build事件将失败,除非你将它添加到结束Project元素正上方的.csproj文件的底部:

<Target Name="PreBuildEvent" Condition="'$(PreBuildEvent)'!=''" DependsOnTargets="$(PreBuildEventDependsOn)">
    <Exec WorkingDirectory="$(OutDir)" Command="$(PreBuildEvent)" ContinueOnError="true" />
</Target>
点赞