WCF service with Named Pipes
The nice thing of WCF is that you can easily switch connection methods. the only thing you need to change is some configuration and the type of the connection classes you’re using. All other code remains the same.
If you use named pipes for your WCF service you’re sharing memory between server and client. This is of course a very fast method, but the downside is that both the client and the server need to be running on the same machine.
This blog post is one in a series of posts.
- Creating a basic service in WCF
- Creating a WCF Client
- Hosting a WCF service in IIS
- WCF with TCP connection
- WCF with Named Pipes (current)
- Self hosted WCF service (coming soon)
If you followed previous post you will see creating a service that runs on named pipes works the same a configuring your service for TCP. There are three steps to follow:
- configure IIS
- change web.config
- change connection classes in the client
If you didn’t follow the previous blog strictly, you might not have installed the named pipes software for WCF. Check this in “turn windows features on or off” and install it if necessary.
You also might want to download the project code from the previous post. It's here.
Now, start up the IIS manager and select the wcf service on the left pane and click “bindings” on the right pane.
Click add.
Select the net.pipe option and put * in the text field.
Click “OK” and “Close”.
Now Click “Advanced Settings” in the right pane of IIS manager.
Add “net.pipe” to the enabled protocols
Click “OK” and close the IIS Manager.
Open the web.config file in the publish folder. Make sure you do not accidentally open the web.config in the project folder since that won’t work here.
Add a binding in system.servicemodel –> bindings
<netNamedPipeBinding> <binding name="InsecNamedPipe"> <security mode="None"></security> </binding> </netNamedPipeBinding>
And add an endpoint.
<endpoint address="net.pipe://localhost/messageservice.messageservice.svc" binding="netNamedPipeBinding" bindingConfiguration="InsecNamedPipe" contract="MessageService.IMessageService"></endpoint>
Save the config file.
Now open the project in visual studio. Since we are only using the client application you can unload MessageService project (but not necessary).
Change the button_click method in MainWindow.xaml.cs into this:
private void Button_Click(object sender, RoutedEventArgs e) { EndpointAddress endpoint = new EndpointAddress("net.pipe://localhost/MessageService.MessageService.svc"); NetNamedPipeBinding binding = new NetNamedPipeBinding(); binding.Security.Mode = NetNamedPipeSecurityMode.None; ChannelFactory<IMessageService> factory = new ChannelFactory<IMessageService>(binding, endpoint); IMessageService service = factory.CreateChannel(); MessageService.Message message = new MessageService.Message(); message.Reverse = true; message.StringValue = InputTextBox.Text; OutputTextBox.Text = service.ProcessMessage(message); }
Here only three changes are made:
- The endpoint address is changed (including the protocol!).
- The binding class is changed to NetNamedPipeBinding.
- The security mode in set to none since we don’t use security here.
Now run the client project and send some text to process!
Download the project here.
No Comments