WCF异常 - 故障状态

时间:2014-07-30 15:10:25

标签: c# wcf channelfactory

我在项目中添加了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调用方法,然后一切都很好。为什么?

1 个答案:

答案 0 :(得分:1)

您的绑定元素安全性为None。在以编程方式定义时,您还需要提及它。

WSHttpBinding bindingDialingType = new WSHttpBinding
   { Security = new WSHttpSecurity { Mode = SecurityMode.None } };