Jasinski Technical Wiki


Home Page
All Pages

Quick Search
Advanced Search »

Contributor Links

Create a new Page
File Management
Your Profile

Other Wiki Sections



Creating a WCF Self-Hosted Service

Modified on Fri, Mar 15, 2013, 10:26 AM by Administrator Categorized as ┬ĚNet Framework

Coding the Service

1. Within Visual Studio, create a Console Application

2. Add a reference to System.ServiceModel

3. Add a public interface to the project, naming it with a "Service" suffix, and add a using System.ServiceModel statement to the file.

4. Decorate the public interface with ServiceContract attribute and its methods with the OperationContract attribute.

5. Add a public class to the project that implements the interface, again naming it with a "Service" suffix.

6. Add an app.config file to the project, mimicking the following settings.

    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
        <behavior name="behavior0">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
      <!-- TODO: The name attribute should be the fully-qualified name of the service class.  -->
      <service name="Microsoft.Samples.GettingStarted.CalculatorService" behaviorConfiguration="behavior0" >
        <endpoint address="http://localhost/ServiceModelSamples/CalculatorService" binding="basicHttpBinding" 
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <add baseAddress="http://localhost/ServiceModelSamples/CalculatorService" />

7. Add the following code to Program.cs.
using System.ServiceModel;
. . .

public static void Main()
    /* TODO: Replace Microsoft.Samples.GettingStarted.CalculatorService with your service class */
    using (var serviceHost = new ServiceHost(typeof(Microsoft.Samples.GettingStarted.CalculatorService)))

        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press <ENTER> to terminate service.");

8. Execute the service (the Console App) and test it by opening a web browser and navigating to the endpoint address.

Coding the Client

1. If the service project is part of the same Visual Studio solution as the client, then (after ensuring the solution builds successfully) remove the service project from the solution and save the solution. Then, in a second Visual Studio session, re-open the service project and run it.

2. In the client project, within Solution Explorer right click References > Add Service Reference.

3. For the address of the service reference to add, specify the endpoint address from the service's app.config file and specify a namespace with a "ServiceReference" suffix.

4. In the client's configuration file (app.config or web.config), verify the following settings, which should have been added when you added the service reference to your project.

      <!-- TODO: The address, binding, and contract attributes should match those in 
           the service's config file. -->
      <endpoint name="main" address="http://localhost/ServiceModelSamples/CalculatorService"
        binding="basicHttpBinding" contract="Microsoft.Samples.GettingStarted.ICalculator" />

5. In your client code, add a using ...ServiceReference statement.

6. To instantiate your service, simply use the new MyServiceClient syntax. (Note that the MyServiceClient class was auto-generated based on the MyService in your service.)

ScrewTurn Wiki version Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.