3
votes

I added the WCF service reference to my project. Next step is to create a channel from the code.

 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();

Because I met an exception, so I guess that something wrong with my channel factory code.

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

To capture the exception, I had the code.

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>

I used WCFTestClient to test it, however the service does work. I added the service reference to my project and I don't know the code details.

Updated:

If I used the code

 DialingServiceClient client = new DialingServiceClient();

Then call the method through client, then everything is fine. Why?

1
Is DialingServiceClient a generated class from service reference?Yuliam Chandra
Yes, it is. After I added the service reference. It is inside the Reference.cs.user1108948
Then it's highly possible that there is missing element / aspect when consuming the service by creating the binding programmatically, try using svcutil to generate the config then compare what is missing svcutil http://wsvc.corporate.my.com/International/DialingService.svc /config:c:\test.configYuliam Chandra
They are exactly same.user1108948

1 Answers

2
votes

Your binding element security is None. You need to also mention it when defining programmatically.

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