Introduction
For a WCF service to be available for the clients to consume, we need to host the WCF service. I highly recommend reading the previous parts:
- Hosting WCF Service: Part 1
- Hosting WCF Service: Part 2
Internet Information Services (IIS)No code is required to host a WCF service, there is no need to write code to instantiate and start the service host, as is the case with self-hosting. Automatic message based activation of IIS provides automatic message-based activation. This means that the service can be activated on demand. When a message arrives at the service, it then launches itself and fulfils the request. In case of self-hosting, the service should be always be running.
Step 1Creating WCF Service
- Open Visual Studio.
- Go To File -> New Project.
- Select WCF Service Application.
- Provide it a name like "SquareService".
Figure 1: SquareService
- Delete the default created Service1.cs and Service1.svc file.
- Add a new svc file as ini the following and name it SquareService, then right-click on the application and select Add -> New Item and Select WCF Service.
Figure 2: WCF Service
Figure 3: WCF Service1
- Replace the following code in the ISquareService interface.
- [ServiceContract]
- public interface ISquareService
- {
- [OperationContract]
- void DoWork();
- }
With:
- [ServiceContract]
- public interface ISquareService
- {
- [OperationContract]
- int GetSqaure(int num);
- }
- Replace the following code in the SquareService.svc file:
- public void DoWork()
- {
-
- }
- With: public int GetSqaure(int num)
- {
- return num * num;
- }
- Build the solution.
Step 2
Now to set up to publish the service to be hosted in IIS.
- Right-click on Application, select Publish.
Figure 4: Publish
- Create profile to publish the application.
Figure 5: IISHostService
When you click on Custom, it will show like the preceding entered profile name IISHostService.
- After clicking Next it will go to the next step.
Figure 6: File System
Select the Publish method then select File System, it will ask for the default path to publish the service.
Figure 7: Publish Method
- Click on the Next Button, it will show the profile created previously with the option select Configuration to Build 1. Debug 2. Release. The publish options are like the following screen:
Figure 8: Configuration
- Click on the Publish button, it will show the following screen:
Figure 9: Output window
In the preceding screen you can see the Output window and the folder created in the Solution Explorer and that we created the profile name IISHostService.
- You can see the folder path also, like in the following screen:
Figure 10: Folder Path
Step 3
Hosting Created service on IIS.
- Click on the Start Menu then search for inetmgr then press Enter, it will open like the following:
Figure 10: Inetmgr
- Right-click on Sites then select Add Web Site.
Figure 11: Add Web Site
- It will open a new window as in the following screen.
Figure 12:Path of Publish folder
Enter the site Name and the path of the Publish folder and assigned the port number then click OK.
- Now you can see that the web site is added in IIS.
Figure 13: Added in the IIS
- Right-click on SquareService.svc then select Browse.
Figure 14: Browse
- Now you see in the output that we created the service is hosted on IIS.
Figure 15:Hosted on IIS
Step 4
Consume the IIS hosted service in Console Application.
- Open Visual Studio.
- Go to File-> New Project.
- Select Console Application then name it IISConsume then click OK.
Figure 16: Console Application
- Open Solution Explorer then right-click on References then select Add Service References.
Figure 17: Add Service References
- It will show a window like the following screen.
Figure 18: Popup Window
- Copy the browse service URL then paste it into the Address: box then click the Go Button then it will show the following screen.
Figure 19: Services box
In the preceding screen, you can see that the Services box shows the interface name and inside the Operations box it showa the method name GetSquare and provides the namespace name IISHostedService then click OK.
- Now you can see in Solution Explorer there is one folder added like the following screen.
Figure 20:Solution Explorer
- Now to test the IIS hosted service. Use the following code in the Program.cs file.
Code to consume the service:
- static void Main(string[] args)
- {
- IISHostedService.SquareServiceClient service = new IISHostedService.SquareServiceClient();
- var res = service.GetSqaure(5);
- Console.WriteLine("Sqaure of 5 is {0}", res);
- Console.ReadKey();
- }
- Build the application and Run. It will show output like following:
Figure 21:Build the application
Note: For detailed code please download the Zip file attached above.
Summary
I hope you understand the concepts of Hosting a WCF Service on Internet Information Services (IIS) and it is useful for all readers. If you have any suggestion regarding this article then please contact me.