动态调用WCF服务,只需要提供*.svc地址,

1:命名空间:

using System.ServiceModel.Channels;
using System.ServiceModel;

2:创建访问类InvokeContext

public class InvokeContext
    {

#region Wcf服务工厂
        public static T CreateWCFServiceByURL<T>(string url)
        {
            return CreateWCFServiceByURL<T>(url, "wsHttpBinding");
        }
        public static T CreateWCFServiceByURL<T>(string url,string bing)
        {
            if (string.IsNullOrEmpty(url)) throw new NotSupportedException("this url isn`t Null or Empty!");
            EndpointAddress address = new EndpointAddress(url);
            Binding binding = CreateBinding(bing);
            ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
            return factory.CreateChannel();
        }
        #endregion

#region 创建传输协议
        /// <summary>
        /// 创建传输协议
        /// </summary>
        /// <param name="binding">传输协议名称</param>
        /// <returns></returns>
        private static Binding CreateBinding(string binding)
        {
            Binding bindinginstance = null;
            if (binding.ToLower() == "basichttpbinding")
            {
                BasicHttpBinding ws = new BasicHttpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding.ToLower() == "netnamedpipebinding")
            {
                NetNamedPipeBinding ws = new NetNamedPipeBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding.ToLower() == "netpeertcpbinding")
            {
                NetPeerTcpBinding ws = new NetPeerTcpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding.ToLower() == "nettcpbinding")
            {
                NetTcpBinding ws = new NetTcpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                ws.Security.Mode = SecurityMode.None;
                bindinginstance = ws;
            }
            else if (binding.ToLower() == "wsdualhttpbinding")
            {
                WSDualHttpBinding ws = new WSDualHttpBinding();
                ws.MaxReceivedMessageSize = 65535000;

bindinginstance = ws;
            }
            else if (binding.ToLower() == "webhttpbinding")
            {
                WebHttpBinding ws = new WebHttpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding.ToLower() == "wsfederationhttpbinding")
            {
                WSFederationHttpBinding ws = new WSFederationHttpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding.ToLower() == "wshttpbinding")
            {
                WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
                ws.MaxReceivedMessageSize = 65535000;
                ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
                ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
                bindinginstance = ws;
            }
            return bindinginstance;

}
        #endregion

}

3.T为调用的接口类型,就是WCF返回的类的实例,由于我的WCF是基于接口的,所以加了一个接品类型判断,调用时直接写:Interface iface = InvokeContext.CreateWCFServiceByURL<Interface>(url);

动态调用WCF服务的更多相关文章

  1. 记录:Web无引用无配置方式动态调用WCF服务

    这几年一直用WebApi较多,最近项目中有个需求比较适合使用WCF,以前也用过JQuery直接调用Wcf的,但是说实话真的忘了… 所以这次解决完还是花几分钟记录一下 WCF服务端:宿主在现有Win服务 ...

  2. 学习之路十四:客户端调用WCF服务的几种方法小议

    最近项目中接触了一点WCF的知识,也就是怎么调用WCF服务,上网查了一些资料,很快就搞出来,可是不符合头的要求,主要有以下几个方面: ①WCF的地址会变动,地址虽变,但是里面的逻辑不变! ②不要引用W ...

  3. 创建一个简单的WCF程序2——手动开启/关闭WCF服务与动态调用WCF地址

    一.创建WCF服务器 1.创建WCF服务器的窗体应用程序 打开VS2010,选择文件→新建→项目菜单项,在打开的新建项目对话框中,依次选择Visual C#→Windows→Windows窗体应用程序 ...

  4. c# 动态调用WCF方法笔记!

    //动态调用wcf方法 string url = "http://localhost:54379/ServiceWCF.svc"; IDoubleService proxy = W ...

  5. C#动态调用WCF接口,两种方式任你选。

    写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...

  6. 实现jquery.ajax及原生的XMLHttpRequest跨域调用WCF服务的方法

    关于ajax跨域调用WCF服务的方法很多,经过我反复的代码测试,认为如下方法是最为简便的,当然也不能说别人的方法是错误的,下面就来上代码,WCF服务定义还是延用上次的,如: namespace Wcf ...

  7. 检测WCF服务是否在线(动态执行WCF服务)

    在我们的系统里有多个wcf支撑.但是有的时候会莫名的停止,于是今天写了一个服务,定时去检测其他wcf服务是否在线.那么最简单的办法自然是引用其他wcf服务让vs给我们自动生成clientProxy然后 ...

  8. 学习调用WCF服务的各种方法

    1.开发工具调用WCF 这中方法很方便也很简单,很多工作VS就帮我们完成了.相信大家也不会对这种方法陌生.这里简单提一下.打开VS,在项目中添加服务引用: 在config中自动声明了有关服务的节点信息 ...

  9. [转]学习 WCF (6)--学习调用WCF服务的各种方法

    转自:http://www.cnblogs.com/gaoweipeng/archive/2009/07/26/1528263.html 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...

随机推荐

  1. C# 平时碰见的问题【3】

    今天发现一个问题纳闷了半个小时, 需求是处理project文件里边的数据内容,其中需要判断任务名称不存在重复; 在测试的时候弄了两行一样的任务,如预想: 任务[xxx]重复 然后删掉重复的任务行,继续 ...

  2. openSUSE13.1安装时要注意的问题(未完待续)

    1.最好用官方给的imageWriter来写镜像,不要用UltraISO来写镜像,会导致安装Kaffein包错误(:)可能也会有别的错误),后来我用imageWriter写了之后就没有在安装时报错了

  3. WdatePicker 动态变量表

    4. 日期范围限制静态限制 注意:日期格式必须与 realDateFmt 和 realTimeFmt 一致 你可以给通过配置minDate(最小日期),maxDate(最大日期)为静态日期值,来限定日 ...

  4. ios8中的UIScreen

    let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation pri ...

  5. IOS内存管理「1」- 引用计数

  6. Swift global function(count indexOfObject contains...)

    当你在使用Swift时会发现一些常用的函数不!见!了! 比如 String:    s.count()  s.contains() Array: a.indexOfObeject(t:<T> ...

  7. [转]分布式文件系统FastDFS架构剖析

    [转]分布式文件系统FastDFS架构剖析 http://www.programmer.com.cn/4380/ 文/余庆 FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实 ...

  8. Qt 读取txt文件乱码的解决办法

    Qt 读取txt文本乱码问题 2015-05-20 15:46 方法一:使用QString的fromLocal8Bit()函数 复制代码 QFile txtfile(filePath);        ...

  9. SQL Server 2008 没有可用于 charge_sys_Log.LDF 的编辑器

    因为上网问题重新装了系统.今天在整 SQL Server  的时候出现了这样一个问题. 因为之前装 SQL Server  的时候没有遇到过这种情况,感觉很新奇.所以详细的记录一下,希望对别人能有一定 ...

  10. 用telnet和php的curl库测试http

    一.telnet测试http telnet简介     Telnet协议是TCP/IP协议族的其中之一,是Internet远端登录服务的标准协议和主要方式,常用于网页服务器的远端控制,可供使用者在本地 ...