zoukankan      html  css  js  c++  java
  • 测试服务是否链接

    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;
    }
    }
    }
    }

  • 相关阅读:
    维护win10注册表
    win10操作技巧
    无处不网络
    事件驱动编程思想
    流程控制之if...else
    python----GIL的概念
    并发与同步异步的概念
    实现并发join的方法
    线程的调用
    三元运算符
  • 原文地址:https://www.cnblogs.com/qinge/p/4387119.html
Copyright © 2011-2022 走看看