A console chat app made using the duplex mode of WCF

Source: Internet
Author: User

WCF Services

Using system;using system.collections.generic;using system.linq;using system.runtime.serialization;using System.servicemodel;using system.servicemodel.web;using system.text;namespace WcfService1{    //Note: Use the "Refactoring" menu Rename command, you can change the code and the interface name "IService1" in the configuration file at the same time.    [ServiceContract (callbackcontract = typeof (IMyClass))] public    interface IService1    {                 [ OperationContract]        string  Send (String id,string pid, string str);        [OperationContract]        String Register (string id);        [OperationContract]        List<string> allhost ();        }    [ServiceContract]    Public interface IMyClass    {        [OperationContract (IsOneWay = true)]//callback function method must be added isoneway=true        void Reciver (string str);    }     }

  

Using system;using system.collections.generic;using system.linq;using system.runtime.serialization;using System.servicemodel;using system.servicemodel.web;using system.text;namespace WcfService1{//Note: Using the rename command on the Refactor menu,    You can change the class name "Service1" in Code, SVC, and configuration files at the same time.        public class Service1:iservice1 {public static dictionary<string, imyclass> hostdic;        public static list<string> Allhost;  <summary>/////</summary>/<param name= "id" > Sender </param>// <param name= "pid" > Recipient </param>//<param name= "str" > Content </param>//<returns>&                               Lt;/returns> public string Send (String id,string pid, string str) {try {                         foreach (var d in Hostdic) {if (D.key = = pid) {                    D.value.reciver (id+ "send:" +str);                }}//imyclass myclass= operationcontext.current.getcallbackchannel<imyclass> (); MyClass.                Reciver ("Hello");            return "1"; } catch (Exception ex) {return ex.            Message;        }} public list<string> Allhost () {return allhost; public string Register (string id) {if (hostdic = = null) {Hostdic =            New dictionary<string, imyclass> ();            } if (allhost = = null) {allhost = new list<string> ();                } IMyClass IMyClass = operationcontext.current.getcallbackchannel<imyclass> (); Hostdic.                ADD (ID, imyclass); Allhost.                        ADD (ID);        return ID; }        }}

Host

Using system;using system.collections.generic;using system.linq;using system.text;using System.ServiceModel; Namespace serverhost{    class program    {        static void Main (string[] args)        {            ServiceHost ServerHost = New ServiceHost (typeof (Wcfservice1.service1));            ServerHost. Open ();            Console.WriteLine ("Open");            Console.readkey ();}}}    

Host configuration file

<?xml version= "1.0" encoding= "Utf-8"?><configuration><system.servicemodel><services>< Service name= "Wcfservice1.service1" ><endpoint address= "Net.tcp://192.168.1.12:3721/calculatorservice"
Change to local IP
binding= "nettcpbinding" contract= "Wcfservice1.iservice1" bindingconfiguration = "ticketbindingconfiguration"/> </service> </services> <bindings> <netTcpBinding> <binding name= " Ticketbindingconfiguration "opentimeout=" 00:10:10 "receivetimeout=" 00:10:10 "sendtimeout=" 00:10:10 "maxBufferSize= "2147483647" maxreceivedmessagesize= "2147483647" > <security mode= "None"/> <readerquotas Maxstringcontentlength= "6553600" maxarraylength= "6553600"/> </binding> </netTcpBinding> </ Bindings> </system.serviceModel> </configuration>

Client

Using system;using system.collections.generic;using system.linq;using system.text;using WcfService1;using System.servicemodel;using System.configuration;namespace consoleapplication1{class Program {static void Mai                N (string[] args) {try {myclass MyClass = new MyClass ();                Myclass.contentevent + = receive;                InstanceContext callback = new InstanceContext (MyClass);  channelfactory<iservice1> CHANNL = new Channelfactory<iservice1> ("Wcfserver"); If there is no callback in duplex mode, use this duplexchannelfactory<iservice1> CHANNL = new Duplexchannelfactory<iservice1&gt                ;(callback, "Wcfserver"); IService1 IService1 = channl.                CreateChannel ();                Console.WriteLine ("Please enter your own user name");                String id = console.readline (); String str = Iservice1.register (ID.                Trim ());                Console.WriteLine (str);     while (true)           {Console.WriteLine ("Input send Data");                    string info = Console.ReadLine ();                    Console.WriteLine ("Input Recipient");                    String piduser = Console.ReadLine (); Console.WriteLine ("Send To" + Piduser. Trim () + ":" + info.                    Trim ()); Iservice1.send (Id,piduser. Trim (), info.                Trim ()); }} catch (Exception ex) {Console.WriteLine (ex).                Message);            Console.readkey ();        }}} public class Myclass:imyclass {//public delegate void Conten (String str);        public event Conten Contentevent;            public void Reciver (String str) {Console.WriteLine ("{0}:" + str, System.DateTime.Now);        Contentevent (str); }    }}

Client configuration file

<?xml version= "1.0" encoding= "Utf-8"?><configuration><system.servicemodel><client>< Endpoint Name= "Wcfserver" address= "net.tcp://192.168.1.12:3721/calculatorservice"//change to local IP  binding= " NetTcpBinding "  contract=" Wcfservice1.iservice1 "bindingconfiguration =" Ticketbindingconfiguration "/></ Client><bindings><nettcpbinding><binding  name= "Ticketbindingconfiguration"  opentimeout= "00:10:10" receivetimeout= "00:10:10"    sendtimeout= "00:10:10"  maxbuffersize= "2147483647" Maxreceivedmessagesize= "2147483647" ><security mode= "None"/><readerquotas  maxStringContentLength = "6553600" maxarraylength= "6553600"/></binding></nettcpbinding></bindings></ System.servicemodel></configuration>

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.