WCF Named Pipe Communication: A Minimal Example
Question: How can I create a simple WCF application that utilizes named pipes for communication?
Answer:
To establish communication via named pipes in WCF, the following steps are necessary:
Server Configuration:
<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);
Client Configuration:
// 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();
These modifications ensure that the WCF application communicates through the specified named pipe, allowing for direct communication between server and client applications.
The above is the detailed content of How to Create a Minimal WCF Application Using Named Pipes?. For more information, please follow other related articles on the PHP Chinese website!