Search code examples
c#asp.net.netsoap-clientservice-reference

.NET Service Reference - Soap Client state always Faulted


I have a .NET Web Form Application which uses soap web service for some operations. In my solution, there are few projects and one of them is responsible for soap operations. Thus I added Service Reference to that project. My old publish works well but now, I can't call any method in the web service because throws error.

The error message:

The communication object, System.ServiceModel.ChannelFactory`1[ProjectName.IntegrationServiceSoap], cannot be used for communication because it is in the Faulted state.

Whenever I create an instance of soap client, it's state property is always Faulted.

I tested it with test endpoints, removed and re-added the service reference but it is not working.

Stack trace:

   at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposed()
   at System.ServiceModel.ChannelFactory.EnsureOpened()
   at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannelInternal()
   at System.ServiceModel.ClientBase`1.get_Channel()
   at SoapLibrary.IntegrationServiceSoapClient.ProjectName.IntegrationServiceSoap.GetTicket(TicketRequest request) in C:\Users\...

Here is my config files

Main Project Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <compilation targetFramework="4.5.1" debug="true" />
        <!--Including other configs-->
    </system.webServer>
    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="IntegrationServiceSoap" maxReceivedMessageSize="2147483647" sendTimeout="23:59:59" receiveTimeout="23:59:59" maxBufferPoolSize="2147483647" maxBufferSize="2147483647">
              <security mode="Transport" />
            </binding>
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="https://mywebservice.com/IntegrationService.asmx"
              binding="basicHttpBinding" bindingConfiguration="IntegrationServiceSoap"
              contract="MyService.IntegrationServiceSoap" name="IntegrationServiceSoap" />
        </client>
    </system.serviceModel>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Second Project's (including soap service reference) App.config

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="IntegrationServiceSoap" receiveTimeout="23:59:59"
                    sendTimeout="23:59:59" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"/>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://mywebservice.com/IntegrationService.asmx"
                binding="basicHttpBinding" bindingConfiguration="IntegrationServiceSoap"
                contract="MyService.IntegrationServiceSoap" name="IntegrationServiceSoap" />
        </client>
    </system.serviceModel>
</configuration>

Any idea? How can I fix this problem?


Solution

  • I resolved the problem by downgrading project's .NET Framework Version from 4.5.1 to 4.5

    I also added fault listener to my SoapClient.

        private void CreateSoapClient()
        {
            client = new IntegrationServiceSoapClient("IntegrationServiceSoap");
            client.ChannelFactory.Faulted += ChannelFactory_Faulted;
        }
    
        private void ChannelFactory_Faulted(object sender, EventArgs e)
        {
            client.Abort();
            client.ChannelFactory.Faulted -= ChannelFactory_Faulted;
            client = null;
            CreateSoapClient();
        }
    

    I don't know what is actually problem. Now, my client is working without any problem.