Silverlight 3 Chat application with WCF Duplex Binding


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?




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

40 mg cialis sale http://cialisverys.com/ - online cialis <a href="http://cialisverys.com/">buy cialis</a> cialis 10mg packungsbeilage

<a href=https://cialisworld.com>cialis once a day 2.5 mg</a> <a href=https://buyrealcialisonline.com>cialis 20mg jdr</a> la diabetes y el cialis cialis spedizione da europa acidez de cialis <a href=https://cialisworld.com>top rated generic viagra sites</a> <a href=https://buycialisgenericzs.com>buying viagra spain at chemist</a> <a href=https://genericcialisonline2018.com>buying viagra z reviews</a> <a href=https://buyrealcialisonline.com>cialis 20 mg bottle</a> <a href=https://cialisworld.com>cialis 20mg nmd</a> <a href=https://cialisworld.com>cialis generico 5mg</a> <a href=https://cialisworld.com>generic viagra los angeles</a>

Perdere 3 kg una settimana <a href=http://ogorodsadovod.com>ogorodsadovod.com</a>

<a href="http://canadianonlinepharmacyhd.com/">online pharmacy no prescription</a> http://canadianonlinepharmacyhd.com/ <a href=http://canadianonlinepharmacyhd.com/>online drugstore</a>

<a href="http://canadianonlinepharmacyhd.com/">online pharmacy</a> http://canadianonlinepharmacyhd.com/ <a href=http://canadianonlinepharmacyhd.com/>pharcharmy online no precipitation</a>

<a href="http://canadianonlinexyz.com/">canadian pharmacy online</a> http://canadianonlinexyz.com/ <a href=http://canadianonlinexyz.com/>highest rated canadian pharmacies</a>

<a href="http://canadianonlinexyz.com/">online pharmacies</a> http://canadianonlinexyz.com/ <a href=http://canadianonlinexyz.com/>rx online no prior prescription</a>

JasonIdeds 01-03-2019
dove acquistare cialis online sicurocialis online nabp buy cheap cialis onine <a href="http://xcialis20mg.com">http://xcialis20mg.com</a>

????? 03-06-2019
https://dancesweb.com/theking/ - ????? https://dancesweb.com/theninecasino/ - ?????? https://dancesweb.com/woori/ - ????? https://dancesweb.com/ - ?????

https://dancesweb.com/first/ 03-06-2019
https://dancesweb.com/first/ https://dancesweb.com/yesca/ https://dancesweb.com/yesno/ https://dancesweb.com/coincasino/ https://dancesweb.com/coinkore/ https://dancesweb.com/obama/ https://dancesweb.com/trump/ https://dancesweb.com/33casino/ https://dancesweb.com/gatsby/2 https://dancesweb.com/bacara/ https://dancesweb.com/casino/

https://como79.com/ 03-06-2019
https://como79.com/ https://como79.com/theking/ https://como79.com/super/ https://como79.com/yescasino/ https://como79.com/yes/ https://como79.com/coincasino/ https://como79.com/coinkorea/ https://como79.com/obama/ https://como79.com/first/ https://como79.com/mcasino/ https://como79.com/trump/ https://como79.com/gatsby/ https://como79.com/theninecasino/

https://stylebet79.com/ 03-06-2019
https://stylebet79.com/ https://stylebet79.com/theking/ https://stylebet79.com/yes https://stylebet79.com/first/ https://stylebet79.com/obama/ https://stylebet79.com/yescasino/ https://stylebet79.com/coinka/ https://stylebet79.com/coincasino/ https://stylebet79.com/canibal/ https://stylebet79.com/4ucsno/ https://stylebet79.com/4uca/ https://stylebet79.com/theninecasino/

https://hero-wanted.com/ 03-06-2019
https://hero-wanted.com/ https://hero-wanted.com/theking/ https://hero-wanted.com/super/ https://hero-wanted.com/first/ https://hero-wanted.com/obama/ https://hero-wanted.com/yes/ https://hero-wanted.com/yescasino/ https://hero-wanted.com/4ucasino/ https://hero-wanted.com/4uca/ https://hero-wanted.com/coincasino/ https://hero-wanted.com/coin/ https://hero-wanted.com/trump/ https://hero-wanted.com/gatsby/ https://hero-wanted.com/canibal/ https://hero-wanted.com/theninecasino/

????? 18-06-2019
https://dancesweb.com/theking/ - ????? https://dancesweb.com/theninecasino/ - ?????? https://dancesweb.com/woori/ - ????? https://dancesweb.com/ - ????? https://dancesweb.com/first/ - ?????? https://dancesweb.com/coinkore/ - ?????

New Comment

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