测试服务是否链接

static void Main(string[] args)
{
DateTime dt = DateTime.Now;
//object[] x = new object[3];
//x[0] = "STEMI";
//x[1] = Convert.ToDateTime("2013-01-01");
//x[2] = Convert.ToDateTime("2013-01-31");
string y = (string)ExecuteMethod<IService>("net.tcp://192.168.2.126:8869/HjService", "TestHello", null);
Console.WriteLine((DateTime.Now - dt).ToString());
Console.WriteLine(wcftest());
Console.ReadLine();
}
private static string wcftest()
{
// 先建一个类下面的成员,来存放进程
List<Thread> pool = new List<Thread>();
DateTime dt = DateTime.Now;
bool flag = true;
while (true)
{
if(flag)
{
flag = false;
//在需要启动你的方法的时候建进城
Thread t = new Thread(new ThreadStart(test));
pool.Add(t);
t.Start();
}
if ((DateTime.Now - dt) >TimeSpan.Parse("00:00:01"))
{
//终止进程
foreach (Thread t in pool)
{
if (t != null && t.IsAlive)
{
if (!t.Join(5))
{
t.Abort();
return "失败";
}
}
else
{
return "成功";
}
}
}

}
}
private static void test()
{
string y = (string)ExecuteMethod<IService>("net.tcp://192.168.2.126:8869/HjService", "TestHello", null);
}
public static object ExecuteMethod<T>(string pUrl, string pMethodName, params object[] pParams)
{
EndpointAddress address = new EndpointAddress(pUrl);
NetTcpBinding netTcpBinding = new NetTcpBinding(SecurityMode.None, true);
netTcpBinding.ReliableSession.Enabled = true;
netTcpBinding.ReliableSession.Ordered = true;
netTcpBinding.MaxConnections = 1000;
netTcpBinding.PortSharingEnabled = true;
netTcpBinding.ListenBacklog = 1000;
netTcpBinding.MaxBufferPoolSize = (long)int.MaxValue;
netTcpBinding.MaxBufferSize = int.MaxValue;
netTcpBinding.MaxReceivedMessageSize = (long)int.MaxValue;
// netTcpBinding.ReaderQuotas = new XmlDictionaryReaderQuotas() { MaxStringContentLength = int.MaxValue };
//netTcpBinding.ReaderQuotas.MaxArrayLength = int.MaxValue;
//netTcpBinding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
netTcpBinding.CloseTimeout = new TimeSpan(0, 30, 0);
netTcpBinding.OpenTimeout = new TimeSpan(0, 30, 0);
netTcpBinding.ReceiveTimeout = new TimeSpan(0, 30, 0);
netTcpBinding.SendTimeout = new TimeSpan(0, 30, 0);
netTcpBinding.ReliableSession.InactivityTimeout = new TimeSpan(0, 30, 0);

// bindinginstance = ws;
using (ChannelFactory<T> channel = new ChannelFactory<T>(netTcpBinding, address))
{
T instance = channel.CreateChannel();
foreach (System.ServiceModel.Description.OperationDescription op in channel.Endpoint.Contract.Operations)
{
System.ServiceModel.Description.DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors.Find<System.ServiceModel.Description.DataContractSerializerOperationBehavior>()
as System.ServiceModel.Description.DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
{
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
}
}
using (instance as IDisposable)
{
try
{
Type type = typeof(T);
MethodInfo mi = type.GetMethod(pMethodName);
return mi.Invoke(instance, pParams);
}
catch (TimeoutException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (CommunicationException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (Exception vErr)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
}
}

posted on 2015-04-02 15:34  秦少66  阅读(148)  评论(0编辑  收藏  举报

导航