zoukankan      html  css  js  c++  java
  • 客户端调用wcf服务,如何提高调用性能

    IO调用服务

    1、使用using(每次自动释放)

      for (var i = 0; i < 10; i++)
                    {
                        var watch = new Stopwatch();
                        watch.Start();
                        using (var client = new StreamServiceClient())
                        {
                            var result = client.Upload(new StreamRequest
                            {
                                Bytes = data,
                                FileExt = ext,
                                Passport = ""
                            });
                            if (!string.IsNullOrEmpty(result.Msg))
                                MessageBox.Show(result.Msg);
                        }
                        watch.Stop();
                        this.richTextBox1.AppendText(string.Format("第{0}次:耗时:{1}
    
    ", i, watch.ElapsedMilliseconds));
                    }
    View Code

    耗时:

    2、重用wcf通信通道

    for (var i = 0; i < 10; i++)
                    {
                        var watch = new Stopwatch();
                        watch.Start();
                        var client = new StreamServiceClient();
                        {
                            var result = client.Upload(new StreamRequest
                            {
                                Bytes = data,
                                FileExt = ext,
                                Passport = ""
                            });
                            if (!string.IsNullOrEmpty(result.Msg))
                                MessageBox.Show(result.Msg);
                        }
                        watch.Stop();
                        this.richTextBox1.AppendText(string.Format("第{0}次:耗时:{1}
    
    ", i, watch.ElapsedMilliseconds));
                    }
    View Code

    耗时:

    3、自定义wcf通道

    先创建一个channel,如下:

    /// <summary>
        /// IO服务通道
        /// by:jgl
        /// date:15-10-10
        /// </summary>
        public sealed class EmpIOChannelFacotry
        {
            private static readonly object LockObject = new object();
            private static StreamServiceClient client;
    
            public static StreamServiceClient Channel
            {
                get
                {
                    lock (LockObject)
                    {
                        var locaClient = client;
                        if (locaClient != null && locaClient.State != CommunicationState.Faulted) return locaClient;
                        client = new StreamServiceClient();
                        return client;
                    }
                }
                set
                {
                    lock (LockObject)
                    {
                        if (client == null) return;
                        if (client.State != CommunicationState.Opened)
                            client.Abort();
                        client.Close();
                        client = value;
                    }
                }
            }
        }
    View Code

    耗时:

    此文章属于个人记录,我是wcf的菜鸟,希望大家多多批评与指点。

    请问各位还有其他办法能够提供wcf客户端调用的办法吗?

  • 相关阅读:
    Redis持久化
    Redis进阶——事务、TTL、排序、消息通知、管道
    行为设计模式
    Redis数据类型
    ASP.NET并发处理
    c# 泛型有什么作用?
    IBatisNet 升级到 .Net Framework 4.0 时发现 IBatisNet 一小BUG
    【Python3】用for循环和while循环分别打印出如下格式“九九乘法表”
    Visual Studio 2010 SP1 WPF程序属性重命名BUG,小心!
    为什么用SOA架构?
  • 原文地址:https://www.cnblogs.com/server126/p/4884415.html
Copyright © 2011-2022 走看看