11.07.2015 Views

y - Net Developer

y - Net Developer

y - Net Developer

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 12 ■ DEVELOPING PEER-TO-PEER APPLICATIONS WITH WCF 385ramrameshreddy.blog.comListing 12-4. Service Host Class Definition[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]public partial class Main : Form, IQuickReturnTraderChat{}The class Main implements, as shown in Listing 12-5, two methods called StartServiceand StopService, which start and stop the service host. The class Main also has a few membervariables exposing the Channel, ServiceHost, and ChannelFactory.Listing 12-5. Service Host ImplementationIQuickReturnTraderChat channel;ServiceHost host = null;ChannelFactory channelFactory = null;string userID = "";private void StartService(){//Instantiate new ServiceHosthost = new ServiceHost(this);//Open ServiceHosthost.Open();//Create a ChannelFactory and load the configuration settingchannelFactory = new ChannelFactory("QuickTraderChatEndpoint");channel = channelFactory.CreateChannel();//Lets others know that someone new has joinedchannel.Say("Admin", "*** New User " + userID + " Joined ****" +Environment.NewLine);}private void StopService(){if (host != null){channel.Say("Admin", "*** User " + userID + " Leaving ****" +Environment.NewLine);}}ramrameshreddyramrameshreddyif (host.State != CommunicationState.Closed){channelFactory.Close();host.Close();}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!