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

  • 相关阅读:
    前端程序员容易忽视的一些基础知识
    一道前端学习题
    Unity调用Windows对话框保存时另存为弹框
    Unity镜子效果的实现(无需镜子Shader)
    Unity射线检测的用法总结
    unity中实现简单对象池,附教程原理
    Unity调用Window提示框Yes/No(英文提示窗)
    Unity调用Windows弹框、提示框(确认与否,中文)
    C#LinQ语法
    服务器的购买与网站的创建
  • 原文地址:https://www.cnblogs.com/server126/p/4884415.html
Copyright © 2011-2022 走看看