2013-05-08 120 views
7

我們流使用WCF回調的數據。 WCF託管在IIS中(默認設置,只是將net.tcp添加到協議中)。WCF在安全模式下沒有給出的net.tcp例外

我們正試圖在WCF禁用安全性,所以我設置安全模式爲「無」,在客戶端和服務器上,但我得到了以下的CommunicationException:

套接字連接被中止。這可能是由於處理您的消息的錯誤 或遠程主機的 接收超時或基礎網絡資源問題所致。本地套接字 超時時間爲'00:00:59.8439844'。

我已經把關於服務跟蹤和TraceViewer給了我下面的異常(System.ServiceModel.ProtocolException):

流安全要求在 http://www.w3.org/2005/08/addressing/anonymous,但沒有安全 上下文經過談判。這很可能是由遠程端點 從其綁定中丟失了StreamSecurityBindingElement引起的。

客戶端的Program.cs:

using System; 
using System.ServiceModel; 
using WcfClient.ServiceReference1; 

class Program { 
    static void Main() { 
     Callback callback = new Callback(); 
     InstanceContext context = new InstanceContext(callback); 
     Service1Client service1Client = new Service1Client(context, 
                  "NetTcpBinding_IService1"); 


     service1Client.GetData(0); 
     Console.Read(); 
     service1Client.Stop(); 
    } 
} 

internal class Callback : IService1Callback { 
    public void StreamSignalData(int[] result) { 
     foreach (int i in result) { 
      Console.WriteLine(DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff") + 
           ": " + i); 
     } 
    } 
} 

客戶App.Config中

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <bindings> 
     <netTcpBinding> 
     <binding name="NetTcpBinding_IService1"> 
      <security mode="None" /> 
     </binding> 
     </netTcpBinding> 
     <wsDualHttpBinding> 
     <binding name="WSDualHttpBinding_IService1"> 
      <reliableSession ordered="true" /> 
      <security mode="None" /> 
     </binding> 
     </wsDualHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost/wcf-error/Service1.svc" 
     binding="wsDualHttpBinding" 
     bindingConfiguration="WSDualHttpBinding_IService1" 
     contract="ServiceReference1.IService1" 
     name="WSDualHttpBinding_IService1" /> 
     <endpoint address="net.tcp://localhost/wcf-error/Service1.svc" 
     binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService1" 
     contract="ServiceReference1.IService1" name="NetTcpBinding_IService1"> 
     </endpoint> 
    </client> 
    </system.serviceModel> 
</configuration> 

WCF Service1.svc.cs

using System; 
using System.Net.Security; 
using System.ServiceModel; 
using System.Threading; 

[ServiceContract(SessionMode = SessionMode.Required, 
       CallbackContract = typeof(IStreamCallback), 
       ProtectionLevel = ProtectionLevel.None)] 
public interface IService1 { 
    [OperationContract] 
    void GetData(int value); 

    [OperationContract] 
    void Stop(); 
} 

public interface IStreamCallback { 
    [OperationContract(IsOneWay = true)] 
    void StreamSignalData(int[] result); 
} 

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] 
public class Service1 : IService1 { 
    private Timer _timer; 

    private readonly IStreamCallback _callback = 
      OperationContext.Current.GetCallbackChannel<IStreamCallback>(); 
    public void GetData(int value) { 
     _timer = new Timer(StreamData, null, 0, 500); 
    } 

    public void Stop() { 
     _timer.Dispose(); 
    } 

    private void StreamData(object state) { 
     int[] randomNumbers = new int[50]; 
     Random random = new Random(); 
     for (int i = 0; i < 50; i++) { 
      randomNumbers[i] = random.Next(100); 
     } 
     _callback.StreamSignalData(randomNumbers); 
    } 
} 

WCF的Web.config

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation targetFramework="4.0" debug="true"/> 
    </system.web> 
    <system.serviceModel> 
    <bindings> 
     <netTcpBinding> 
     <binding name="netTcpBinding" > 
      <security mode="None" /> 
     </binding> 
     </netTcpBinding> 
     <wsDualHttpBinding> 
     <binding name="wsDualHttpBinding"> 
      <security mode="None" /> 
     </binding> 
     </wsDualHttpBinding> 
    </bindings> 
    <protocolMapping> 
     <add scheme="http" binding="wsDualHttpBinding"/> 
    </protocolMapping> 
    <services> 
     <service name="Service"> 
     <endpoint address="" binding="wsDualHttpBinding" 
      bindingConfiguration="wsDualHttpBinding" 
      name="EndPointHTTP" contract="WcfService1.IService1"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" name="mex" 
      contract="IMetadataExchange" /> 
     <endpoint binding="netTcpBinding" bindingConfiguration="netTcpBinding" 
      name="EndPointTCP" contract="WcfService1.IService1" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
      <useRequestHeadersForMetadataAddress /> 
      <dataContractSerializer /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

任何想法,這可能是? 谷歌上的大部分命中都表示客戶端和服務器上的配置應該是相同的(即安全性爲無),但我似乎無法找到該錯誤。 谷歌搜索StreamSecurityBindingElement想出了沒有好explenations ...

+0

哪一方(客戶端/服務)給你日誌中的錯誤? – 2013-05-08 18:45:55

+0

@PetarVučetin服務。 – ahoibakk 2013-05-09 06:17:12

+0

@PetarVučetin我試圖在客戶端上進行跟蹤,但是除了已經拋出的異常外,它沒有給出任何其他信息。 – ahoibakk 2013-05-12 10:34:32

回答

1

我無法複製你的問題,但我有相同的設置和幾件事情,我注意到:

在WCF Service1.svc.cs創建namesapace。

namespace WcfService1 
    { 
     [ServiceContract(SessionMode = SessionMode.Required, 
      CallbackContract = typeof (IStreamCallback), 
      ProtectionLevel = ProtectionLevel.None)] 
     public interface IService1 
     { 
     ..... 
     [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
     [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] 
     public 

    class Service1 : IService1 
    { 

<service name="Service">應該是<service name="WcfService1.Service1">。服務名稱已滿服務的類型。在配置

服務部分:

<services> 
    <service name="WcfService1.Service1"> 
     <endpoint address="" binding="wsDualHttpBinding" 
      bindingConfiguration="wsDualHttpBinding" 
      name="EndPointHTTP" contract="WcfService1.IService1"> 
      <identity> 
       <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" name="mex" 
      contract="IMetadataExchange" /> 
     <endpoint binding="netTcpBinding" bindingConfiguration="netTcpBinding" 
      name="EndPointTCP" contract="WcfService1.IService1" /> 
    </service> 
</services> 

WCF Service1.svc。CS

using System; 
using System.Net.Security; 
using System.ServiceModel; 
using System.Threading; 

namespace WcfService1 
{ 
    [ServiceContract(SessionMode = SessionMode.Required, 
     CallbackContract = typeof (IStreamCallback), 
     ProtectionLevel = ProtectionLevel.None)] 
    public interface IService1 
    { 
     [OperationContract] 
     void GetData(int value); 

     [OperationContract] 
     void Stop(); 
    } 

    public interface IStreamCallback 
    { 
     [OperationContract(IsOneWay = true)] 
     void StreamSignalData(int[] result); 
    } 

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] 
    public class Service1 : IService1 
    { 
     private Timer _timer; 

     private readonly IStreamCallback _callback = 
      OperationContext.Current.GetCallbackChannel<IStreamCallback>(); 

     public void GetData(int value) 
     { 
      _timer = new Timer(StreamData, null, 0, 500); 
     } 

     public void Stop() 
     { 
      _timer.Dispose(); 
     } 

     private void StreamData(object state) 
     { 
      int[] randomNumbers = new int[50]; 
      Random random = new Random(); 
      for (int i = 0; i < 50; i++) 
      { 
       randomNumbers[i] = random.Next(100); 
      } 
      _callback.StreamSignalData(randomNumbers); 
     } 
    } 
} 

回調

確保您處理_callback.StreamSignalData(randomNumbers);當客戶端斷開連接時。你會得到一個通信異常。

+0

感謝您的輸入,但不幸的是,這並沒有解決我的問題 – ahoibakk 2013-05-13 17:00:14

+0

這很奇怪。那麼 - 我贏得了「它在我的電腦上運行」徽章: – 2013-05-13 19:06:09