DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Hosting WCF Services In A Windows Service

Hosting WCF Services In A Windows Service

Gunnar Peipman user avatar by
Gunnar Peipman
·
Aug. 24, 12 · Interview
Like (0)
Save
Tweet
Share
9.09K Views

Join the DZone community and get the full member experience.

Join For Free

When building Windows services we often need a way to communicate with them. The natural way to communicate to service is to send signals to it. But this is very limited communication. Usually we need more powerful communication mechanisms with services. In this posting I will show you how to use service-hosted WCF web service to communicate with Windows service.

Create Windows service

VS2010: Create Windows service

Suppose you have Windows service created and service class is named as MyWindowsService. This is new service and all we have is default code that Visual Studio generates.

Create WCF service

Add reference to System.ServiceModel assembly to Windows service project and add new interface called IMyService. This interface defines our service contracts.

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    string SayHello(int value);
}

We keep this service simple so it is easy for you to follow the code. Now let’s add service implementation:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class MyService : IMyService
{
   public string SayHello(int value)
    {
        return string.Format("Hello, : {0}", value);
    }
}

With ServiceBehavior attribute we say that we need only one instance of WCF service to serve all requests. Usually this is more than enough for us.

Hosting WCF service in Windows Service

Now it’s time to host our WCF service and make it available in Windows service. Here is the code in my Windows service:

public partial class MyWindowsService : ServiceBase
{
    private ServiceHost _host;
    private MyService _server;
 
    public MyWindowsService()
    {
        InitializeComponent();
    }
 
    protected override void OnStart(string[] args)
    {
       _server = new MyService();
        _host = new ServiceHost(_server);
        _host.Open();
   }
 
    protected override void OnStop()
    {
       _host.Close();     }
}

Our Windows service now hosts our WCF service. WCF service will be available when Windows service is started and it is taken down when Windows service stops.

Configuring WCF service

To make WCF service usable we need to configure it. Add app.config file to your Windows service project and paste the following XML there:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  <services>
    <service name="MyWindowsService.MyService" behaviorConfiguration="def">
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8732/MyService/"/>
        </baseAddresses>
      </host>
      <endpoint address="" binding="wsHttpBinding" contract="MyWindowsService.IMyService">
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="def">
        <serviceMetadata httpGetEnabled="True"/>
        <serviceDebug includeExceptionDetailInFaults="True"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

Now you are ready to test your service. Install Windows service and start it. Open your browser and open the following address: http://localhost:8732/MyService/ You should see your WCF service page now.

Conclusion

WCF is not only web applications fun. You can use WCF also as self-hosted service. Windows services that lack good communication possibilities can be saved by using WCF self-hosted service as it is the best way to talk to service. We can also revert the context and say that Windows service is good host for our WCF service.

Windows Communication Foundation Web Service

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Observability Is Redefining Developer Roles
  • Top Three Docker Alternatives To Consider
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • How to Secure Your CI/CD Pipeline

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: