首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF应用程序中的net.pipe服务主机

WPF应用程序中的net.pipe服务主机
EN

Stack Overflow用户
提问于 2014-03-04 05:14:09
回答 1查看 593关注 0票数 1

合同:

代码语言:javascript
复制
[ServiceContract]
public interface IDaemonService {
    [OperationContract]
    void SendNotification(DaemonNotification notification);
}

服务:

代码语言:javascript
复制
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class DaemonService : IDaemonService {
    public DaemonService() {
    }

    public void SendNotification(DaemonNotification notification) {
        App.NotificationWindow.Notify(notification);
    }
}

在WPF应用程序中,我执行以下操作:

代码语言:javascript
复制
using (host = new ServiceHost(typeof (DaemonService), new[] {new Uri("net.pipe://localhost")})) {
            host.AddServiceEndpoint(typeof (IDaemonService), new NetNamedPipeBinding(), "AkmDaemon");                
            host.Open();
        } 

这个WPF应用程序启动另一个应用程序,如下所示:

代码语言:javascript
复制
Task.Factory.StartNew(() => {
                var tpm = new Process { StartInfo = { FileName = "TPM" } };
                tpm.Start();
                }
            });

名为TPM的应用程序启动正常。然后,我在Visual的调试菜单中附加到进程,我看到客户端说没有人在监听端点。

这是客户端:

代码语言:javascript
复制
 [Export(typeof(DaemonClient))]
public class DaemonClient : IHandle<DaemonNotification> {
    private readonly ChannelFactory<IDaemonService> channelFactory;
    private readonly IDaemonService daemonServiceChannel;

    public DaemonClient(IEventAggregator eventAggregator) {           
        EventAggregator = eventAggregator;
        EventAggregator.Subscribe(this);

        channelFactory = new ChannelFactory<IDaemonService>(new NetNamedPipeBinding(),
            new EndpointAddress("net.pipe://localhost/AkmDaemon"));            
        daemonServiceChannel = channelFactory.CreateChannel();
    }

    public IEventAggregator EventAggregator { get; private set; }

    public void Handle(DaemonNotification message) {
        daemonServiceChannel.SendNotification(message); //Here I see that the endpoint //is not found
    }

    public void Close() {
        channelFactory.Close();
    }
}

EndpointNotFoundException没有端点侦听“net.管道://localhost/AkmDaemon”.短唇

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-04 08:15:16

您是在一个ServiceHost语句中创建您的using语句,因此在Open调用之后立即对它进行处理。Dispose调用关闭ServiceHost。

代码语言:javascript
复制
using (host = new ServiceHost(...))
{
    host.AddServiceEndpoint(...);
    host.Open();
}
// ServiceHost.Dispose() called here

把使用的块放下来。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22163473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档