专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WCF

客户端怎么更具需要连接不同的服务端

发布时间:2011-06-23 13:50:49 文章来源:www.iduyao.cn 采编人员:星星草
客户端如何更具需要连接不同的服务端

当我们写客户端《--------》服务端的时候,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

 

第二步:在客户端我们新建两个接口文件IService、IMyName(我采用的是服务端就是服务端,客户端是客户端,分为两个解决方案)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace WCFdny
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IMyName”。
    [ServiceContract]
    public interface IMyName
    {
        [OperationContract]
        string DoWork();
    }
}
View Code

 

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

namespace WCFdny
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”。
    [ServiceContract]
    public interface IService
    {
        //上传文件
        [OperationContract]
        UpFileResult UpLoadFile(UpFile filestream);

        //下载文件
        [OperationContract]
        DownFileResult DownLoadFile(DownFile downfile);

        [OperationContract]
        string myname(string str_name);
    }
    [MessageContract]
    public class DownFile
    {
        [MessageHeader]
        public string FileName { get; set; }
    }

    [MessageContract]
    public class UpFileResult
    {
        [MessageHeader]
        public bool IsSuccess { get; set; }
        [MessageHeader]
        public string Message { get; set; }
    }

    [MessageContract]
    public class UpFile
    {
        [MessageHeader]
        public long FileSize { get; set; }
        [MessageHeader]
        public string FileName { get; set; }
        [MessageBodyMember]
        public Stream FileStream { get; set; }
    }

    [MessageContract]
    public class DownFileResult
    {
        [MessageHeader]
        public long FileSize { get; set; }
        [MessageHeader]
        public bool IsSuccess { get; set; }
        [MessageHeader]
        public string Message { get; set; }
        [MessageBodyMember]
        public Stream FileStream { get; set; }
    }
}
View Code

 

第三步:客户端的按钮实现功能

private void button1_Click(object sender, EventArgs e)
        {
            string aa = string.Empty;
            NetTcpBinding bind = new NetTcpBinding();//绑定方式
            bind.MaxBufferPoolSize = 2147483647;//最大缓冲
            bind.TransferMode = TransferMode.Streamed;//传输模式为流式处理
            bind.MaxReceivedMessageSize = 2147483647;//定义了服务端接收Message的最大长度,防止文件过大
            bind.Security.Mode = SecurityMode.None;//安全模式设置为不进行验证;
            if (radioButton1.Checked)
            {
                EndpointAddress epAddr = new EndpointAddress("net.tcp://127.0.0.1:8081");//此处也可以用IIS做服务
                IMyName proxy = ChannelFactory<IMyName>.CreateChannel(bind, epAddr);
                aa = "本地WCF" + proxy.DoWork();
            }
            if(radioButton2.Checked)
            {
                EndpointAddress epAddr = new EndpointAddress("net.tcp://120.25.160.17:8081");//此处也可以用IIS做服务
                IMyName proxy = ChannelFactory<IMyName>.CreateChannel(bind, epAddr);
                aa = "服务器WCF" + proxy.DoWork();
            }
            MessageBox.Show(aa);
        }
View Code

 

第四步:开启服务端,将服务端分布在服务器和本地测试一下,结果如图

 本地服务端运行截图

服务端运行截图

客户端运行截图

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: