How to self-host WCF

J.D. Meier, Jason Taylor, Prashant Bansode, Carlos Farre, Madhu Sundararajan, Steve Gregersen.
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 in your config file.
<service 
    name="Service1"
    behaviorConfiguration="ServiceBehavior">
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost:8000/WCFSecuritySamples/service"/>
    </baseAddresses>
  </host>
  ...
</service>

Additional Resources

Last edited May 8, 2008 at 3:50 AM by prashantbansode, version 1

Comments

No comments yet.