你的位置:首页 > ASP.net教程

[ASP.net教程]客户端调用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}\r\n\r\n", 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}\r\n\r\n", 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客户端调用的办法吗?