Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ServiceModel;
- using System.Runtime.Serialization;
- namespace WCFSimple.Contract
- {
- [ServiceContract]
- public interface IService
- {
- [OperationContract]
- string Hello(Communication.DataForRequestPing test);
- [OperationContract]
- Communication.DataForResponsePong Ping(Communication.DataForRequestPing requestPing);
- [OperationContract]
- Communication.DataForResponseVersion Version(Communication.DataForRequestVersion requestVersion);
- }
- }
- namespace Communication
- {
- [DataContract]
- public class DataForRequestPing
- {
- [DataMember]
- public string Ping { get; set;}
- //public string Ping = "";
- }
- public class DataForResponsePong
- {
- public string Pong = "";
- }
- public class DataForRequestVersion
- {
- public string Ping = "";
- }
- public class DataForResponseVersion
- {
- public string Pong = "";
- public string Version = "";
- }
- }
- namespace WCFSimple.ClientSide
- {
- public class Client
- {
- #region Connection
- public static ChannelFactory<WCFSimple.Contract.IService> scf;
- public static WCFSimple.Contract.IService Service;
- public static void CreateConnection(string host, int port)
- {
- NetTcpBinding tcpBind = new NetTcpBinding();
- //tcpBind.ReliableSession.Enabled = true;
- //tcpBind.ReliableSession.Ordered = false;
- //tcpBind.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
- tcpBind.Security.Mode = SecurityMode.None;
- //tcpBind.ReceiveTimeout = TimeSpan.FromSeconds(10);
- scf = new ChannelFactory<WCFSimple.Contract.IService>(
- //new NetTcpBinding(),
- tcpBind,
- String.Format("net.tcp://{0}:{1}", host, port));
- Service = WCFSimple.ClientSide.Client.scf.CreateChannel();
- }
- public static void CloseConnection()
- {
- try
- {
- if((Service as ICommunicationObject).State == CommunicationState.Opened)
- (Service as ICommunicationObject).Close();
- }
- catch (CommunicationException ex) { }
- catch (Exception ex) { }
- }
- #endregion
- }
- }
- namespace WCFSimple
- {
- class Program
- {
- static void Main(string[] args)
- {
- string host;
- int port = 0;
- #region input check
- //Default port
- if (args.Length == 1)
- port = 1333;
- try
- {
- host = args[0];
- if(port != 1333)
- port = Convert.ToInt16(args[1]);
- }
- catch
- {
- return;
- }
- #endregion
- WCFSimple.ClientSide.Client.CreateConnection(host, port);
- Communication.DataForRequestPing request = new Communication.DataForRequestPing();
- request.Ping = "test";
- response = WCFSimple.ClientSide.Client.Service.Hello(request);
- ClientSide.Client.CloseConnection();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement