michielpost.nl

Silverlight 3 Chat application with WCF Duplex Binding

01-06-2009

How does the application work?
The ChatHost accepts connections from all Silverlight clients. Silverlight calls a login method on the ChatHost and the ChatHost holds a list of all connected clients.

When a Silverlight clients sends a message to the chatbox, the ChatHost will broadcast this message to all connected clients.

If it fails to deliver the message to the client, the client probably closed it's browser and is removed from the connected clients list.

Live Example Currently Offline

Client Access Policy
The ChatHost server is running on a different port number than the Silverlight webpage's (port 80). Because port 80 is used by IIS, the ChatHost can't host the WCF services on the same port number.
But Silverlight is not automatically allowed to access services on other servers/ports. So a Client Access Policy file (clientaccesspolicy.xml) must be provided by the target server.
The ChatHost can provide this file to Silverlight through the CrossDomainService.cs which is included in the ChatHost project.


How to get this working on your server?

  1. Download the source code
  2. Modify the App.config of the ChatHost and change the ServiceBase setting. Change the server name and port number to the name of your server and a port number you like.
  3. Modify the Silverlight project and specify the server name and port of the WCF endpoint (which you modified at step 2).
  4. Compile the projects
  5. Run the server
  6. Run Silverlight project

Download the full source code of the project here.

Silverlight Sockets
Are you looking for a socket based Silverlight chat application?
http://www.michielpost.nl/PostDetail_7.aspx

 

 

Comments


Jeff Jin 14-06-2009
Great example of using WCF duplex binding. One suggestion about Singleton implementation for the client list in DuplexService, maybe just make the service InstanceContextMode as Single, such as [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, Namespace = ".....")] One problem I found in SL3 is Add/Update service reference does not create correct proxy. DuplexClientBase is not used for Service class, instead simple ClientBase is used.


Michiel Post 16-06-2009
@Jeff Jin, thanks for the tip on InstanceContextMode.Single. I modified the solution and made the service InstanceContactMode Single instead of the Singleton.


Michiel 08-07-2009
See this blog on how to increase amount of silverlight duplex clients: http://weblogs.asp.net/alexeyzakharov/archive/2009/04/17/how-to-increase-amount-of-silverlight-duplex-clients.aspx


badr 29-07-2009
hi please tell me how i can run the host.exe on the server it's worked fine on my pc but when i tried to upload it to my site i couldn't start it please help as soon as you can Thank you


Sean 14-08-2009
How do you add a service reference to the host from the client when there is no .svc file?


bob 18-08-2009
can someone help? i love the fact that Michiel doesn't use SQL here. but, i can't get mine to work. keep getting error on my server, "No connection to the server, reload page and try again." i'm using VS2008 with .NET FW 3.5 SP1. based on the instruction above, here's what i did. in ChatHost > App.Config file: <ChatHost.Properties.Settings> <setting name="ServiceBase" serializeAs="String"> <value>http://www.myDomain.com:50343/</value> </setting> </ChatHost.Properties.Settings> for instruction #3 above (ChatClient > Page.xaml.cs), i made the following modification: EndpointAddress address = new EndpointAddress("http://www.myDomain.com:50343"); did instruction 4 thru 6. uploaded the stuff to my server. got error msg as i mentioned above. can someone help what am i doing wrong? also, if i just run the code inside VS2008 without modification, shouldn't it work? i tried and i got error message, "An error occurred while trying to make a request to URI 'http://localhost:50343/'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details."


bob 21-08-2009
i got the stuff to work on the local PC. very nice!! now, have same question as BADR and SEAN. please help.


shane 03-10-2009
Great sample. I can get it to work on my pc using the host.exe running in the background. How do I get this to work in IIS on my hosting site? I've got a paid space through discountasp.net so it's not my own server.


Michiel 29-11-2009
It won't work if you only have space on a shared server. You need to be able to run the ChatServer application...


Saif 02-02-2010
great example..I took you code and modified it to be more like a peer to peer chat like we have in yahoo web messenger.But the message sent by one client takes very long to deliver to the other client as the number of clients increase.I am filtering the message on the client itself..checking if the message sent is for him or not.Also i have hosted the wcf library project in IIS.Could you please help


New Comment

Name
E-mail (not published)
Comment
Enter the code shown: