using System;
using System.ServiceModel;
namespace WCFSimple.Contract
{
[ServiceContract]
public interface IService
{
[OperationContract]
string Hello();
}
}
namespace WCFSimple.ClientSide
{
public class Client
{
public static class Requests
{
public static string Hello()
{
string response = "";
try
{
response = WCFSimple.ClientSide.Client.Service.Hello();
}
catch (TimeoutException ex)
{
}
catch (Exception ex)
{
}
return response;
}
}
#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);
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)
{
#region input check
string host;
int port;
try
{
host = args[0];
port = Convert.ToInt16(args[1]);
}
catch
{
return;
}
#endregion
WCFSimple.ClientSide.Client.CreateConnection(host, port);
string response = WCFSimple.ClientSide.Client.Requests.Hello();
ClientSide.Client.CloseConnection();
if(!String.IsNullOrEmpty(response))
{
Console.WriteLine("CLIENT> Response from service: " + response);
}
else
Console.WriteLine("CLIENT> No response");
}
}
}