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, 4:05 PM


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.

<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 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)))
    {
        serviceHost.Open();

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

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 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.

4. In the client's configuration file (app.config or web.config), mimic the following settings.
<configuration>
  <system.serviceModel>
    <client>
      <!-- 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" />
    </client>
  </system.serviceModel>
</configuration>

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