【问题标题】:Duplex WCF communication doesn't work in Metro application双工 WCF 通信在 Metro 应用程序中不起作用
【发布时间】: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


    【解决方案1】:

    原来我不能在 Metro 中使用同步客户端。我必须使用异步调用。这就是它不起作用的原因。

    【讨论】:

      猜你喜欢
      • 2011-03-23
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多