Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Creating a WCF Self-Hosted Service

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Tue, Mar 12, 2013, 3:30 PM


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.

<configuration>
  <system.serviceModel>
    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="behavior0">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <!-- 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" 
                  contract="Microsoft.Samples.GettingStarted.ICalculator" 
                  />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/ServiceModelSamples/CalculatorService" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel> 
</configuration>

7. Add the following code to the Main method of Program.cs
public static void Main()
{
    /* TODO: Replace Microsoft.Samples.GettingStarted.CalculatorService with your service class */
    using (var serviceHost = new ServiceHost(typeof(Microsoft.Samples.GettingStarted.CalculatorService)))
    {
        serviceHost.Open();

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

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