簡體   English   中英

WPF應用程序中的net.pipe服務主機

[英]net.pipe service host in WPF app

合同:

[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

您正在using語句創建ServiceHost,因此它在Open調用之后立即處理。 Dispose調用關閉ServiceHost。

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

只需刪除使用塊即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM