簡體   English   中英

啟用可靠 Session 時 WCF 合同過濾器不匹配

[英]WCF ContractFilter Mismatch when enabling Reliable Session

我有一個托管在 Windows 服務中的 WCF 服務。 然后我有一個 GUI(客戶端),然后與該服務通信。 最近有報道稱,與服務的通信在空閑 10 分鍾后停止。

我做了一些研究,看起來該服務由於不活動而丟棄了連接。 因此,我想增加接收超時並啟用可靠會話並將 inactivityTimeout 設置為更長。 但是,當我在 WCF 服務和客戶端 app.config 文件中執行此操作時,我收到以下錯誤:

客戶端產生的錯誤

設置reliableSession enabled="False" 會導致客戶端和服務運行。 (雖然只有10分鍾)

做一些研究,建議是因為以下三個原因之一:

  • 客戶和發件人之間有不同的合同。
  • 您在客戶端和發件人之間使用了不同的綁定。
  • 客戶端和發送者之間的消息安全設置不一致。

但是據我所知,客戶端和服務器之間的設置/合同是一致的。 我希望這是一件愚蠢的事情。 這是我的服務和客戶端的 app.config:

服務

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IHwResourceManagerWcfService" receiveTimeout="infinite" >
          <reliableSession enabled="True" inactivityTimeout="infinite"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="net.tcp://localhost:8523/HwResourceManagerWcfService"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHwResourceManagerWcfService"
        contract="WindowsServices.IHwResourceManagerWcfService" name="NetTcpBinding_IHwResourceManagerWcfService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
    <services>
      <service name="MT.Tools.HwResourceManager.WCF.HwResourceManagerWcfService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="MT.Tools.HwResourceManager.WCF.IHwResourceManagerWcfService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8523/HwResourceManagerWcfService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

客戶

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IHwResourceManagerWcfService" receiveTimeout="infinite" >
          <reliableSession enabled="True" inactivityTimeout="infinite"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="net.tcp://localhost:8523/HwResourceManagerWcfService"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHwResourceManagerWcfService"
        contract="WindowsServices.IHwResourceManagerWcfService" name="NetTcpBinding_IHwResourceManagerWcfService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>

</configuration>

任何幫助將不勝感激。

您的配置中有幾個問題。 這個項目似乎是一個 class 庫項目。 請使用 WCF 服務申請模板。 那么服務的基地址應該配置在IIS中,而不是配置文件中。 此外,您的綁定配置不會生效,因為您沒有在服務端點中應用它。

<endpoint address="" binding="netTcpBinding" bindingConfiguration=""

請參考我的例子。
服務器(WCF 服務應用程序)。

IService.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(string value);
}

服務1.svc

public class Service1 : IService1
    {
        public string GetData(string value)
        {
            return DateTime.Now.ToLongTimeString();
        }
}

Web.config

  <system.serviceModel>
    <services>
      <service name="WcfService3.Service1">
        <endpoint address="" binding="netTcpBinding" 
          contract="WcfService3.IService1">
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" 
          contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

然后我們在 IIS 中部署服務。 在我們部署它之前,我們應該為net.tcp協議啟用 windows 功能。
在此處輸入圖像描述
在網站上添加對net.tcp協議的支持。
在此處輸入圖像描述
然后添加站點綁定。
在此處輸入圖像描述
我們需要注意的另一件事是確保以下服務正在運行 state。
在此處輸入圖像描述
客戶。 (通過添加服務引用,客戶端代理發送調用)

static void Main(string[] args)
{
    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
    //by default, the nettcpbinding uses windows credential, we should provide server windows account.
    client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
    client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";
    try
    {
        var result = client.GetData("Hello");
        Console.WriteLine(result);
    }
    catch (Exception)
    {
        throw;
    }    
}

應用程序配置。

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="NetTcpBinding_IService1">
                <security>
                    <transport sslProtocols="None" />
                </security>
            </binding>
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint address="net.tcp://vabqia969vm/Service1.svc" binding="netTcpBinding"
            bindingConfiguration="NetTcpBinding_IService1" contract="ServiceReference1.IService1"
            name="NetTcpBinding_IService1">
            <identity>
                <servicePrincipalName value="host/vabqia969VM" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

結果。
在此處輸入圖像描述
如果有什么我可以幫忙的,請隨時告訴我。

在對此進行了更多閱讀之后,我發現了我看到的錯誤的原因以及連接超時問題的解決方案。

錯誤 - 錯誤是因為我設置了不同的綁定配置。 通過將客戶端和服務設置為空字符串,錯誤被刪除。

超時問題 - 即使啟用了可靠連接並且對於不活動和接收超時的長時間超時,10 分鍾的連接問題仍然存在。 然后我閱讀了一篇文章,該文章建議長時間超時是錯誤的做法。 相反,它建議處理故障異常並嘗試重新連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM