0

第二次使用客户端引用调用底层方法时出现错误。在第一次通话时,它工作得很好。我已经用谷歌搜索并做了很多尝试,比如设置超时,但没有任何效果。任何建议将不胜感激。另外,如果需要更多详细信息,请告诉我,我将发布所需的代码。MDI 子窗体将调用此方法。

调试器显示 CommunicationException。Trace Viewer 显示消息:无法完成操作,因为管道已关闭。这可能是由于管道另一端的应用程序退出造成的。

合同

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

0 回答 0