如何在不更改后者绑定的情况下从WCF服务引用WCF客户端?

时间:2014-07-02 22:40:15

标签: c# web-services wcf wcf-binding

我有一个C#WCF服务,我需要引用另一个Web服务。旧服务是用Visual Basic编写的,而不是WCF。

前者应该弃用后者,但旧服务中有一些方法很复杂。因此,我想从新服务中调用旧服务中的方法。

为了避免命名混淆,我想在一个单独的项目中封装旧服务的包装器,该项目是从新服务引用的。但是,当我尝试从新服务项目调用包装器时,新服务抛出了InvalidSoapOperation。当我从包装器项目本身调用旧服务时,不会抛出此异常。

通常,我会将旧服务的绑定从包装项目复制并粘贴到新项目中,但由于合同的相似性,我很愿意这样做。

有没有办法通过包装器项目“间接”引用旧服务,以便新服务项目不直接与旧服务耦合?

编辑:堆栈跟踪如下:

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=Could not find default endpoint element that references contract 'OldService.OldServiceSoap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
  Source=System.ServiceModel
  StackTrace:
       at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)
       at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName, Configuration configuration)
       at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)
       at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
       at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
       at System.ServiceModel.ConfigurationEndpointTrait`1.CreateSimplexFactory()
       at System.ServiceModel.ConfigurationEndpointTrait`1.CreateChannelFactory()
       at System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait)
       at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef()
       at System.ServiceModel.ClientBase`1..ctor()
       at OldServiceWrapper.OldService.OldServiceClient..ctor() in C:...\MyProject\Service References\TheOldService\Reference.cs:line 21614

Wrapper项目中的App.Config:

<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="OldServiceSoap" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost/OldService/OldService.asmx"
                binding="basicHttpBinding" bindingConfiguration="LogOrderSoap"
                contract="OldService.OldServiceSoap" name="OldServiceSoap"/>
        </client>
</system.serviceModel>

2 个答案:

答案 0 :(得分:1)

您构建新服务的方式是旧服务的客户端。因此,您必须将包装器的app.config集成到新服务的web.config中。

答案 1 :(得分:1)

解决方案是将引用项目中的绑定和客户端信息复制并粘贴到包装器项目中。

C# DLL config file中所述:

  

虽然很少需要在一个用户配置文件中单独跟踪应用程序的不同副本的设置,但您不太可能希望DLL的所有不同用法彼此共享配置。因此,当您使用&#34; normal&#34;检索配置对象时方法,您获取的对象与您正在执行的App Domain的配置相关联,而不是特定的程序集。

或者Sam Holder加入Reference Web.Config file from another project in same solution C#

  

类库没有自己的配置。它们使用的是可用于执行它们的配置。

在C#中,标准做法是配置应该属于应用程序,而不是引用的DLL。因此,将app.config信息复制并粘贴到调用项目是有意义的。这不需要您直接从调用项目引用旧的Web服务,因为我担心它会。