客户端使用自定义代理类访问WCF服务

时间:2021-11-27 16:02:26

通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或web.config文件中产生WCF服务的客户端配置信息。若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否简单在app.config或web.config文件增加WCF服务地址,然后直接通过此地址访问WCF服务呢?可以,那就是通过自定义客户端代理类来实现。本文是通过继承ClientBase<T>类实现的自定义客户端代理类,来实现同过简单在app.config或web.config文件增加wcf服务地,然后直接通过此地址访问WCF服务。

以下以一个简单的计算器WCF服务为例:

解决方案项目目录结构:

客户端使用自定义代理类访问WCF服务

其中WCFExample.ServiceInterface项目是WCF服务接口、WCFExample.ServiceImplement项目是WCF服务实现、WCFExample.Host项目是服务宿主、WCFExample.ServiceClient项目自定义WCF服务客户端代理、WCFExample.ServiceClientTest项目是客户端测试

WCF服务接口定义

[ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        decimal Add(decimal a, decimal b);
    }

WCF服务接口实现

public class Calculator : ICalculator
    {
        public decimal Add(decimal a, decimal b)
        {
            return a + b;
        }
    }

WCF服务客户端自定义代理类

internal class CalculatorClient : ClientBase<ICalculator>,ICalculator
    {
        public CalculatorClient(Binding binding, EndpointAddress remoteAddress):base(binding,remoteAddress)
        {
        }

public  decimal Add(decimal a,decimal b)
        {
           return base.Channel.Add(a, b);
        }
    }

服务创建工厂

internal class ServiceFactory
    {
        public ICalculator GetCalculatorClient(string remotingAddress)
        {
            if(string.IsNullOrEmpty(remotingAddress))
            {
                return null;
            }
            try
            {
                return new CalculatorClient(this.GetInitBinding(), new EndpointAddress(remotingAddress));
            }
            catch
            {
                return null;
            }
        }

public ICalculator GetCalculatorClient(string remotingAddress, Binding binding)
        {
            if (string.IsNullOrEmpty(remotingAddress))
            {
                return null;
            }
            try
            {
                return new CalculatorClient(binding, new EndpointAddress(remotingAddress));
            }
            catch
            {
                return null;
            }
        }

private BasicHttpBinding GetInitBinding()
        {
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.MaxBufferSize = 0x27100000;
            binding.MaxReceivedMessageSize = 0x27100000L;
            binding.MaxBufferPoolSize = 0x138800000L;
            XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
            quotas.MaxStringContentLength = 0x4e20000;
            binding.ReaderQuotas = quotas;
            return binding;
        }
    }

对外服务代理类

public class ServiceProxy
    {
        private ICalculator m_ICalculator;
        public ServiceProxy(string remotingAddress)
        {
            m_ICalculator = new ServiceFactory().GetCalculatorClient(remotingAddress);
        }

public ServiceProxy(string remotingAddress,Binding binding)
        {
            m_ICalculator = new ServiceFactory().GetCalculatorClient(remotingAddress, binding);
        }

public decimal Add(decimal a,decimal b)
        {
            return m_ICalculator.Add(a, b);
        }
    }

客户端web.config增加服务地址

<appSettings>
  <add key="WCFAddress" value="http://wcf.test.com/Calculator.svc%22/>
 </appSettings>

客户端调用

decimal a = 10;
   decimal b = 20;
   string url = System.Configuration.ConfigurationManager.AppSettings["WCFAddress"];
   ServiceProxy serviceProxy = new ServiceProxy(url);
   Response.Write(serviceProxy.Add(a,b));

项目下载:http://files.cnblogs.com/binny1983/WCFExample.rar