C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Raising Events From Server to Clients using .NET Remoting


Posted Date: 20 Jun 2004    Resource Type: Articles    Category: WCF/Webservices

Posted By: SATHISH C.G.       Member Level: Silver
Rating:     Points: 17



Download sample project

Introduction

In this article we discuss how we can raise an event from a server to clients using .Net remoting. This is one of the advanced topics in remoting, but its cool. The good example where we will use this, is in case if we are having a server which monitors SQL server periodically and informs the clients who have subscribed for events about the changes.

How it works?

Each and every client will subscribe for some events. Events are nothing but some happening in the server side. It may be in SQL server or even in an external website. After this whenever the events happens, the server will intimate the clients who has subscribed for this event with some data. Following are some of the examples: -

1. Notify me when stock rates increases greater then 100.
2. Notify me when new customer registers.

Internals in .NET Remoting

Normally we can pass string, Int32 and other basic .Net data types to the server. But we cannot pass a Type/Class directly to a remoting server even after marking the class with the serializable attribute. If we try to pass the class object directly, the server will try to search for the class in the server bin and it will give Type not found error. Only way to achieve this is using Abstract classes. If we create abstract class in the server and if the clients pass the implemented instance of the class, then we can send the same to the server. When the server calls any of the methods in the class passed by the client, the client who has supplied the class will get the event fired. This cannot be achieved directly, we need to do some security settings in the remoting configuration. We will checkout the setting later in the article.

After this we cannot conclude only the reference is needed in the server. If that is the case we can achieve this using Interfaces also. I tried, but it doesn’t work. Now Let us jump in to the code directly. In our Example we have the following applications, which simulate the server events. They are as follows.

1. Events Server (Console Application which hosts the server)
2. Events Manager, which has the server class and the client abstract class.
3. Event Publisher (Console Application which publishes some events)
4. Clients who subscribes for events

Events Architecture

Events Manager

Class Design

All the clients will inherit the Events Manager (Must Inherit) Class and do their own specific implementation. Client Console application will pass an newly instantiated class of this to the server for subscription. Whenever the server raises an event, client will receive the event.

Event Server

Class Design

Event Server is an console application which will loads the Hoster remotable class of the events manager to start serving the clients.

Events Publisher

Class Design

Events publisher is a console application, which will contact the remoting server and raise some events. (Event Simulator)

Client

Class Design

Client is a console application, which will subscribe for some events in the server by passing a class, which inherits, from Events Manager (Abstract Class).

Configuration Files

Configuration settings plays very vital role in this, because by default remoting doesn’t allow to raise events directly from the server to clients. For this we need to turn on some configuration settings in both sever and clients.

Server Configuration File

In the server providers we need to set the typeFilterLevel attribute of formatters to FULL, other wise .net security policy will not allow the server to raise events to the clients.

Client Configuration File

Similarly in the client side we need to enable the above settings to make the clients to receive the events raised by the server. In this the most important thing is the port no.
We may wonder that, why do we need port number in the client and why we are setting it to 0. The communication between the server and client in carried through Sockets internally. That’s why we are specifying the port no. in client also. The significance of 0 in that the client is, asking the server to choose any one of the free ports for communication.

How to run the sample application ?

1. Extract the contents of the Events.zip file to a folder.
2. Open the Events.sln in the VS.Net 2003.
3. Build the solution.
4. Open 4 Command prompt windows.
5. In the first command prompt window go the EventServer\bin and execute the EventServer.exe to start the server.
6. In the second and third command prompt go to clients\bin and execute the clients.exe to start two clients which will subscribe for events.
7. In the fourth window go to EventsPublisher\bin and exeute the EventsPublisher.exe. The exe will prompt you to publish some events. Type some event data, the message will get broadcasted to all the subscribed clients.

Conclusion

It’s a cool topic and I have used in one of my projects which is live now. Till date I don’t get problems from my clients regarding this. This approach has some disadvantages because the server holds the running instance of the clients process across machine boundaries. If the server throws any exception it will abort the client process also. If we do good exception handling then we can overcome these problems. Nothing in the world comes for free of cost.

Download sample project




Responses

Author: critic    20 Jun 2004Member Level: Bronze   Points : 0
Nice article sathish !!
We want to see your photo near your name! Also, instead of your company name we all see your long address near your name !!


Author: Mary Mathew    21 Jun 2004Member Level: Bronze   Points : 0
This artcle is realy excellent!!


Author: Ashwani Gaur    25 Jun 2004Member Level: Bronze   Points : 0
Your article seems to be interesting, but i want to see it running, which couldn't be possible till you include .config files also in your downloadable sample.


Author: Tony John    25 Jun 2004Member Level: Gold   Points : 0
Sathish has updated the source files and now it should work.


Author: Ben Spigle    04 Aug 2004Member Level: Bronze   Points : 0
It appears that if the server goes down the clients will continue to execute normally but just don't get any messages, even if the server comes back up. That makes sense looking at the code, but for the application that I'm looking at, this behaviour would be a serious flaw. Can you think of a way for the clients to know if the server goes away?


Author: Yacdani    24 Nov 2004Member Level: Bronze   Points : 0
Hellow, I'm Interesting in do it but in a web server
by port 80 using soap, I did it locally (localhost) but when I get it to the web server and try to conect It give me an internal error and the client never conects any Idea ?? Thanks...


Author: Kristofer Krause    18 Jan 2005Member Level: Bronze   Points : 0
I have the following above example in C# if anyone is interested. Drop me an email over at http://www.kriskrause.com.


Author: SudhakarPaulraj    09 Oct 2007Member Level: Bronze   Points : 0
Hi,

Suppose a client program get's closed, how to remove the same Client from the Server (Array m_clients).

Is there any way to Check Whether the Client is still alive or Not





Author: Nisha Agrawal    23 Nov 2007Member Level: Bronze   Points : 0
it was a nice article. i have downloaded and run the server at web server. now i run the client at system in same network in which our server and another client in different network. client in the same network work well but in different could not work.
so can u suggest me anything so that ii can work in two different networks.


Author: Nick Locke    23 Jun 2008Member Level: Bronze   Points : 2
An excellent article - thank you. I have downloaded it and it worked first time. My challenge is that the images on the article page are missing, which makes it difficult to understand the architecture.

It looks as if the URL structure of the site might have changed since the article was published. Can anyone tell me how/where to find the images? Thanks.


Author: Nick Locke    23 Jun 2008Member Level: Bronze   Points : 2
An excellent article - thank you. I have downloaded it and it worked first time. My challenge is that the images on the article page are missing, which makes it difficult to understand the architecture.

It looks as if the URL structure of the site might have changed since the article was published. Can anyone tell me how/where to find the images? Thanks.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Using .Net Remoting to Raise Events to Clients from Events  .  Using .Net Remoting to Raise Events from Servers to Clients  .  Raising Events to Clients from Server  .  Raising Events from Servers to Clients  .  Raising Events from Server  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Overview of the System.Web.Services Namespace
Previous Resource: Creating and Consuming an ASP.NET (C#.NET) Web Service
Return to Discussion Resource Index
Post New Resource
Category: WCF/Webservices


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

accuconference

Contact Us    Privacy Policy    Terms Of Use