简体   繁体   中英

net.pipe service host in WPF app

The contract:

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

The service:

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

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

In WPF app I do the following:

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

This WPF app launches another app like this:

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

The app named TPM starts properly. Then I do attach to process in the debugging menu of Visual Studio and I see the client says that nobody is listening at the endpoint.

Here is the client:

 [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 There was no endpoint listening at "net.pipe://localhost/AkmDaemon"... blablabla

You are creating your ServiceHost in a using statement, so it is disposed right after the Open call. The Dispose call closes the ServiceHost.

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

Just drop the using block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM