[英]Duplex WCF communication doesn't work in Metro application
您能告诉我为什么在我的Metro应用程序中无法使用TcpTransportBindingElement进行双工通信吗?
根据此文档,.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
函数commChannel.Initialize
。
为什么会这样呢? 我想念什么?
原来我不能在Metro中使用同步客户端。 我必须使用异步调用。 这就是为什么它不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.