Harsh_Joshi
Harsh_Joshi

Reputation: 47

WCF Error: The operation cannot be completed because the pipe was closed

I am getting an error when the client reference is used a second time for calling the underlying method. On first call, It works perfectly. I have googled it and did so many tries like setting timeouts but nothing worked. Any suggestions will be highly appreciated. Also, if more details are required please let me know, I will post the required code. A MDI Child form will call this method.

Debugger shows CommunicationException. Trace Viewer shows message: The operation cannot be completed because the pipe was closed. This may have been caused by the application on the other end of the pipe exiting.

Contract

[ServiceContract(Namespace = "http://Company/ManagementInformationSystemServices/", SessionMode = SessionMode.Required)]
public interface IPrincipalService
{
    #region Service contracts for Reports

    [OperationContract]
    [FaultContract(typeof(ProcessExecutionFault))]
    Parcel InsertParcel(Parcel singleParcel, out bool Exists);

    [OperationContract]
    [FaultContract(typeof(ProcessExecutionFault))]
    Parcel GetByParcelId(int id);
    #endregion
}

Contract Implementation

[ServiceBehavior(UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public class PrincipalService : IPrincipalService
{        
    #region Constructor
    public PrincipalService()
    {

    }
    #endregion

    #region Public Methods

    #region Parcel Methods
    public Parcel InsertParcel(Parcel singleParcel, out bool Exists)
    {
        bool ExistsInner = false;
        try
        {
            ParcelComponent parcelComponent = new ParcelComponent();
            singleParcel = parcelComponent.Insert(singleParcel, out ExistsInner);
            Exists = ExistsInner;
            return singleParcel;
        }
        catch (Exception ex)
        {
            throw new FaultException<ProcessExecutionFault>
                (new ProcessExecutionFault(LogResource.InsertParcelExistsError, ex), ex.Message);
        }
    }

    public Parcel GetByParcelId(int id)
    {
        try
        {
            ParcelComponent parcelComponent = new ParcelComponent();
            return parcelComponent.GetById(id);
        }
        catch (Exception ex)
        {
            throw new FaultException<ProcessExecutionFault>
                (new ProcessExecutionFault(LogResource.ReadParcelError, ex), ex.Message);
        }
    }
    #endregion
    #endregion
}

Server Configuration

<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

<services>
  <service behaviorConfiguration="serviceBehavior" name="ManagementInformationSystem.Services.PrincipalService">

    <host>
      <baseAddresses>
        <add baseAddress="net.pipe://localhost/ManagementInformationSystemServices" />
      </baseAddresses>
    </host>

    <endpoint address="PrincipalService"
              binding="netNamedPipeBinding"
              contract="ManagementInformationSystem.Services.Contracts.IPrincipalService" />

    <endpoint address="PrincipalService/mex"
              binding="mexNamedPipeBinding"
              contract="IMetadataExchange" />

  </service>
</services>    
<behaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior">
      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<diagnostics wmiProviderEnabled="true">
</diagnostics>

Client Configuration

  <system.serviceModel>
<bindings>
  <netNamedPipeBinding>
    <binding name="NetNamedPipeBinding_IPrincipalService" />
  </netNamedPipeBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="endpointBehavior">
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<client>
  <endpoint address="net.pipe://localhost/ManagementInformationSystemServices/PrincipalService"
    binding="netNamedPipeBinding" bindingConfiguration="NetNamedPipeBinding_IPrincipalService"
    contract="PrincipalServiceReference.IPrincipalService" name="NetNamedPipeBinding_IPrincipalService">
    <identity>
      <userPrincipalName value="company-238\company" />
    </identity>
  </endpoint>
</client>
<diagnostics wmiProviderEnabled="true">
</diagnostics>

Service Helper Class and I am getting Communication Exception here

public static class ServiceHelper<T>
{
    public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>(GlobalResource.PrincipalServiceEndPointName);

    public static void Use(UseServiceDelegate<T> codeBlock)
    {
        IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
        bool success = false;
        try
        {
            codeBlock((T)proxy);
            proxy.Close();
            success = true;
        }
        catch (CommunicationException ex)
        {
            throw ex;
        }
        catch (TimeoutException ex)
        {
            throw ex;
        }
        finally
        {
            if (!success)
            {
                proxy.Abort();
            }
        }
    }
}

Upvotes: 0

Views: 1048

Answers (0)

Related Questions