• 客户端调用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客户端调用的办法吗?

  • 相关阅读:
    Windows Server环境下消息队列之ActiveMQ实战
    Javascript 面向对象编程(一):封装
    Python常用函数
    redis源码(一):为redis添加自己的列表类型
    导出数据到Excel表格
    Oracle————存储过程与函数
    Solr搜索引擎 — 通过mysql配置数据源
    redis的安装和使用【2】redis的java操作
    MySql(四)Select条件查询
    MySql (二)入门语句和基本操作
  • 原文地址:https://www.cnblogs.com/server126/p/4884415.html
Copyright © 2020-2023  润新知