【发布时间】:2012-02-21 02:07:37
【问题描述】:
您能否告诉我为什么与 TcpTransportBindingElement 的双工通信在我的 Metro 应用程序中不起作用?
根据this文档,.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 返回。
为什么会这样?我错过了什么?
【问题讨论】:
标签: c# .net wcf microsoft-metro