Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System;
  2. using System.ServiceModel;
  3. using System.Runtime.Serialization;
  4.  
  5. namespace WCFSimple.Contract
  6. {
  7.     [ServiceContract]
  8.     public interface IService
  9.     {
  10.         [OperationContract]        
  11.         string Hello(Communication.DataForRequestPing test);
  12.  
  13.         [OperationContract]
  14.         Communication.DataForResponsePong Ping(Communication.DataForRequestPing requestPing);
  15.  
  16.         [OperationContract]
  17.         Communication.DataForResponseVersion Version(Communication.DataForRequestVersion requestVersion);
  18.  
  19.     }
  20. }
  21.  
  22.  
  23. namespace Communication
  24. {
  25.     [DataContract]
  26.     public class DataForRequestPing
  27.     {
  28.         [DataMember]
  29.         public string Ping { get; set;}
  30.         //public string Ping = "";
  31.     }
  32.    
  33.     public class DataForResponsePong
  34.     {
  35.         public string Pong = "";
  36.     }
  37.    
  38.     public class DataForRequestVersion
  39.     {
  40.         public string Ping = "";
  41.     }
  42.    
  43.     public class DataForResponseVersion
  44.     {
  45.         public string Pong = "";
  46.         public string Version = "";
  47.     }
  48. }
  49.  
  50. namespace WCFSimple.ClientSide
  51. {
  52.     public class Client
  53.     {
  54.         #region Connection
  55.  
  56.         public static ChannelFactory<WCFSimple.Contract.IService> scf;
  57.  
  58.  
  59.         public static WCFSimple.Contract.IService Service;
  60.  
  61.         public static void CreateConnection(string host, int port)
  62.         {
  63.  
  64.             NetTcpBinding tcpBind = new NetTcpBinding();
  65.             //tcpBind.ReliableSession.Enabled = true;
  66.             //tcpBind.ReliableSession.Ordered = false;
  67.             //tcpBind.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
  68.             tcpBind.Security.Mode = SecurityMode.None;
  69.             //tcpBind.ReceiveTimeout = TimeSpan.FromSeconds(10);
  70.  
  71.             scf = new ChannelFactory<WCFSimple.Contract.IService>(
  72.                 //new NetTcpBinding(),                
  73.                 tcpBind,
  74.                 String.Format("net.tcp://{0}:{1}", host, port));
  75.  
  76.  
  77.             Service = WCFSimple.ClientSide.Client.scf.CreateChannel();
  78.  
  79.         }
  80.  
  81.         public static void CloseConnection()
  82.         {
  83.             try
  84.             {
  85.                 if((Service as ICommunicationObject).State == CommunicationState.Opened)
  86.                     (Service as ICommunicationObject).Close();
  87.             }
  88.             catch (CommunicationException ex) { }
  89.             catch (Exception ex) { }
  90.         }
  91.  
  92.         #endregion
  93.  
  94.     }
  95. }
  96.  
  97. namespace WCFSimple
  98. {
  99.     class Program
  100.     {
  101.         static void Main(string[] args)
  102.         {
  103.  
  104.             string host;
  105.             int port = 0;
  106.  
  107.  
  108.             #region input check
  109.  
  110.             //Default port
  111.             if (args.Length == 1)
  112.                 port = 1333;
  113.  
  114.             try
  115.             {
  116.                 host = args[0];
  117.                 if(port != 1333)
  118.                     port = Convert.ToInt16(args[1]);
  119.             }
  120.             catch
  121.             {
  122.                 return;
  123.             }
  124.  
  125.             #endregion
  126.  
  127.            
  128.  
  129.             WCFSimple.ClientSide.Client.CreateConnection(host, port);
  130.  
  131.  
  132.                     Communication.DataForRequestPing request = new Communication.DataForRequestPing();
  133.                     request.Ping = "test";
  134.                     response = WCFSimple.ClientSide.Client.Service.Hello(request);
  135.  
  136.  
  137.             ClientSide.Client.CloseConnection();
  138.            
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement