Tag Archives: Visual Studio 2008

How to generate WCF Client Proxy Class using svcutil.exe

I wanted to use wcf because I read about it and like the power and clean implementation which is highly configurable without a lot of effort. I have used and made many clients for web services using Visual Studio .Net and all it requires is to know where the web service is hosted. When you add a service reference and pass in a url where it is hosted (WSDL) the IDE generates a proxy class and WOW !! there you go… Call the methods in ur client, no fuss at all clean , nice and smooth. I was under the impression that WCF is esentially the same and support many other types of bindings like http,tcp blah blah ….. So i started developing the Kool WCF server client application. Little did I know about generating the client proxy class using svcutil.exe . I knew that you can generate proxy classes using svcutil.exe but that is a painful painful process. A little up and down and it gives you exceptions that are very easy to understand [ Devilish smile ] . So to save you the time and effort that you will have to put it to generate proxy classes I will show you the exact app.config file and how to use the svcutil. You can simply copy and paste it and be happy [just rename the Contracts , end point info according to your own and you are good to go] …

I have changed the font to BOLD whatever i was missing and was required by svcutil to generate the proxy class that I used.
Here is my app.config in the Hosting Application (server) from where the service would be run:

<?xml version=1.0 encoding=utf-8 ?>

<configuration>

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior name=mex>

          <serviceDebug includeExceptionDetailInFaults=true />

          <serviceMetadata />

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <services>

      <service name=WCFServiceLibrary1.service1 behaviorConfiguration =mex>

        <endpoint address=net.tcp://localhost:6587/Service1/

                  binding=netTcpBinding

                  bindingConfiguration=TestBinding

                  name=RoleEndPoint

                  contract=WCFServiceLibrary1.IService1 >

          <identity>

            <dns value=localhost />

          </identity>

        </endpoint>

        <endpoint

             address=mex

             binding=mexTcpBinding

             name=MEX

             contract=IMetadataExchange />

        <host>

          <baseAddresses>

            <add baseAddress=net.tcp://localhost:6587/Service1/ />

          </baseAddresses>

        </host>

      </service>

    </services> 

    <bindings>

      <netTcpBinding>

        <binding name=TestBinding maxBufferPoolSize=524288 maxReceivedMessageSize=65536 portSharingEnabled=false>

          <readerQuotas maxDepth=32 maxStringContentLength=8192 maxArrayLength=16384 maxBytesPerRead=4096 maxNameTableCharCount=16384 />

          <security mode=None />

        </binding>

      </netTcpBinding>

    </bindings> 

  </system.serviceModel>

</configuration>

Observe that there is an endpoint defined for metadata exchange called mex. This is used by the svcutil to generate the proxy class. The behavior node also contains a behavior named as mex. Now you are all set to use the svcutil.exe. Simply open the Visual Studio Command prompt from your startup menu à Visual Studio à tools and Command Prompt and type in the following command:

(Remember to replace the name to the service with yours as defined in the server configuration file)
Hope it helps and save you time and effort and I wish you don’t have to go through the pain of getting svcutil to work. Happy Coding !!

C:\Program Files\Microsoft Visual Studio 8\VC>svcutil.exe  net.tcp://localhost:6587/Service1/mex

This will generate two files service1.cs (proxy class for the client) and output.config (the app.config for the client). Enjoy !!

 

 

 

 

 

 

Advertisements

4 Comments

Filed under 1

Using Windows Communication Foundation WCF with Visual Studio 2008:

There is a lot of hype about new (not so new now) technologies from Microsoft and the new architectures that are now a part of Microsoft .Net development environment. These include Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF) and Workflow Foundation (WF).

I will give a brief introduction about what these are just in case you are not familiar. Anyways if you are then probably this article is no good J since I just started working with these. This will help you get a quick start in case you are a starter and I have gathered the info skimming through tutorials and I want to give you something quick and easy. So to start off “What is WPF?” WPF exclusively has everything to do with the UI or user interfaces. Till .Net 2005 the Graphics were GDI (Graphics device Interface) based and in WPF they are Direct X based thus utilizing the faster video card acceleration and fast memory access as compared to the GDI. Secondly this used to be an issue when clients wanted fancy UI e.g. Elliptical buttons, Window Layouts, Animated menus and colors in controls. Microsoft made it possible with user controls to some extent but off course the mainstream developers didn’t want to get in that mess and always wanted to use the default controls with some styling that the client wont like. Customers these days doesn’t want “just another windows button” there should be something innovative about it so WPF enables you to define your own controls with XAML pronounced as (zammel).

The styles, colors, shapes, data bindings and everything that is a part of the control can be done using XAML which is simple xml just defined somewhat like html ( I always see the elements and attributes notations) whenever I see XAML.

So WPF is for UIs and probably an effective solution for at least Windows Vista if not XP. Just as with the “User Interfaces” Microsoft realized that “Services Oriented Architectures” are gaining in popularity. With Central server centric applications and Microsoft efforts to support the SOA with applications like “BizTalk”, “SharePoint” e.t.c something also needed to be done to provide a standard platform for network programming using .Net. Microsoft came with WCF which is a powerful framework for writing and utilizing or consuming web services , windows services , .Net Remoting without needing to write a whole lot of socket interfaces. The implementation is hidden in the components constituting the WCF. WCF like WPF depends heavily on configurations and it is usually a good idea to separate the business logic from the configurations or the (app.config) file. In this article I will demonstrate writing a simple client server program using WCF which will give you some bare essentials for WCF and will help you to explore more in the ever growing world of emerging technologies.

By the way just to get you started if you are not familiar with the client server paradigm, client server application that I am going to develop would be basically two applications. A Server application will let many clients connect to it and say “Hi”. Keeping in mind this is not to tell you how to design better client server applications this is all about getting you up and running with WCF. Ok my friends so don’t get angry =] 

Designing the Client and Server with WCF:

Primarily while designing the application remembers there should be something common in the client and server. To explain this better ask yourself the question “How is client going to interact with the Server”? There should be some interface the server should provide to achieve this. If you are familiar with web services think of wsdl (web services description language) that exposes the web service API. So just like that the WCF Service also exposes interfaces. The clients can generate the proxy classes for the API and will call those API methods to communicate with the Server with underlying implementation of Sockets and communication mechanism). I will discuss “How to generate the proxy classes” but first things first “Decide what should the Server be like”? And “what functionality you want to expose”?

So just for the sake of simplicity just think that what the server wants to expose and client wants to consume is 1 single functionality called send message.

 

The First Step in Writing the Server: 

I consider adding the “system.servicemodel” reference a prerequisite to every WCF application so if you have not already added that do make sure the reference is included in the list of your references.

1/ Add a new interface to your solution. I named the solution as WCFServer so the namespace here is WCFServer. I named the interface as CommonInterface just to be clear what this does. This would be the API exposed for the client and would be executed on the server. Good naming convention for this would be to prefix the name of the class with an “I” so something like ICommonInterface but that’s something you know right.

namespace WCFServer

{

    [ServiceContract]

    public interface CommonInterface

    {

        [OperationContract]

        void SendMessage(string msg);

 

    }

}

Observe that there are two things here which might be new to you. The interface is marked as “ServiceContract” which is the attribute which specifies that this is the contract between the client and server.The other thing is the method is marked as “OperationContract”. For the time being just think that every method in this interface should be marked as an “OperationContract”. This method is what will be executed on the server and what the client will call from the “Client code”. 

Second Step is to implement the Interface with your server class:

Give a meaning to the interface by implementing it in your server side code.How to do that is declare a class in your program and name it appropriately and inherit it from this interface that we defined in step 1. For our sample application I defined the server class as shown in the definition below:

 

namespace WCFServer

{

    class Server : CommonInterface

    {

        public void SendMessage(string msg)

        {

            Console.WriteLine(“Message from client –> “ + msg);

        }

 

        static void Main(string[] args)

        {

            Console.WriteLine(“Starting the server …”);

            ServiceHost host = new ServiceHost(typeof(Server));           

            host.Open(); 

            Console.WriteLine(“Server started successfully at…Pressing a key would cause the server to shut down …”);

            Console.ReadLine();

            host.Close();

        }

    }

}

 

So if you look at the code closely you will find the implementation of the “SendMessage” function that will be called by the client and a message would be passed and displayed on the Server’s console window.

If you look at the Main function the first thing that we do is to declare a Service Host and specify the sever’s type. Then use the Open method of the service host to start the service.

Our server at this point is up and running and waiting for the clients to say “Hi”. How to write the clients will be discussed in the next article. Hope this helps getting you a quick start of essentially what it is and if you have any ideas, observations I would love to hear from you in the form of comments.Stay tuned for more and thanks for reading.Happy Coding !!

Here are the Configurations necessary for the configurations: 

<?xml version=1.0 encoding=utf-8?>

<configuration>

  <system.serviceModel>

    <services>

      <service name=WCFServer.Server>

        <endpoint address=net.tcp://localhost:5555/Server binding=netTcpBinding

            bindingConfiguration=“” contract=WCFServer.CommonInterface />

      </service>

    </services>

  </system.serviceModel>

</configuration>

 You can use the “svcconfigeditor.exe” to generate this so you don’t have to memorize this simply go through a wizard and save the file as app.config and copy and paste.The svccinfigeditor can be found at “C:\Program Files\Microsoft Visual Studio 8\Common7\IDE

 

1 Comment

Filed under C#