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

客户端使用自定义代理类访问WCF服务 z的更多相关文章

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

    通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否简 ...

  2. 关于IIS寄宿WCF服务,客户端不能生成代理类

    我在使用VS2010写好WCF的Web服务后,部署在IIS7.0上,可以在IE9上进行访问,并且能显示XML数据,如下图 然后我在项目的客户端进行服务的添加引用,如下图 VS2010自动生成代理类,但 ...

  3. Android访问WCF服务(使用json实现参数传递)

    经过多日努力, 终于勉强弄明白了Android访问WCF服务的方法. 服务端实现 一, 实现服务. 操作契约 [ServiceContract] public interface IService { ...

  4. ajax调用handler,使用HttpWebRequest访问WCF服务

    引言 随着手机及移动设备的普及,移动端的应用也进入了热潮.以前PC端的门户网站,大多也均推出了适配移动设备的网站或者APP,再差的也注册了个公众号.在移动应用开发中,目前据我所了解到的解决方案有:1. ...

  5. 快速访问WCF服务--ServiceModel 元数据实用工具 (Svcutil.exe)

    基本定义 ServiceModel 元数据实用工具用于依据元数据文档生成服务模型代码,以及依据服务模型代码生成元数据文档. SvcUtil.exe ServiceModel 元数据实用工具可在 Win ...

  6. Android访问WCF服务

    原文链接:http://www.cnblogs.com/VinC/archive/2011/02/24/1964049.html 本章目的: 用Wcf建立可以上Android可以访问的数据服务, 数据 ...

  7. Wince 中访问WCF服务

    由于本文并非WinCE开发普及篇,所以一些WinCE开发和WCF开发的基础还请移步百度和谷歌寻找答案,然后结合本文开发出WinCE中如何访问WCF,谢谢. 开发环境 IDE:Visual Studio ...

  8. Ajax跨域访问wcf服务中所遇到的问题总结。

    工具说明:vs2012,sql server 2008R2 1.首先,通过vs2012建立一个wcf服务项目,建立好之后.再新开一个vs2012 建立web项目,通过jQuery的ajax方法访问服务 ...

  9. 三种客户端访问wcf服务端的方法 C#

    原文 http://blog.csdn.net/zlj002/article/details/7914556 string jsonstr = String.Empty; string url = & ...

随机推荐

  1. spring 初始化时注入bean实现listener的方法

    两种方法: 1.实现ApplicationListener<ContextRefreshedEvent>的onApplicationEvent(ContextRefreshedEvent ...

  2. 通过JavaScript更新UpdatePanel备忘

    1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs ...

  3. unity, animtion倒放

    AnimationState.speed Description The playback speed of the animation. 1 is normal playback speed. A ...

  4. Openjudge计算概论——数组逆序重放【递归练习】

    /*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...

  5. c# 调用MD5CryptoServiceProvider出现 System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.

    进注册表按Win+R运行regedit修改下面的值为0就可以了 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorith ...

  6. XSS转码 &amp;&amp; struts2 property标签的bug

    struts2: <s:property value="name" escape="false"/> EL表达式: jsp 2.0中的 ${todo ...

  7. 邮件发送工具类 SendMail.java

    package com.util; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.Simp ...

  8. Hibernate常用配置文件详解

    本文转载自:http://blog.csdn.net/csh624366188/article/details/7578939 初学hibernate的童鞋,刚开应该都有这种感觉,hibernate的 ...

  9. 转载:mybatis自动生成

    MyBatis Generator中文文档 MyBatis Generator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容 ...

  10. sublime text 3 安装注释

    sublime text 3 1.安装Sublime Text 3  下载安装:http://www.sublimetext.com/3 Package Control安装:https://subli ...