Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 27 - Hosting a wcf service in a windows service

Suggested Videos
Part 24 - Self hosting a wcf service in console application
Part 25 - Self hosting a wcf service in winforms application
Part 26 - What is a windows service



In this video we will discuss, hosting a wcf service in a windows service. This is continuation to Part 26. Please watch Part 26, before proceeding. We will be working with the same example, that we worked with in Part 25.



Steps to create a windows service and host wcf service
Step 1: Right click on HelloService solution in solution explorer and select Add - New Project.

Step 2: From the Add New Project dialog box, select "Windows" under "Installed Templates". Select "Windows Service" project template. Type "WindowsServiceHost" in the "Name" textbox.

Step 3: Rename Service1.cs file to "HelloWindowsService.cs" and press "Enter" key. This will prompt you to change all references of "Service1". Click "Yes".

Step 4: Right click on "References" folder under "WindowsServiceHost" project and add a reference to System.ServiceModel assembly and HelloService project.

Step 5: Right click on WindowsServiceHost project in solution explorer, and add the Application Configuration file. This should add App.config file. Copy and paste the following configuration in App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="mexBehavior"
                     name="HelloService.HelloService">
                <endpoint address="HelloService" binding="netTcpBinding"
                    contract="HelloService.IHelloService" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8080" />
                        <add baseAddress="net.tcp://localhost:8090" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>

</configuration>

Step 6: Right click on "HelloWindowsService.cs" file and select "View Code" option from the context menu.

Step 7: Copy and paste the following code in HelloWindowsService.cs file.
using System.ServiceModel;
using System.ServiceProcess;

namespace WindowsServiceHost
{
    public partial class HelloWindowsService : ServiceBase
    {
        ServiceHost host;
        public HelloWindowsService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            host = new ServiceHost(typeof(HelloService.HelloService));
            host.Open();
        }

        protected override void OnStop()
        {
            host.Close();
        }
    }

}

Step 8: Double click on "HelloWindowsService.cs" file under "WindowsServiceHost" project. Right click on the Design surface of "HelloWindowsService.cs" file and select "Add Installer" option from the context menu. This should add "ProjectInstaller.cs" file.

Step 9: Right click on "serviceInstaller1" on "ProjectInstaller.cs" and select "Properties" from the context menu. Set
Name = HelloWindowsService
StartType = Automatic

Step 10: Right click on "serviceProcessInstaller1" on "ProjectInstaller.cs" and select "Properties" from the context menu. Set
Account = Network Service

Step 11: Build the solution

Step 12: Let's install the HelloWindowsService now. Open Visual Studio Command Prompt as an administrator.

Step 13: Type the following command and press enter
installutil -i C:\HelloService\WindowsServiceHost\bin\Debug\WindowsServiceHost.exe

Step 14: At this point we should have "HelloWindowsService" installed. Now open run window by pressing "Windows + R" keys. Type "services.msc" and press enter. In the "Services" window, right click on "HelloWindowsService" and select "Start" from the context menu. This should start the windows service.

Step 15: Open "WindowsClient" project. Right click on "HelloService" under "Service References" folder and select "Update Service Reference" option. Run the client application and test it.

wcf tutorial

7 comments:

  1. Exception occurred while initializing the installation:
    System.IO.FileNotFoundException: Could not load file or assembly 'file:///E:\VB'
    or one of its dependencies. The system cannot find the file specified..

    please me related this problem. Ijaz Ali Shah

    ReplyDelete
  2. I'm also getting same error any one find out any soluation

    ReplyDelete
  3. It probably did not install because your path has spaces in it. Simply put "" marks around your string like this.
    installutil -i "C:\HelloService\WindowsServiceHost\bin\Debug\WindowsServiceHost.exe"

    ReplyDelete
  4. HelloWindowsService installed correctly. When I start service it starts and immediately stops. Any Idea?

    ReplyDelete
  5. Sir, While i start the service in services it is showing one dialog box

    The HelloWindowsService service on local computer started and then stopped. Some services stops automatically if they are not in use by other services or programs.

    ReplyDelete
  6. system.badimageformat exception

    ReplyDelete
  7. How it is possible to access wfc from another machine in the network

    ReplyDelete

It would be great if you can help share these free resources