How to Self-Host WCF

- J.D. Meier, Carlos Farre, Jason Taylor, Prashant Bansode, Steve Gregersen, Madhu Sundararajan, Rob Boucher

Self-hosting is best suited for development and debugging scenarios in which you want maximum flexibility and you want to get the service running as quickly as possible. When readying for deployment you should choose between hosting in a Windows service or in IIS.

Use the following methods to self-host your WCF service in any .NET application:
  1. Create a method to start the service, as shown in the following code example
// Host the service within the application.
public static void Main()
{
    // Create a ServiceHost for the CalculatorService type.
    using (ServiceHost serviceHost = 
           new ServiceHost(typeof(Service1)))
    {
        // Open the ServiceHost to create listeners         
        // and start listening for messages.
        serviceHost.Open();

        Console.ReadLine();
    }
}
  1. In the self-hosted case, you must specify the base address. This example shows how to configure the config file.
<service 
    name="Service1"
    behaviorConfiguration="ServiceBehavior">
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost:8000/WCFSecuritySamples/service"/>
    </baseAddresses>
  </host>
  ...
</service>

Additional Resources

Last edited Jun 13, 2008 at 7:02 PM by prashantbansode, version 1

Comments

No comments yet.