【发布时间】: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