WCF Simplified Part 1: Getting Started with WCF 4 and Visual Studio 2010

1. Create a WCF Service Library in Visual Studio 2010 (run as Administrator),

image

2. Visual Studio will create a template for us that includes a ServiceContract with OperationContracts, and a DataContract with DataMembers. This is enough to get started,

[ServiceContract]
public interface IService1 {
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

[DataContract]
public class CompositeType {
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue {
        get { return stringValue; }
        set { stringValue = value; }
    }
}


3. We now have a WCF service library containing  our contracts – definitions on how data will be exchanged with clients. If you build the solution using F6 and look at the bin/Debug folder you’ll see WcfServiceLibrary1.dll. We will have to host this dll in a process so that we can interact with it. Visual Studio 2008 and 2010 both include a WCF Test Client, which you can see by running the solution using F5,

image

Now click on GetData() on the left table view, enter a number in the Value field and click the Invoke button,

image

If you see this, just click OK,

image

In the respose, you’ll see “You entered: [your number]”, this shows that your WCF service library is working correctly.

4. For more flexibility, you’ll want to write your own application to host the service dll. To do this add a new Project to the solution (a console application),

image

5. In the ConsoleApplication1 add a project reference to the WcfServiceLibrary1,

image

And also a reference to the System.ServiceModel,

image

6. Your solution should look like this,

image

7. Now change the Main method to,

static void Main(string[] args) {
    using (ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.Service1), new Uri("http://localhost:8000/HelloWCF"))) {
        // Set up a service endpoint [Contract, Binding, Address]
        host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), new BasicHttpBinding(), "HelloWCF");

        // Enable metadata exchange
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior() { HttpGetEnabled = true };

        host.Description.Behaviors.Add(smb);
        host.Open();

        Console.WriteLine("Ready...");
        Console.ReadLine();
    }
}

8. Now set the ConsoleApplication1 as the StartUp Projectand run using F5. We are now actually hosting the WCF

service library at two addresses,

    1. http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/ (the default created by Visual Studio as defined in the App.config file)

    2. http://localhost:8000/HelloWCF (we created this service endpoint)

9. Let’s create a test application to interact with the WCF service, open a new instance of Visual Studio 2010, and create a console application,

image

10. In this new project, we need to add a service reference that points to the hosted WCF app we created in steps 1-8. So start the previous solution and browse to http://localhost:8000/HelloWCF to make sure the address is accessible, then add a service reference,

image

11. Now you should be able to add this code to the Main method to call the WCF service,

static void Main(string[] args) {
    // Create an endpoint
    EndpointAddress epoint = new EndpointAddress("http://localhost:8000/HelloWCF/HelloWCF");

    ServiceReference1.IService1 proxy = ChannelFactory<ServiceReference1.IService1>.CreateChannel(new BasicHttpBinding(), epoint);
    using (proxy as IDisposable) {
        // Call the WCF service using the proxy
        string str = proxy.GetData(42);
        Console.WriteLine(str);
    }
    Console.ReadLine();
}

About these ads

About soumya chattopadhyay
I live and work in Seattle, WA. I work with Microsoft technologies, and I'm especially interested in C#.

9 Responses to WCF Simplified Part 1: Getting Started with WCF 4 and Visual Studio 2010

  1. Pingback: WCF Simplified Part 2: Message Exchange Patterns (MEPs) « I.Net

  2. Pingback: WCF Simplified Series « I.Net

  3. Pingback: WCF Simplified Part 3: Using the WCF Duplex MEP « I.Net

  4. Pingback: WCF Simplified Part 4: Comparing the Request/Reply and One-Way Patterns « I.Net

  5. Pingback: WCF Simplified Part 5: Handling Exceptions and Faults « I.Net

  6. sloughin says:

    It is important to note that in the host console app, you have the endpoint defined like this:

    ServiceHost host = new ServiceHost( typeof( HelloWCF), new Uri( “http://localhost:8000/HelloWCF” ) )

    whereas in the client console app, you need to also specify the endpoint that you added to that service, which was the same as the service itself.

    EndpointAddress epoint = new EndpointAddress(“http://localhost:8000/HelloWCF/HelloWCF”);

  7. Wonderfully simple illustration of WCF. It allows you to play with the concepts and see the resulting errors.

  8. I did change the service name to “WCFService.Service1″ to avoid the confusion between the HelloWCF namespace. in the URI.

  9. Awesome blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would really make my blog jump
    out. Please let me know where you got your design.
    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: