我将WCF服务引用添加到我的项目中。下一步是从代码中创建一个通道。
WSHttpBinding bindingDialingType = new WSHttpBinding();
EndpointAddress endingPointDialingType = new EndpointAddress("http://wsvc.corporate.my.com/International/DialingService.svc");
ChannelFactory<IDialingService> iDialingServiceChannelFactory = new ChannelFactory<IDialingService>(bindingDialingType, endingPointDialingType);
IDialingService instanceIDialingService = iDialingServiceChannelFactory.CreateChannel();
因为我遇到了一个异常,所以我猜我的通道工厂代码有问题。
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
为了捕获异常,我编写了以下代码:
My app.config is:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_iLuCRE" />
</basicHttpBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_IDialingService">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://wsvc.corporate.my.com/LuCRE/LuCRE.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_iLuCRE"
contract="LuCRE.iLuCRE" name="BasicHttpBinding_iLuCRE" />
<endpoint address="http://wsvc.corporate.my.com/International/DialingService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDialingService"
contract="DialingService.IDialingService" name="WSHttpBinding_IDialingService" />
</client>
</system.serviceModel>
我使用WCFTestClient来测试它,但是该服务确实工作。我将服务引用添加到我的项目中,但我不知道代码细节。
更新:
如果我使用代码 DialingServiceClient client = new DialingServiceClient();
然后通过client
调用该方法,则一切正常。为什么?
您的绑定元素安全性是None
。在以编程方式定义时也需要提到它。
WSHttpBinding bindingDialingType = new WSHttpBinding
{ Security = new WSHttpSecurity { Mode = SecurityMode.None } };