Remote method invocation -Example with WCF

Windows Communication Foundation (WCF) is a framework for building service-oriented applications in .NET. It supports various communication protocols and various ways for clients to access the services, with or without using proxies.

The following example (the StockMarket example) is realised in 2 variants, using 2 different communication protocols: NetTcpBinding and WebHttpBinding.

clients can access the services in different ways, with or without using proxies.

For clients that acces the remote service by method invocation on a proxy object, we can have 2 different kinds of proxies:

  1. transparent proxy, dynamically generated at runtime. The application derveloper does not need to do anything.
  2. proxy generated by the application developer using the svcutil tool

The StockMarket example

The application developer must provide:

StockMarket with NetTcpBinding

Client uses transparent proxy generated at runtime

If the client part knows the service interface, then a transparent proxy can be generated dynamically at runtime.

The application developer must write the following code:

Compiling and deploying:

At the server site: you need the following source files: IStockMarket.cs, StockMarket.cs, ServerProgram1.cs. In a Visualstudio Developer Command Prompt compile all code with the command csc *.cs and then start ServerProgram1. You can see the sequence of operations in the figure below.

At the client site: you need the following source files: IStockMarket.cs, Client1.cs. In a Visualstudio Developer Command Prompt compile all code with csc *.cs and then start Client1. You can see the sequence of operations in the figure below.

After running twice the client, the server console will show the following output:

in the figure above we see that the StockMarket object is instantiated only once in the beginning, then the servicehost is started. All method invocations are done on the same StockMarket server object. This is because in the StockMarket.cs implementation the class is annotated with [ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]

If the annotation from class StockMarket is removed the StockMarket object will be instantiated for every session. After running the client twice, the server console shows:

Client uses proxy generated with svcutil

The client can also use a proxy generated using the svcutil tool. A use case where this scenario is useful is the following one: the client discovers an existing third party service, but has not the service interface. This service interface and a proxy can be generated from the metadata describing the service. It is possible that a deployed service also publishes its metadata. With help of the svcutil tool the published metadata are used to generate the code needed by the client.

Step 1. The code written by the application developer:

Step 2. On the server site, build and start ServerProgram2

Step 3. On the client site, you need only Client2.cs. You have no IStockMarket.cs at the client site now!
You must run svcutil to generate code:
svcutil /language:C# net.tcp://localhost:8000/mexserver
This generates file StockMarket.cs. This contains a generated IStockMarket interface and the classStockMarketClient which is a proxy for accessing the remote Stockmarket.

Step 4. Now you can build and start Client2

StockMarket with WebHttpBinding

We can change the type of protocol to HTTP instead of TCP. The StockMarket will be accessed as a web service, in 2 variants: by a client program with a dynamic generated proxy, and by using the web browser as a client.

In order to make the operations accessible via web browser, the service interface changes to IStockMarket3.cs.

The server program changes to ServerProgram3.cs.

Client uses dynamic generated transparent proxy

The Client program is now Client3.cs

No client program, client is in web browser

The usage and result can be seen in the figure below.