<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tags 'Misc Technology' and 'WIndows Communication Foundation'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=Misc+Technology,WIndows+Communication+Foundation&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'Misc Technology' and 'WIndows Communication Foundation'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>WCF to the rescue</title><link>http://msmvps.com/blogs/williamryan/archive/2008/07/07/wcf-to-the-rescue.aspx</link><pubDate>Mon, 07 Jul 2008 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1639700</guid><dc:creator>William</dc:creator><description>&lt;p&gt;Although part of the idea below was mine, James Ashley was crucial to helping me figure this out.&lt;/p&gt; &lt;p&gt;Problem:&lt;/p&gt; &lt;p&gt;Without getting into the ghoury details, we have an application that allows users to send messages to large groups of people.&amp;nbsp; Anyone using a client application that is logged on will get a special notification which they can respond to once a message is sent. So far, Polling has been the bane of our existence.&amp;nbsp; We&amp;#39;re trying to fancy up the UI and Telerik&amp;#39;s Winforms controls have helped out a lot, but there&amp;#39;s simply no way I&amp;#39;ve been able to find to have a snappy UI with a lot of really cool looking effects - all the while polling every few seconds.&amp;nbsp; Until now, I just bit down, and took a walk through multi-threadedville.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;I wanted to remove the polling.&amp;nbsp; We had experimented with a few different architectures, but all had the same problem, polling.&amp;nbsp; So let&amp;#39;s say you had 200 users logged on.&amp;nbsp; In the User&amp;#39;s bar, we wanted to show each user and their picture - much the same way IM does.&amp;nbsp; This app had to work across network boundaries and you couldn&amp;#39;t count on being able to use TCP so we had to at least make it work with http.&amp;nbsp; If you poll every three seconds, to get the latest info on who&amp;#39;s logged on and off, rebuilding the list of user&amp;#39;s was painful. We resorted to several tricks to determine if a user was already in the list, but all of it was putting lipstick on a pig.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Solution:&lt;/p&gt; &lt;p&gt;One of the big things you often hear about with WCF is self-hosting.&amp;nbsp; I had ignored it for the most part.&amp;nbsp; But today, I wanted to basically have a Winforms app host one service, and a Windows Service host the other part of the service, having the services talk to each other. The client winforms app would simply talk to the client service, it would never hit the Windows Service directly.&lt;/p&gt; &lt;p&gt;So we built a client service and server service, with a client winforms app hosting the client service and a mock Windows Service (which was simply another winforms app) that hosted the service.&lt;/p&gt; &lt;p&gt;The problem we kept running into is as each client signed on to the system, how could we tell the server svc that they were online?&amp;nbsp; Dynamically adding endpoints is child&amp;#39;s play, but with each winforms app, you could only interact with the ServiceHost - not the service.&lt;/p&gt; &lt;p&gt;We tried using traditional events to no avail - that wasn&amp;#39;t a surprise but we figured it&amp;#39;s worth a try.&amp;nbsp; Then James recommended a static event. I was completely skeptical, but alas it worked.&amp;nbsp; &lt;/p&gt; &lt;p&gt;After looking at Juval&amp;#39;s framework though, it became really clear. If we want the services to be able to interact with the hosts, then simply self-host the things.&lt;/p&gt; &lt;p&gt;So we had a method called ReceiveMessage which was called from the service (via a client proxy for the client service).&amp;nbsp; Here&amp;#39;s basically what the client looked like:&lt;/p&gt; &lt;p&gt;public partial class Form1 : Form, IClientService&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Form1()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; InitializeComponent();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }  &lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ServiceHost currentHost = null;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private void btnStartService_Click(object sender, EventArgs e)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentHost = new ServiceHost(typeof(Form1));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; currentHost.Open();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lblServiceStatus.Text = &amp;quot;Started&amp;quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (Exception ex)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lblServiceStatus.Text = ex.Message;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }  &lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string ReceiveMessage(string msg)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; String myMessage = msg;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MessageBox.Show(myMessage);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return msg;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;p&gt;.... &lt;p&gt;&amp;nbsp; &lt;p&gt;Now, from the Administrative application (which interacted with the service, you could Send a message by doing the following:) &lt;p&gt;private void btnSend_Click(object sender, EventArgs e)&lt;br /&gt;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proxy = new ClientServiceClient(new NetNamedPipeBinding(), endpoint );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proxy.ReceiveMessage(rtbMessageText.Text);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; catch (FaultException ex)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lblServiceStatus.Text = ex.Message;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp; } &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;ClientServiceClient is a stupid name for a client proxy class, but we were just experimenting - the main thing to note is that it&amp;#39;s simply a proxy class for the client service, the same service that&amp;#39;s effectively being hosted by the Winforms client.&amp;nbsp; There&amp;#39;s a server method called SendMessage(String msg) which you&amp;#39;d call on the service, which in turn would dynamically add endpoints for each active client , and then send the messages to them.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;You can easily store all the information for clients you want to send messages to in a List&amp;lt;&amp;gt; or similar structure - or if you need stronger persistence or have different needs, then you can use a database.&amp;nbsp; Either way the end result is the same - loop through all the clients, add an endpoint for each one, loop through each and call the method you want.&lt;/p&gt; &lt;p&gt;I&amp;#39;m having trouble getting to my ftp server from where i&amp;#39;m at, but I&amp;#39;ll post the code when I can. In the meantime, if you&amp;#39;re interested, just drop me a line.&lt;/p&gt;</description></item></channel></rss>