1

合同:

[ServiceContract]
public interface IDaemonService {
    [OperationContract]
    void SendNotification(DaemonNotification notification);
}

服务:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class DaemonService : IDaemonService {
    public DaemonService() {
    }

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

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

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

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

Task.Factory.StartNew(() => {
                var tpm = new Process { StartInfo = { FileName = "TPM" } };
                tpm.Start();
                }
            });

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

这是客户端:

 [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.pipe://localhost/AkmDaemon”没有监听端点...... blablabla

4

1 回答 1

2

您正在using声明中创建 ServiceHost,因此它会在Open调用后立即处理。该Dispose调用将关闭 ServiceHost。

using (host = new ServiceHost(...))
{
    host.AddServiceEndpoint(...);
    host.Open();
}
// ServiceHost.Dispose() called here

只需放下 using 块。

于 2014-03-04T08:15:16.810 回答