双工 WCF 通信在 Metro 应用程序中不起作用

发布于 2025-01-07 07:36:51 字数 2396 浏览 5 评论 0原文

您能告诉我为什么与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2025-01-14 07:36:51

结果我无法在 Metro 中使用同步客户端。我必须使用异步调用。这就是为什么它不起作用。

Turns out I can't use synchronous client in Metro. I have to use asynchronous calls. That's why it didn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文