Sunday, 10 July 2011

A Self-Hosting WCF Service Host With Client

The following WCF service is hosted in a .NET console application
Client which consumes the service is a simple WinForm application

A service can also be hosted in a managed windows service, IIS, or Windows Process Activation Service (WAS).

The following code is written in Visual Studio 2010, C#.

Create WCF Host

1. Create a Console Application, name it as SelfHost
2. Add Reference System.ServiceModel
3. The entire Program.cs is as following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace SelfHost
{
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }

    public class HelloWorldService : IHelloWorldService
    {
        public string SayHello(string name)
        {
            return string.Format("Hello, {0}", name);
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddressHello = new Uri("http://localhost:8080/hello");
           
            // create serviceHost

            ServiceHost hostHello = new ServiceHost(typeof(HelloWorldService), baseAddressHello);

            // Enable metadata publishing
            ServiceMetadataBehavior smbHello = new ServiceMetadataBehavior();
            smbHello.HttpGetEnabled = true;
            smbHello.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            hostHello.Description.Behaviors.Add(smbHello);

            hostHello.Open();
            Console.WriteLine("the hello service is ready at {0}", baseAddressHello);
                      
            Console.WriteLine("press <enter> to stop the service.");
            Console.ReadLine();

            hostHello.Close();
     
        }
    }
}

It uses default endpoints, and no configuration file is required for this service. If no endpoints are configured, the runtime creates one endpoint for each base address for each service contract implemented by the service

To test your WCF service using WCF Test Client

1. Build the project, make sure it is successful
2. Run the service (Ctrl+F5), you should see the following Screenshot

2. To open WCF Test Client, open a Visual Studio 2010 command prompt and execute WcfTestClient.exe.
3. Select Add Service... from the File menu
4. Type http://localhost:8080/hello into the address box and click OK.
5. Double-click SayHello under the My Service Projects node. Type your name into the Value column in the Request list, and click Invoke. A reply message should appear in the Response list if everything is alright

Create Client Application

1. Add new winform application to the solution, name it as WcfClient
2. Add Reference System.ServiceModel
3. Drag drop a button and double click it to create a click event handler
4. Run the service: Set the Console as startup project and run it by Ctrl+F5
5. Open Visual Studio 2010 command prompt and navigate to WcfClient folder
6. Use the command-line tool ServiceModel Metadata Utility Tool (Svcutil.exe) with the appropriate switches to create the client code

svcutil.exe /language:cs /out:Proxy.cs /config:app.config http://localhost:8080/hello

7. Include the generated file into your winform application: Proxy.cs and app.config
8. Invoke the service

Using System.ServiceModel;
.....

private void button1_Click(object sender, EventArgs e)
{
   HelloWorldServiceClient client = new HelloWorldServiceClient();
   MessageBox.Show(client.SayHello("michael"));
}


9. Click the button, you should the Message Box with the "hello, michael". Done


Reference: MSDN

No comments:

Post a Comment