WCF Services
A WCF service keeps data in either XML or JSON format that can be processed by any language. In other words if you create a Service application then it can communicate with any application.
Now I will explain how to create a simple WCF Service and how to host it in a console application.
Step 1: Open Visual Studio 2010.
Step 2: Now click on "New Project" -> "Windows" -> "Class Library" and add the name of Class library; I used "Hello".
![Class Library]()
Step 3: Now delete Class1.cs
Step 4: Now go to the Solution Explorer click on "Add" > "New Item".
![New Item]()
Step 5: Add the WCF Service and add the name of the service.
![name of service]()
Step 6: And now declare the method in the interface as in the following:
- [OperationContract]
- string getmessage(string name);
And implement it in the .cs file as in the following:
- public string getmessage(string name)
- {
- return "Hello" + name;
- }
And build the project.
Step 7: Now delete the app.config file by first right-clicking on the Solution Explorer as in the following:
![Solution Explorer]()
Step 8: Now we want to host this WCF Service in a console application. For that we need to add the console application from "Add" > "New Project" and select the Console Application and add the name of application just like I gave HelloService.
![New Project]()
Step 9: Now first we need to add the reference for the ServiceModel. To add the services in the console application click on the console application and go to "References" > "Add Reference" and then add the reference of serviceModel.
![serviceModel]()
And add the reference of service also.
![add the reference of service]()
And now add the App.config in the console application. For adding it first right-click on the console application and go to "Add" > "New Item" > "Application Configuration File".
Step 10: Now we need to configure the service in the app.config file so for that go to "Tools" > "WCF Service Configuration Editor". Then go to "File" > "Open" > "Config File" and open the config file of this project where it is present and then click on Create a New Service.
![Create a New Service]()
And then open the DLL of the service and then open the service reference.
![open the service reference]()
And click on the next button 2 times and then for communication mode we choose TCP binding and then again click "Next >".
![TCP binding]()
And now it shows an address that is the address of the end point that can be a relative address and it specifies the base address. You can give the base address that you want, just as I used hello.
![base address]()
And then click on "Next" then it will show all the details about the service and then click on Finish.
Step 11: Now we have added a Relative address to add the Base Address of the service so to add the base address first go to host and then we need 2 base addresses, one is for the service end point and the second one is for the metadata to be available.
![]()
And now after click on new provide the base address of the service just like I used
http://localhost:8080.
![base address of service]()
And now again add the address of the metadata available. Click on new again and provide the address, I used net.tcp://localhost:8090.
Step 12: Now to enable the metadata information we need to set the service behaviour. First expand Advanced and then Service Behaviour and here click on New Service Behaviour Configuration.
![Service Behaviour Configuration]()
And then click on Add and the service Metadata.
![service Metadata]()
And then here go to the service Metadata and set the property HttpGetEnabled to true.
![HttpGetEnabled]()
Now we need to associate the behaviour with service so for that:
![behaviour with service]()
And now close this and it will ask to save the changes, click Yes and then you will see the app.config file as:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <system.serviceModel>
- <behaviors>
- <serviceBehaviors>
- <behavior name="NewBehavior0">
- <serviceMetadata httpGetEnabled="true" />
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <services>
- <service behaviorConfiguration="NewBehavior0" name="Hello.Service1">
- <endpoint address="hello" binding="netTcpBinding" bindingConfiguration=""
- contract="Hello.IService1" />
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8080" />
- <add baseAddress="net.tcp://localhost:8090" />
- </baseAddresses>
- </host>
- </service>
- </services>
- </system.serviceModel>
- </configuration>
Step 13: Now add the code inside in the console application for the host.
- static void Main(string[] args)
- {
-
- using (ServiceHost host = new ServiceHost(typeof(Hello.Service1)))
- {
-
- host.Open();
- Console.WriteLine(DateTime.Now.ToString());
- host.Close();
- Console.ReadLine();
- }
- }
Output
![Output]()
The advantages of self-hosting in WCF are:
- Very easy to setup with app.config file.
- For debugging we don't need to attach the separate process, it is easy to debug.
- It supports all bindings.
- Very flexible to control the lifetime of the service using the open and close methods of the service.
The disadvantages of self-hosting in WCF are:
- The service is available only for the client.
- We need to start the WCF service manually and it doesn't support automatic message-based activation.
- It needs custom code.