Generating WCF Proxy using SvcUtil.exe

7 08 2008

Visual Studio 2008 has made our WCF life a lot easier with the ability to self-host the services and also allow debugging in our service.

What if we were pushed to a situation wherein we need to use the tool svcutil.exe to create our proxy for the client…hmm… 🙂

To do so, we need to have our service running. Once the service is up and running, we could generate the proxy by the following command (replace the tags correspondingly) :

   1: svcutil /t:code http://<service_url> 
   2:       /out:<file_name>.cs /config:<file_name>.config

Here is my example :

   1: C:\Users\Chaks\Documents\Visual Studio 2008\Projects\MyService\MyServiceHost>
   2: svcutil /t:code http://localhost:8731/Design_Time_Addresses/MyService/WcfService/ 
   3: /out:MyServiceProxy.cs /config:MyServiceProxy.config
   4: Microsoft (R) Service Model Metadata Tool
   5: [Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.648]
   6: Copyright (c) Microsoft Corporation.  All rights reserved.
   7:  
   8: Attempting to download metadata from 'http://localhost:8731/Design_Time_Addresse
   9: s/MyService/WcfService/' using WS-Metadata Exchange or DISCO.
  10: Generating files...
  11: C:\Users\Chaks\Documents\Visual Studio 2008\Projects\MyService\MyServiceHost\MyS
  12: erviceProxy.cs
  13: C:\Users\Chaks\Documents\Visual Studio 2008\Projects\MyService\MyServiceHost\MyS
  14: erviceProxy.config

Now we can add this .cs file to the Client project and copy the .config file contents to the Client’s app.config file and execute the client 8)

We can also instruct svcutil to generate the proxy in the preferred language we want :

   1: svcutil /t:code /language=VB 
   2:      http://localhost:8731/Design_Time_Addresses/MyService/WcfService/ 
   3:          /out:MyServiceProxy.vb /config:MyServiceProxy.config

But svcutil already identifies the language with the extension of our output file name 8)





Silverlight and WCF

31 05 2008

With the release of Silverlight 2 beta 1, now developers can use .Net to create Silverlight applications To be more precise, developers can use XAML and VB.NET or C# as the back-end code language. To know more about Silverlight and what tools you need to start developing Silverlight applications, visit my post here.

This post focuses on how to make your Silverlight applications interact with Windows Communication Foundation(WCF) services. Visual Studio 2008 introduced several new WCF features. We will create a small calculator service which is going to add two numbers and return the result to the user.

First, let us create a Silverlight Web Application project.

silverlight-project

Visual Studio creates the necessary files needed for your solution. Let us straight away start with adding our WCF service. Visual Studio creates two projects, one being the actual Silverlight control project and the other being the web application project where the Silverlight control is hosted. So, we would be able to add a WCF service to our web application project and then reference the service in our Silverlight control project. Right click the web application project and add a WCF service to the project.

add-new-item

Name the service as CalculatorService. Visual Studio adds the necessary files such as the SVC and code-behind files and also the service configuration to the web.config file.

wcf-service-added

I have modified the service to hold only function as shown below:

[ServiceContract]
public interface ICalculatorService
{
    [OperationContract]
    int AddTwoNumbers(int num1,int num2);
}

And here is the implementation:

#region ICalculatorService Members

public int AddTwoNumbers(int num1, int num2)
{
    return (num1 + num2);
}

#endregion

Nothing complex, just simple addition and return the result 😉

Now comes the most important thing, at least for this beta 1 release – Silverlight supports only basicHttpBinding and thus our WCF service can only use basicHttpBinding if we are to interact with a Silverlight client. Let us check our CalcultorService’s binding in the web.config file under the system.servicemodel section.

default-binding

Yes, you are right. WCF by default uses wsHttpBinding and thus we need to change the binding to basicHttpBinding. Below is the modified version:

modified-binding

How do we use this service in our Silverlight application? Below is a screenshot of our Silverlight application.

screenshot-control

The interface is pretty simple accepting two numbers from the user and when Add is clicked, our service’s AddTwoNumbers is invoked to get the result.

How do we reference our WCF service now? We do the same way as we do in normal WPF or WinForms applications 😀

Right click on the Reference and select Add Service Reference

silverlight-add-service-reference

We will be presented with a dialog box to add a service reference.

service-discover

We can use the Discover option to discover any WCF services in our solution. As our CalculatorService is hosted in the same solution, the Discover would be able to find our service.

calculator-service-discovered

Let us give a name to our service reference – CalculatorServiceProxy 🙂

Once we add our service, we can see that the Silverlight project now has the necessary files to interact with our WCF service.

silverlight-added-wcf-reference-files

Now we are ready to add our code to the button Click event handler. Below is the code snippet.

private void Button_Click(object sender, RoutedEventArgs e)
{
    int num1 = Convert.ToInt32(TxtNum1.Text);
    int num2 = Convert.ToInt32(TxtNum2.Text);

    CalculatorServiceClient calculator_proxy =
        new CalculatorServiceClient();

    calculator_proxy.AddTwoNumbersCompleted +=
        new EventHandler<AddTwoNumbersCompletedEventArgs>
            (calculator_proxy_AddTwoNumbersCompleted);

    calculator_proxy.AddTwoNumbersAsync(num1, num2);

}

void calculator_proxy_AddTwoNumbersCompleted(object sender,
    AddTwoNumbersCompletedEventArgs e)
{
    HtmlPage.Window.Alert(Convert.ToString(e.Result));
}

You guessed it right – WCF reference adds the asynchronous operations to our Silverlight project and it does makes more sense to use asynchronous operations in a Silverlight environment as we cant block the browser and make the user wait.

Let us build and execute our project 🙂

And here is the screenshot:

output

That’s it! Now we have a Silverlight application interacting with a WCF service 😉

Wasn’t it easy 8)

The sample can be downloaded here:





WCF debugging in Visual Studio 2008

6 11 2007

vs08_v_rgb_web.jpg

This is one of the great pain relief if you ever wondered how would you debug WCF Service using Visual Studio 😉

Visual Studio 2008 allows you to debug WCF Services by exposing the service in the local ASP.NET development server!

Here is an excellent screencast on Debugging WCF using VS 2008 by Darryl

As I had told in my earlier post, that I would be moving my WCF Service from VS 2005 to VS 2008 and since I use VS 2008 now, am able to debug my service locally as well as hosting it via my local IIS 7. I have moved my debugging to IIS 7 from local ASP.NET development server.

Below is screenshot of everything in place 😀

debugging-wcf.png

VS 2008 rocks ! Cant wait for the final release as Visual Studio 2008 Get it’s date 😉