我有一个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
包装项目中的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>
解决方案是将引用项目中的绑定和客户端信息复制并粘贴到包装器项目中。
尽管很少需要在一个用户配置文件中分别跟踪应用程序不同副本的设置,但您不太希望让DLL的所有不同用法彼此共享配置。因此,当您使用“常规”方法检索配置对象时,返回的对象将与您在其中执行的App Domain的配置绑定,而不是与特定程序集绑定。
或者如Sam Holder从相同解决方案C#中的另一个项目中放入Reference Web.Config文件:
类库没有自己的配置。他们使用正在使用的可执行文件的配置。
在C#中,标准做法是配置应属于应用程序,而不是引用的DLL。因此,将app.config信息复制并粘贴到调用项目是有意义的。这并不要求您直接从调用项目中引用旧的Web服务,因为我担心这样做。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句