Advertisement
Guest User

Untitled

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