WCF 动态调用(动态创建实例接口)
很多时候,服务地址都不止一个的,这个时候就要动态去配置地址。配置Web.config,很麻烦
下面就看看怎样实现动态调用WCF。
首先看看动态创建服务对象的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Channels; /// <summary>
/// 动态调用WCF的工具类库
/// </summary>
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.MaxBufferSize = 2147483647;
ws.MaxBufferPoolSize = 2147483647;
ws.MaxReceivedMessageSize = 2147483647;
ws.ReaderQuotas.MaxStringContentLength = 2147483647;
ws.CloseTimeout = new TimeSpan(0, 10, 0);
ws.OpenTimeout = new TimeSpan(0, 10, 0);
ws.ReceiveTimeout = new TimeSpan(0, 10, 0);
ws.SendTimeout = new TimeSpan(0, 10, 0); 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 }
svcutil.exe http://localhost:8034/WCFserver.svc?wsdl
IWCFserver dpser = InvokeContext.CreateWCFServiceByURL<IWCFserver>(Public.getXmlElementValue("LocalDpPathologySliceServ"), "basicHttpBinding");
WCF 动态调用(动态创建实例接口)的更多相关文章
- 阿里云ECS开放批量创建实例接口,实现弹性资源的创建
摘要: 为了更方便的实现弹性的资源创建,方便用户一次运行多台ECS按量实例来完成应用的开发和部署,阿里云开放了ECS的批量创建实例接口RunInstances,可以单次最多创建100台实例,避免重复调 ...
- 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值
前言: 最近小匹夫参与的游戏项目到了需要读取数据的阶段了,那么觉得自己业余时间也该实践下数据相关的内容.那么从哪入手呢?因为用的是Unity3d的游戏引擎,思来想去就选择了C#读取XML文件这个小功能 ...
- [转]Delphi DLL的创建、静态 以及动态调用
第一章 DLL简单介绍 由于在目前的学习工作中,需要用到DLL文件,就学习了下,在这里作个总结. 首先装简单介绍下DLL: 1,减小可执行文件的大小 DLL技术的产生有很大一部分原因是为了减小可执行 ...
- [转]linux 调用动态库so文件
记录一个面试被问到的问题. extern 有什么用途? 除了多文件共享全局变量外还有呢? extern "C" 的功能? 我想看完这篇文章就可以知道第三个问题了. 关于动态调用动态 ...
- C#调用WebService服务(动态调用)
原文:C#调用WebService服务(动态调用) 1 创建WebService using System; using System.Web.Services; namespace WebServi ...
- C#动态调用带有SoapHeader验证的WebServices
http://blog.csdn.net/u012995964/article/details/54573143 本文记录C#中通过反射动态的调用带有SoapHeader验证的WebServices服 ...
- Java父类对象调用子类实体:方法重写与动态调用
众所周知Java的handle和C++的ponter而不是object对应,我们很熟悉C++的父类pointer调用子类实体的例子,那么对于Java的handle是不是也可以这样呢? 这里我先给一个例 ...
- C# 中静态调用C++dll 和C# 中动态调用C++dll
在最近的项目中,牵涉到项目源代码保密问题,由于代码是C#写的,容易被反编译,因此决定抽取核心算法部分使用C++编写,C++到目前为止好像还不能被很好的反编译,当然如果你是反汇编高手的话,也许还是有可能 ...
- C#动态调用WCF接口,两种方式任你选。
写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...
随机推荐
- 【Nowcoder 上海五校赛】1 + 2 = 3?(斐波那契规律)
题目描述 小Y在研究数字的时候,发现了一个神奇的等式方程,他屈指算了一下有很多正整数x满足这个等式,比如1和2,现在问题来了,他想知道从小到大第N个满足这个等式的正整数,请你用程序帮他计算一下. (表 ...
- oracle-sql优化-通过分组和缓存减少不必要的读
环境:aix 7.1,oracle12.1.0.2 cdb 优化前SQL select * from (select row_.*, rownum rownum_ from (select '弱覆盖' ...
- ubuntu安装和常用软件推荐
ubuntu安装和常用软件推荐(个人整理) 2016.08.22 17:29 13811浏览 字号 安装一套双系统,win10打游戏,ubuntu开发,win10放机械,ubuntu放固态,电脑联想i ...
- 精干货! Java 后端程序员 1 年工作经验总结
一.引言 毕业已经一年有余,这一年里特别感谢技术管理人员的器重,以及同事的帮忙,学到了不少 东西.这一年里走过一些弯路,也碰到一些难题,也受到过做为一名开发却经常为系统维护 和发布当救火队员的苦恼 ...
- Tornado学习
为什么用Tornado? 异步编程原理 服务器同时要对许多客户端提供服务,他的性能至关重要.而服务器端的处理流程,只要遇到了I/O操作,往往需要长时间的等待. 屏幕快照 2018-10-31 上午 ...
- I/O流、序列化
1)流序列化对象ObjectOutputStream调用writerObject写出序列化对象,ObjectInputStream调用readObject读取序列化对象,序列化的对象必须要实现Seri ...
- I/O流、字符集
1)InputStream.OutPutStream是输出输入的基础流,均为抽象类,提供了read和writer方法,所有的子类均实现read和writer方法,read在遇到输入源的结尾时返回-1. ...
- python多线程举例
Python中使用线程有两种方式:函数或者用类来包装线程对象. 1. 函数式:调用thread模块中的start_new_thread()函数来产生新线程.如下例: import time ...
- Python的Bottle框架中实现最基本的get和post的方法的教程
这篇文章主要介绍了Python的Bottle框架中实现最基本的get和post的方法的教程,Bottle框架在Python开发者中的人气很高,需要的朋友可以参考下 1.GET方式: # -*- cod ...
- node Cookie
代码: const express = require('express'); const cookieParser = require('cookie-parser'); const app = e ...