Windows Communication Foundation -Simple Program

Based on my previous WCF post I received a trackback to give an example that should be really simple to start WCF with.

There are two ways to present this. In this example I’ll use as little code as possible in as few locations as possible. The other way is using the WCF Service Library project that comes with the VS2005 WCF Extension but requires a lot more code, text, etc.

I’m using two projects, because WCF is all about getting clients to communicate with a service. Let’s start.

1. Open Visual Studio 2005 and create a new console application project, called “CalcServiceHost”.

2. Add a reference to the project for the System.ServiceModel assembly. It’s the core assembly used by WCF.

3. Add a using statement at the top of your class for System.ServiceModel.
using System.ServiceModel;

4. Create a service contract and implement it in the Program.cs file, directly under your Program class. In this example I’m expecting a two numbers and adding this and return the output.

[ServiceContract]
class CalcService
{
  [OperationContract]
  int Sum(int first,int second)
  {
    return first+second;
  }
}
5. In your Program class, inside the static Main method we need code to host our service. We’ll add two endpoints, one for our service and one for extracting the metadata.
   

 1 static void Main(string[] args)
    2 {
    3   // We did not separate contract from implementation.
    4   // Therefor service and contract are the same in this example.
   

5   Type serviceType = typeof(CalcService);
    6
    7   ServiceHost host = new ServiceHost(serviceType, new Uri[] { new Uri(“http://localhost:8080/“) } );
    8
   

 9   // Add behavior for our MEX endpoint
   10   ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
   11   behavior.HttpGetEnabled = true;
   12   host.Description.Behaviors.Add(behavior);
   13
  

 14   // Create basicHttpBinding endpoint at http://localhost:8080/CalcService/
   15   host.AddServiceEndpoint(serviceType, new BasicHttpBinding(), ” CalcService “);
  

 16   // Add MEX endpoint at http://localhost:8080/MEX/
   17   host.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), “MEX”);
   18
   19   host.Open();
   20
   21   Console.WriteLine(“Service is open, press any key to close.”);
   22   Console.ReadKey();
   23 }

At line 7 we’re instantiating our ServiceHost object, specifying a base address at localhost on port 8080.

At line 15 we’re adding our endpoint, using basicHttp, for our service.

At line 17 we’re adding our metadata endpoint. For this we also need to specifiy the MetadataExchange behavior, which we do at line 10, adding it to our ServiceHost at line

12. At line 11 we’re enabling HttpGet, which means we can view the metadata using a browser.

At line 19 we just open the host and everything should work fine. Lines 21 and 22 are waiting on confirmation of a user to close the console application, automatically closing our ServiceHost and thus our service.

We only need to ad a using statement for the System.ServiceModel.Description because adding a MEX endpoint using code requires this namespace.
using System.ServiceModel.Description;

6. Now this is done, let’s try and run our service. If all goes well, we’ll see the message that the service is ready. You can check it by opening up a browser and visiting http://localhost:8080/

7. Now we need to create our client. Add a new Windows Forms project called “CalcServiceClient” to our solution.

8. Right-click the “Host” project and choose “Debug” and then “Start New Instance”. Then right-click the “CalcServiceClient” project and select “Add Service Reference”. In the dialog that appears type in the address http://localhost:8080/ and press “OK”. Now you should have a new folder in your “CalcServiceClient” project with the reference to your service. Stop the service that’s still running in the background.
The System.ServiceModel assembly should be referenced now, the app.config has been added and modified as well as a proxy class under your “localhost” service reference.

9. On your form in the “CalcServiceClient” project, add a textbox and a button. Double-click the button.

10. Add three lines to your button-click event.
   

1 private void button1_Click(object sender, EventArgs e)
    2 {
    3   localhost. CalcServiceClient proxy = new Client.localhost. CalcServiceClient();
    4   int sumResult = proxy.Sum(textBox1.Text,textBox2.Text);
    5
    6   MessageBox.Show(sumResult.ToString());
    7 }

11. Again, right-click the “CalcServiceHost” project and select to start a new instance and do the same for your Windows application. Then fill in the two numbers in the textboxes and press the button.

Advertisements

One thought on “Windows Communication Foundation -Simple Program

  1. Pingback: Windows Communication Foundation -Simple Program

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s