WCF错误:由于管道已关闭,无法完成操作

时间:2016-06-22 07:24:32

标签: c# wcf exception communicationexception

当第二次使用客户端引用来调用底层方法时,我收到错误。在第一次通话时,它完美无缺。我用谷歌搜索了它并做了很多尝试,比如设置超时但没有任何效果。任何建议将受到高度赞赏。此外,如果需要更多详细信息,请告知我们,我将发布所需的代码。 MDI子窗体将调用此方法。

调试器显示CommunicationException。 跟踪查看器显示消息:由于管道已关闭,无法完成操作。这可能是由管道另一端的应用程序退出引起的。

合同

[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
}

合同执行

[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
}

服务器配置

<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>

客户端配置

  <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>

服务助手类我在这里得到了通信异常

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

0 个答案:

没有答案