Хост службы net.pipe в приложении WPF

Контракт:

[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

1 ответ

Решение

Вы создаете свой ServiceHost в using заявление, поэтому он расположен сразу после Open вызов. Dispose вызов закрывает ServiceHost.

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

Просто бросьте блок использования.

Другие вопросы по тегам