无法从 WPF 应用程序访问 WCF websocket 服务
Can't access WCF websocket service from WPF application
我正在尝试通过 Websockets(netHttpBinding 与 transportUsage="Always"
)使用简单的 WCF 服务。
从控制台应用程序使用服务时工作正常,但从 WPF 应用程序使用服务时抛出超时错误:
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).
从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>
我刚在另一个论坛上收到答案:主UI频道出现死锁。
可以在此处找到详细的解释和解决方法:
我正在尝试通过 Websockets(netHttpBinding 与 transportUsage="Always"
)使用简单的 WCF 服务。
从控制台应用程序使用服务时工作正常,但从 WPF 应用程序使用服务时抛出超时错误:
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).
从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>
我刚在另一个论坛上收到答案:主UI频道出现死锁。
可以在此处找到详细的解释和解决方法: