无法从WPF应用程序访问WCF websocket服务

时间:2015-09-21 10:33:52

标签: .net wpf wcf websocket wcf-binding

我正在尝试通过Websockets使用简单的WCF服务(带有transportUsage="Always"的netHttpBinding)。

从控制台应用程序中使用服务时它可以正常工作,但是当从WPF应用程序中使用它时会抛出超时错误:

  

此请求操作发送到   http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/   在配置的超时(00:01:00)内没有收到回复。

transportUsage="Always"切换到transportUsage="Never"时没有超时。

如何从WPF应用程序访问websocket WCF服务?

服务合同:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);

}

服务:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}

配置:

<system.serviceModel>
  <services>
    <service name="WcfServiceLibrary1.Service1">
      <host>
        <baseAddresses>
          <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
        </baseAddresses>
      </host>
      <endpoint address="" binding="netHttpBinding" contract="WcfServiceLibrary1.IService1" bindingConfiguration="My_NetHttpBindingConfig">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
  </services>
  <bindings>
    <netHttpBinding>
      <binding name="My_NetHttpBindingConfig">
        <webSocketSettings transportUsage="Always"/>
      </binding>
    </netHttpBinding>
  </bindings>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
        <serviceDebug includeExceptionDetailInFaults="False" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel> 

控制台代码(工作正常):

    static void Main(string[] args)
    {

        Service1Client c = new Service1Client("NetHttpBinding_IService1");
        string s = c.GetData(8);
        Console.WriteLine(s);
        Console.ReadLine();
    }

WPF代码(不起作用):

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Service1Client c = new Service1Client("NetHttpBinding_IService1");
        string s = c.GetData(5);
        MessageTextBlock.Text=s;
    }

WPF和控制台应用程序的配置相同:

<system.serviceModel>
    <bindings>
        <netHttpBinding>
            <binding name="NetHttpBinding_IService1">
                <webSocketSettings transportUsage="Always" />
            </binding>
        </netHttpBinding>
    </bindings>
    <client>
        <endpoint address="ws://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
            binding="netHttpBinding" bindingConfiguration="NetHttpBinding_IService1"
            contract="ServiceReference1.IService1" name="NetHttpBinding_IService1">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

1 个答案:

答案 0 :(得分:0)

我刚刚在另一个论坛上收到了答案:这是主要UI渠道的僵局。

详细解释和解决方法可以在这里找到:

http://blogs.msdn.com/b/dsnotes/archive/2015/09/22/wpf-winform-nethttpbinding-timeout-deadlock-issue-on-main-ui-thread-using-web-sockets.aspx