Hosting a WCF service in IIS
In previous posts I described how to create a basic web service and connect to it without using a proxy generated by Visual Studio. The next step is to describe how to setup IIS to use WCF through Http and connect to it with a client.
This post is the third in a series of posts.
- Creating a basic service in WCF
- Creating a WCF Client
- Hosting a WCF service in IIS (current)
- WCF with TCP
- WCF with Named Pipes
- Self hosted WCF service (coming soon)
For this tutorial I’m using the code as described in my previous post. If you want to start here, download the solution.
Configuring IIS
First check if all necessary software is installed.
Start up “Windows Features”. I assume you already have IIS installed but if not you can do so here. If IIS is installed you can add WCF services to the .NET framework under the option .NET Framework 4.5 Advanced Services.
Click OK to install the WCF services.
Next start up IIS manager by clicking start and typing IIS in the start menu. Right click sites and click add website.
Fill in the settings like below picture. The path will be the path where the project will get published to. Don’t use the path to the project but a path to an empty folder.
Click “Select” to check the settings of the application pool.
Make sure you are using .Net version 4 (or higher) and pipeline mode. I chose to use the default app pool but you could create a new one.
Click ok twice so the web application is created.
Publishing the project
Next open the Visual Studio solution and right click the MessageService project. Click publish.
Choose the same path as you did while creating the web application and click “publish”.
Configuring the service
Open the web.config in the pubished folder (not the one in the solution), and add a binding to the system.servicemodel part.
<bindings> <basicHttpBinding> <binding name="InsecHttp"> <security mode="None" /> </binding> </basicHttpBinding> </bindings>
Change the base address and the port of the service and use the new binding in the end point configuration.
<services> <service name="MessageService.MessageService"> <host> <baseAddresses> <add baseAddress="http://localhost:8734/" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- Unless fully qualified, address is relative to base address supplied above --> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="InsecHttp" contract="MessageService.IMessageService"> <!-- Upon deployment, the following identity element should be removed or replaced to reflect the identity under which the deployed service runs. If removed, WCF will infer an appropriate identity automatically. --> <identity> <dns value="localhost" /> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> <!-- This endpoint does not use a secure binding and should be secured or removed before deployment --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services>
Now the service should be running. To check this you can type the url of the service in the browser. In the publish folder there is a file with an svc extension. Go to this file in the browser: http://localhost:8734/MessageService.MessageService.svc
Here you see the url you can use in the WCF test client to test the service.
Connect the client to the service
Now the service is running and ready to receive requests. The next step is to connect the client to the service.
First prevent the service project from starting up while debugging. Right click the solution in Visual Studio and click “properties”.
Click “startup project” and make sure only the WcfClients project is started up.
Click ok to save.
Open MainWindow.xaml.cs and change the end point address in the Button_Click event.
private void Button_Click(object sender, RoutedEventArgs e) { EndpointAddress endpoint = new EndpointAddress("http://localhost:8734/MessageService.MessageService.svc"); Binding binding = new BasicHttpBinding(); 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); }
Debug the solution and test if the client works.
Congratulations! You just connected your WCF client to an in IIS hosted service.
Download the complete code here.
I am genuinely thankful to the holder of this web page
How To Start A Convenience Store saidwho has shared this enormous article at here.
Since the admin of this web page is working, no question very quickly
https://www.viagrapascherfr.com/vente-viagra-france/ saidit will be well-known, due to its quality contents.