Proxy > Gmail Facebook Yahoo!

Hosting and Running a Basic WCF Service




The code for hosting and running a WCF service enables to run the service that can be consumed by the WCF client application. This third step involves a sequence of programming commands which enable to run a basic Windows Communication Foundation service. It consists of the following steps to self-host and run a service:
1. creating a base address for the service.
2. creating an instance for self-hosting the service.
3. enabling the metadata behavior
4. opening the service host                     
In this tutorial we are continuing extending the console application created in the previous tutorial: Implementing Service Contract in WCF.

Self-Hosting and Running a WCF Service

1. Specify a base address for the WCF service using Uri class instance. The following Uri will open the port number 5000 to expose the service instance:
Uri serviceBaseAddress = new Uri("http://localhost:5000/WCFService/Sample");
2. Next create the ServiceHost instance for self-hosting the WCF service over the specified base address:
ServiceHost selfServiceHost = new ServiceHost(typeof(AreaService), serviceBaseAddress);
3. Then add a service endpoint that enables to expose the service:
selfServiceHost.AddServiceEndpoint(typeof(IArea), new WSDualHttpBinding(), "AreaService");
4. To enable the metadata information exchange functionality of a WCF service, you must import the System.ServiceModel.Descriptionnamespace at the top of the program.cs file:
using System.ServiceModel.Description;
5. You can use the following C# code inside the Main method of program.cs file to run the basic WCF service.
static void Main(string[] args)
{
    // creating base address for self-hosting the WCF service
    Uri serviceBaseAddress = new Uri("http://localhost:5000/WCFService/Sample");
    // creating an instance for self-hosting
    ServiceHost selfServiceHost = new ServiceHost(typeof(AreaService), serviceBaseAddress);
    try
    {
        // adding a service endpoint
        selfServiceHost.AddServiceEndpoint(typeof(IArea), new WSDualHttpBinding(), "AreaService");
        // enabling metadata behavior of service
        ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();
        serviceMetadataBehavior.HttpGetEnabled = true;
        selfServiceHost.Description.Behaviors.Add(serviceMetadataBehavior);

        // starting the WCF service instance
        selfServiceHost.Open();
        Console.WriteLine("The service is running now.");
        Console.WriteLine("Press any key to stop the service...");
        Console.WriteLine();
        Console.ReadLine();
        // stopping the WCF service instance
        selfServiceHost.Close();
    }
    catch(CommunicationException ex)
    {
        // abort the service in case any error occurs
        Console.WriteLine("An exception occurred: {0}", ex.Message);
        selfServiceHost.Abort();
    }
}
6. Press F5 to compile and run the service and try to browse the base address URL in the Internet Explorer. If it shows the web service information without any error, it means you have created a service successfully.
Continue to next tutorial: Creating a WCF Client using Service Model Metadata Utility Tool.


Responses

0 Respones to "Hosting and Running a Basic WCF Service"


Send mail to your Friends.  

Expert Feed

 
Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar