Search code examples
.netwpfwcfwebsocketwcf-binding

Can't access WCF websocket service from WPF application


I'm trying to use a simple WCF service via Websockets (netHttpBinding with transportUsage="Always").

It works fine when consuming the service from a console application, but throws a timeout error, when consuming it from a WPF application:

This request operation sent to http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/ did not receive a reply within the configured timeout (00:01:00).

There is not timeout when switching from transportUsage="Always" to transportUsage="Never".

How can I access a websocket WCF service from a WPF application?

Service Contract:

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

}

The Service:

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

The configuration:

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

Console Code (works fine):

    static void Main(string[] args)
    {

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

WPF code (doesn't work):

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

The configuration is identical for both WPF and the console application:

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

Solution

  • I just received the answer on another forum: It's a deadlock on the main UI channel.

    A detailed explanation and workaround can be found here:

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