wcf不同协议的调用效率比较

项目中使用的wcf服务调用起来反映比较慢,昨天看了下怎么使用tcp协议传输,今天就对tcp和http的调用效率做了一个比较。

一.寄宿与iis的tcp协议的服务调用和控制台自寄宿http协议的服务调用的比较:

因为tcp协议不能使用控制台自寄宿,所以用iis来代替,http使用轻量级的自寄宿。

客户端的调用:

        static void Main(string[] args)
        {
            //比较一下使用http和tcp的性能
            TestHttpService();
            TestTcpService();
            Console.Read();
        }

        private static void TestTcpService()
        { 
             using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
             {
                ICalculator calculator = channelFactory.CreateChannel();
                using (calculator as IDisposable)
                {
                    try
                    {
                        DateTime beginTime = DateTime.Now;
                        calculator.Add(1, 2);
                        DateTime endTime = DateTime.Now;
                        Console.WriteLine("tcp调用花费时间:" + (endTime-beginTime).TotalMilliseconds.ToString());    
                    }
                    catch (CommunicationException)
                    {
                        (calculator as ICommunicationObject).Abort();
                        throw;
                    }
                    catch (TimeoutException)
                    {
                        (calculator as ICommunicationObject).Abort();
                        throw;
                    }
                }
            }
        }

        private static void TestHttpService()
        {
            using (HttpService.CalculatorClient client = new HttpService.CalculatorClient())
            {
                try
                {
                    DateTime beginTime = DateTime.Now;
                    client.Add(1, 2);
                    DateTime endTime = DateTime.Now;
                    Console.WriteLine("http调用花费时间:" + (endTime - beginTime).TotalMilliseconds.ToString());    
                }
                catch (CommunicationException)
                {
                    (client as ICommunicationObject).Abort();
                    throw;
                }
                catch (TimeoutException)
                {
                    (client as ICommunicationObject).Abort();
                    throw;
                }
            }
        }

控制台自寄宿的实现:

static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
            {
                host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");
               if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                {
                    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                    behavior.HttpGetEnabled = true;
                    behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");
                     host.Description.Behaviors.Add(behavior);
                }
              host.Opened += delegate
                {
                    Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
                };
 
                 host.Open();
                Console.Read();
            }
        }

wcf的调用,使用毫秒级的单位就可以体现出来区别了,运行自寄宿程序,启动iis,第一次调用的速度真是坑,tcp的调用也用了800毫秒,而http竟然用了6秒多:

第二次运行速度就快了很多,不过http还是比较慢:

 

二.wcf应用程序的http协议和控制台寄宿的http协议的效率比较:

第一次调用:

 

第二次调用,vs调试器的效率竟然基本没有变化,被自寄宿的给爆成渣了:

应该比较一下iis寄宿的http协议的效率,不过老是报错,今天就先到这里,过两天补上,未完待续...

转载于:https://www.cnblogs.com/jinshizuofei/p/4157778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值