【问题标题】:Can't access WCF websocket service from WPF application无法从 WPF 应用程序访问 WCF websocket 服务
【发布时间】:2015-12-18 00:35:22
【问题描述】:

我正在尝试通过 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>

【问题讨论】:

    标签: .net wpf wcf websocket wcf-binding


    【解决方案1】:

    我刚刚在另一个论坛上收到了答案:这是主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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      相关资源
      最近更新 更多