双工 WCF 通信在 Metro 应用程序中不起作用
您能告诉我为什么与 TcpTransportBindingElement 的双工通信在我的 Metro 应用程序中不起作用吗?
根据 此文档 .Net Framework 的 Metro 子集支持 TCP 绑定。
所以我将 WCF 服务器编写为控制台应用程序。这是源代码:
static void Main()
{
UiWcfSession.OnInitialize += ClientInitialize;
var baseAddresses = new Uri("net.tcp://localhost:9000/");
var host = new ServiceHost(typeof(UiWcfSession), baseAddresses);
var reliableSession = new ReliableSessionBindingElement { Ordered = true, InactivityTimeout = TimeSpan.MaxValue };
var binding =
new CustomBinding(reliableSession, new TcpTransportBindingElement()) { ReceiveTimeout = TimeSpan.MaxValue };
host.AddServiceEndpoint(typeof(IClientFulfillmentPipeService), binding, "");
var metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
var mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");
host.Open();
Thread.CurrentThread.Join();
}
private static void ClientInitialize(int uiprocessid, string key)
{
Debug.WriteLine("ClientInitialize");
}
这是 Metro 应用程序中的客户端代码:
partial class MainPage
{
public MainPage()
{
InitializeComponent();
}
private void onclick(object sender, RoutedEventArgs e)
{
try
{
var ep = new EndpointAddress("net.tcp://localhost:9000/");
var binding = new CustomBinding(new TcpTransportBindingElement());
var ctx = new InstanceContext(new Wrapper());
var pipeFactory = new DuplexChannelFactory<IClientFulfillmentPipeService>(ctx, binding, ep);
IClientFulfillmentPipeService commChannel = pipeFactory.CreateChannel();
// open up the the comm channel with a reasonable timeout...
((IChannel)commChannel).Open();
commChannel.Initialize(1234, "Test");
((IChannel)commChannel).Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
所以它有点可以工作。但是,当我在 Metro 应用程序的调试器中一步步执行时,它会挂起并且永远不会从函数 commChannel.Initialize
返回。
为什么会这样?我缺少什么?
Could you please tell me why duplex communication with TcpTransportBindingElement doesn't work in my Metro application ?
According to this document Metro subset of .Net Framework supports TCP binding.
So I wrote WCF server as console application. Here is source code:
static void Main()
{
UiWcfSession.OnInitialize += ClientInitialize;
var baseAddresses = new Uri("net.tcp://localhost:9000/");
var host = new ServiceHost(typeof(UiWcfSession), baseAddresses);
var reliableSession = new ReliableSessionBindingElement { Ordered = true, InactivityTimeout = TimeSpan.MaxValue };
var binding =
new CustomBinding(reliableSession, new TcpTransportBindingElement()) { ReceiveTimeout = TimeSpan.MaxValue };
host.AddServiceEndpoint(typeof(IClientFulfillmentPipeService), binding, "");
var metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
var mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");
host.Open();
Thread.CurrentThread.Join();
}
private static void ClientInitialize(int uiprocessid, string key)
{
Debug.WriteLine("ClientInitialize");
}
and here is client code in Metro app:
partial class MainPage
{
public MainPage()
{
InitializeComponent();
}
private void onclick(object sender, RoutedEventArgs e)
{
try
{
var ep = new EndpointAddress("net.tcp://localhost:9000/");
var binding = new CustomBinding(new TcpTransportBindingElement());
var ctx = new InstanceContext(new Wrapper());
var pipeFactory = new DuplexChannelFactory<IClientFulfillmentPipeService>(ctx, binding, ep);
IClientFulfillmentPipeService commChannel = pipeFactory.CreateChannel();
// open up the the comm channel with a reasonable timeout...
((IChannel)commChannel).Open();
commChannel.Initialize(1234, "Test");
((IChannel)commChannel).Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
So it kinda works. But when I walk step by step in the debugger in Metro app it hangs and never returns from the function commChannel.Initialize
.
Why is it happening ? What am I missing ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果我无法在 Metro 中使用同步客户端。我必须使用异步调用。这就是为什么它不起作用。
Turns out I can't use synchronous client in Metro. I have to use asynchronous calls. That's why it didn't work.