当我们写客户端《--------》服务端的时候,AB服务端都可以做同样的工作,通过右键添加服务引用的话,拂过做成分布集群的话,一两个服务端无所谓,十个了你得添加十次,二十个你得添加二十次。那么问题出现了,如何在客户端动态的根据各个服务器连接数自动的负载均衡了。

客户端如何更具需要连接不同的服务端

第一步:服务端肯定有服务文件,如图

 

客户端如何更具需要连接不同的服务端

代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFService
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service”。
    public class Service : IService ,IMyName
    {
        public UpFileResult UpLoadFile(UpFile filedata)
        {

            UpFileResult result = new UpFileResult();

            string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\service\";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            byte[] buffer = new byte[filedata.FileSize];

            FileStream fs = new FileStream(path + filedata.FileName, FileMode.Create, FileAccess.Write);

            int count = 0;
            while ((count = filedata.FileStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                fs.Write(buffer, 0, count);
            }
            //清空缓冲区
            fs.Flush();
            //关闭流
            fs.Close();

            result.IsSuccess = true;

            return result;

        }

        //下载文件
        public DownFileResult DownLoadFile(DownFile filedata)
        {

            DownFileResult result = new DownFileResult();

            string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\service\" + filedata.FileName;

            if (!File.Exists(path))
            {
                result.IsSuccess = false;
                result.FileSize = 0;
                result.Message = "服务器不存在此文件";
                result.FileStream = new MemoryStream();
                return result;
            }
            Stream ms = new MemoryStream();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            fs.CopyTo(ms);
            ms.Position = 0;  //重要,不为0的话,客户端读取有问题
            result.IsSuccess = true;
            result.FileSize = ms.Length;
            result.FileStream = ms;

            fs.Flush();
            fs.Close();
            return result;
        }

        public string myname(string str_name)
        {
            return string.Format("我的名字是:{0}",str_name);
        }

        public string DoWork()
        {
            return "付长梦";
        }
    }
}
View Code

相关文章:

  • 2021-11-16
  • 2021-06-26
  • 2022-12-23
  • 2021-10-13
  • 2021-08-22
  • 2022-01-29
  • 2022-12-23
猜你喜欢
  • 2021-05-02
  • 2021-07-02
  • 2021-10-07
  • 2021-06-17
  • 2021-06-04
  • 2021-11-22
  • 2021-12-06
相关资源
相似解决方案