0

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();
            }
        }
    }
}
Harsh_Joshi
  • 47
  • 1
  • 8
  • Don't you get any error or warning? `out` parameters are not supported by WCF. – nvoigt Jun 22 '16 at 12:17
  • I am getting communication Exception : There was an error reading from the pipe: Unrecognized error 109 (0x6d). – Harsh_Joshi Jun 23 '16 at 09:03
  • I have edited my question to include service helper class – Harsh_Joshi Jun 23 '16 at 09:09
  • You should turn on tracing, a description can be found [here](http://stackoverflow.com/questions/15836199/wcf-namedpipe-communicationexception-the-pipe-has-been-ended-109-0x6d). – nvoigt Jun 23 '16 at 10:18
  • @Emmanuel Thanks for answering Durin. What should I check on the server side.? How you want me to check? If there is an exception it should not work even for the first time. So why would it generate com. exception second time. When my form loads I am calling a method to retrieve data using service. Everything goes fine. But a second time exception occurs. So I stopped the service and started again without closing my application. Then it worked properly. At some point, it loses connectivity or what? I don't know. – Harsh_Joshi Jun 23 '16 at 12:53
  • @nvoigt I caught the issue but I am not able to solve it. I need more help from you. I am using Code First approach of EF6.My Parcel is linked to other entities having various relationship so when I remove those relationship from the Parcel Class it will work fine else run into an exception. Now what to do? – Harsh_Joshi Jun 23 '16 at 13:39
  • @nvoigt I think I have to include service known types in it. But I have total 26-30 Entities or classes, so should I have to include all those types or is there any other easy way to do it? – Harsh_Joshi Jun 23 '16 at 13:43
  • You need to include everything as a KnownType that is not visible from the class signature. For example if you have a list of IReceiver as part of the parcel, but the actual types are AReceiver and BReceiver, then those types need to be annotated as KnownTypes because WCF cannot find them from looking at the class definition. – nvoigt Jun 23 '16 at 15:56
  • @nvoigt I have so many types to be included. Don't you have a simple solution? Because if a type which is included contains a reference of the other type then that should also be included. And if is it so, then I will be mad one day. Because there are lots of them. – Harsh_Joshi Jun 24 '16 at 05:30
  • @nvoigt One more question. If i want to check which are those types that are missed to be included then how can I? – Harsh_Joshi Jun 24 '16 at 05:34

0 Answers0