Windows Communication Foundation (WCF)

.NET, C#, Windows Communication Foundation Add comments

Windows Communication Foundation (WCF) (code-named “Indigo”) is a set of .NET technologies for building and running connected systems. It is a new breed of communications infrastructure built around the Web services architecture. Windows Communication Foundation, or just WCF, is a programming framework used to build applications that inter-communicate. Here is one sample which demonstrates WCF.

First create a blank solution in Visual Studio .NET 2005. I’ll call it “WSFSample”.

Then add New Project to your solution, language C#, type of project Class Library.
Let’s call this project “ServiceLibrary”. When you do this, Visual Studio cretes new file in your project. By default it is named: Class1.cs. Let’s rename it to MathService.cs. If you renamed the file name, you can see that the class name was renamed too. Add a reference to System.ServiceModel. After that let’s put some code to our class MathService. This is how our class looks like when you put few lines of code there.

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

namespace ServiceLibrary
{
    [ServiceContract]
    public class MathService
    {
        [OperationContract]
        public Int32 AddFunction(Int32 a, Int32 b)
        {
            return a + b;
        }

        [OperationContract]
        public Int32 SquareFunction(Int32 a)
        {
            return a * a;
        }
    }
}

Now we will create Service Hosts. Add new project to your solution. Right click on solution and then select: Solution -> Add New Web Site->WCF Service.
Select WCF service template. For language select C#. Take care where is location of your WCF service because by default it is in your Document and Settings folder. I prefer to hold all project files in the root of my solution file.
I’ll call my WCF service project: “WebServiceHost”.


If you take a look at your new project, you will find a file named “Service.svc”.




add this line to file:

<% @ServiceHost Language=VB Service="ServiceLibrary.MathService" %>

As you can see there is a file named “Service.cs” under App_Code folder. Because we will not use it, you can delete it. Now, please rebuid your application and set WCF service to be Startup Project. Before we test it we need to ad some end points to Web.config and we need to add refernce to the project “ServiceLibrary”. Add this lines to Web.config




  
    
      
        
          
        
      
    
    
      
        
      
    
  

  
    
  


and add Reference in your wcf service project to “ServiceLibrary” project and press “F5″ please. You must have something like this on your screen.



Now we can create some client to consume this web service. Once again we add new project to our solution. This time it will be Windows application, language C# off course. I’ll call it “WindowsClient”.We will add 3 text boxes and one button control to our windows form. As you can imagine 2 text boxes will be for integers “a” and “b” and third text box will be result. Also we need to add “Service Reference” (not web reference as we did with *.asmx) to our wcf service. To do this right click on Windows project we’ve just created and click on “Add Service Reference”.But before that we must know URL of our WCF. We can just start with F5 our application and copy paste address from address bar of Service.svc. In my case it is: http://localhost:1157/WebServiceHost/Service.svc and this address I’ll put for “Service Reference”. Now click to add Service Reference, after you enter Service URI: http://localhost:1157/WebServiceHost/Service.svc
, your service reference name is “localhost” by default. I’ll rename it to “WCFMathService”. If you want your application to be ready for test you must check your app.config file. But Visual Studio set up all for your. Just for remember, you must specify end points element in your App.config too. Here is how it looks like:



    
        
            
                
                    
                    

                        
                    
                
            
        
        
            
        
    

.
Also you must add “System.ServiceModel” reference into your Windows client application.

Here is the code of our form which will call WCF Service when we press button on the form.
Just before I show tou you my code to say how I called controls on form:
button is called: bntCallWebService

2 text boxes txtA and txtB

The third textbox is called” txtResult

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void bntCallWebService_Click(object sender, EventArgs e)
        {
            using( WCFMathService.MathServiceClient ws = new WCFMathService.MathServiceClient())
            {
                txtResult.Text = ws.AddFunction(Convert.ToInt32(txtA.Text), Convert.ToInt32(txtB.Text)).ToString();
            }
        }
    }
}


Before I finish I want to tell you to pay attention to this class: WCFMathService


And before we test this we must be carefull because our application will not work if WCF is not started. One way to do this is following: you must specify multiple startup projects: like this:



Just for case rebuild your solution and press “F5″.
Here is what I got!




heappy programming :)

One Response to “Windows Communication Foundation (WCF)”

  1. WebProgBlog » Blog Archive » Testing your web service with SOUP UI Says:

    [...] You only need WSDL and SoapUI. Foe this demonstration we’ll use this service created in Windows Communication Foundation (WCF). If you start your Service you, you will see something like this in your web browser. In my case [...]

Leave a Reply

You must be logged in to post a comment.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in