WCF 命名管道通訊:一個最小範例
問題:如何建立一個簡單的WCF 應用程式命名管道通信?
答案:
要在 WCF 中透過命名管道建立通信,需要執行以下步驟:
伺服器配置:
<endpoint address="net.pipe://localhost/[pipe_name]" binding="netNamedPipeBinding" bindingConfiguration="" contract="ICalculator" name="NetNamedPipeBinding_ICalculator"> </endpoint>
// Create a URI using the named pipe format Uri baseAddress = new Uri("net.pipe://localhost/[pipe_name]"); // Create a service host ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); // Add the service endpoint using the netNamedPipeBinding selfHost.AddServiceEndpoint(typeof(ICalculator), new NetNamedPipeBinding(), "CalculatorServicePipe"); // Enable metadata exchange for hosting ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; selfHost.Description.Behaviors.Add(smb);設定服務主機:
設定服務主機:
// Create a client endpoint for the pipe EndpointAddress endpoint = new EndpointAddress($"net.pipe://localhost/[pipe_name]", endpoint_uri); // Create a new client channel factory ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new NetNamedPipeBinding(), endpoint); // Obtain a client proxy ICalculator client = channelFactory.CreateChannel();
設定服務主機:
設定服務主機:客戶端設定:產生客戶端:以上是如何使用命名管道創建最小的 WCF 應用程式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!