C#动态调用WCF接口
C#动态调用WCF接口
写在前面
接触WCF还是它在最初诞生之处,一个分布式应用的巨作。 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF。 从这个小项目中我学会了两个地方: 1、利用IIS部署WCF服务,也就是大家接触到的发布SVC文件。2、动态调用WCF接口。
在这个项目中接触WCF时遇到的其实不仅仅是这两个问题,甚至连IIS支持SVC文件也让我折腾了好几把,IIS都重新卸载了两次。 我在这篇文章里用两种方式来实现。
如何使用
1、第一种方式比较简单,而且也是大家喜欢的,因为不需要任何配置文件就可解决,只需知道服务契约接口和服务地址就可以调用。
2、使用Invoke的方式,但是需要在调用客户端配置WCF,配置后在Invoke类里封装服务契约接口即可。
客户端调用DEMO
1
2
3
4
5
6
7
|
//第一种方式 IDoubleService proxy = WcfInvokeFactory.CreateServiceByUrl<IDoubleService>(url); int result = proxy.Add(1, 3); //第二种方式<br><br>int result1 = WCFInvoke.Invoke(t => t.Add(1, 3));<br><br> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name= "NewBehavior" > <dataContractSerializer maxItemsInObjectGraph= "65536000" /> </behavior> </endpointBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name= "BasicHttpBinding_IDoubleService" closeTimeout= "01:00:00" openTimeout= "01:00:00" sendTimeout= "01:00:00" receiveTimeout= "01:00:00" maxBufferSize= "2147483647" maxBufferPoolSize= "524288" maxReceivedMessageSize= "2147483647" > <readerQuotas maxDepth= "128" maxStringContentLength= "2147483647" maxArrayLength= "16384" maxBytesPerRead= "4096" maxNameTableCharCount= "16384" /> </binding> </basicHttpBinding> <netMsmqBinding> <binding name= "NetMsmqBinding_IAsyncSender" > <security mode= "None" /> </binding> </netMsmqBinding> </bindings> <client> binding= "basicHttpBinding" bindingConfiguration= "BasicHttpBinding_IDoubleService" contract= "DoubleStone.WebHost.IDoubleService" name= "BasicHttpBinding_IDoubleService" /> </client> </system.serviceModel> |
第一种调用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
public class WcfInvokeFactory { #region WCF服务工厂 public static T CreateServiceByUrl<T>( string url) { return CreateServiceByUrl<T>(url, "basicHttpBinding" ); } public static T CreateServiceByUrl<T>( string url, string bing) { try { if ( string .IsNullOrEmpty(url)) throw new NotSupportedException( "This url is not Null or Empty!" ); EndpointAddress address = new EndpointAddress(url); Binding binding = CreateBinding(bing); ChannelFactory<T> factory = new ChannelFactory<T>(binding, address); return factory.CreateChannel(); } catch (Exception ex) { throw new Exception( "创建服务工厂出现异常." ); } } #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, 30, 0); ws.OpenTimeout = new TimeSpan(0, 30, 0); ws.ReceiveTimeout = new TimeSpan(0, 30, 0); ws.SendTimeout = new TimeSpan(0, 30, 0); 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() == "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 } |
第二种调用方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class WCFInvoke { /// <summary> /// 你需要调用的服务契约 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="func"></param> /// <returns></returns> public static T Invoke<T>(Func<IDoubleService, T> func) { IServiceInvoker serviceInvoker= new WCFServiceInvoker(); return serviceInvoker.InvokeService(func); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
public interface IServiceInvoker { void InvokeService<T>(Action<T> invokeHandler) where T : class ; TReslt InvokeService<T, TReslt>(Func<T, TReslt> invokeHandler) where T : class ; } public class WCFServiceInvoker:IServiceInvoker { private static readonly ChannelFactoryManager FactoryManager = new ChannelFactoryManager(); private static readonly ClientSection ClientSection = ConfigurationManager.GetSection( "system.serviceModel/client" ) as ClientSection; public void InvokeService<T>(Action<T> invokeHandler) where T : class { KeyValuePair< string , string > endpointNameAddressPair = GetEndpointNameAddressPair( typeof (T)); var arg = FactoryManager.CreateChannel<T>(endpointNameAddressPair.Key, endpointNameAddressPair.Value); var obj2 = (ICommunicationObject)arg; try { invokeHandler(arg); } finally { try { if (obj2.State != CommunicationState.Faulted) { obj2.Close(); } } catch { obj2.Abort(); } } } public TReslt InvokeService<T, TReslt>(Func<T, TReslt> invokeHandler) where T : class { KeyValuePair< string , string > endpointNameAddressPair = GetEndpointNameAddressPair( typeof (T)); var arg = FactoryManager.CreateChannel<T>(endpointNameAddressPair.Key, endpointNameAddressPair.Value); var obj2 = (ICommunicationObject)arg; try { return invokeHandler(arg); } finally { try { if (obj2.State != CommunicationState.Closed || obj2.State != CommunicationState.Faulted) { obj2.Close(); } } catch { obj2.Abort(); } } } private KeyValuePair< string , string > GetEndpointNameAddressPair(Type serviceContractType) { var configException = new ConfigurationErrorsException( string .Format( "No client endpoint found for type {0}. Please add the section <client><endpoint name=\"myservice\" address=\"http://address/\" binding=\"basicHttpBinding\" contract=\"{0}\"/></client> in the config file." , serviceContractType)); if (((ClientSection == null ) || (ClientSection.Endpoints == null )) || (ClientSection.Endpoints.Count < 1)) { throw configException; } foreach (ChannelEndpointElement element in ClientSection.Endpoints) { if (element.Contract == serviceContractType.ToString()) { return new KeyValuePair< string , string >(element.Name, element.Address.AbsoluteUri); } } throw configException; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
public class ChannelFactoryManager : IDisposable { private static readonly Dictionary<Type, ChannelFactory> Factories = new Dictionary<Type, ChannelFactory>(); private static readonly object SyncRoot = new object (); public void Dispose() { Dispose( true ); } public virtual T CreateChannel<T>() where T : class { return CreateChannel<T>( "*" , null ); } public virtual T CreateChannel<T>( string endpointConfigurationName) where T : class { return CreateChannel<T>(endpointConfigurationName, null ); } public virtual T CreateChannel<T>( string endpointConfigurationName, string endpointAddress) where T : class { T local = GetFactory<T>(endpointConfigurationName, endpointAddress).CreateChannel(); ((IClientChannel)local).Faulted += ChannelFaulted; return local; } protected virtual ChannelFactory<T> GetFactory<T>( string endpointConfigurationName, string endpointAddress) where T : class { lock (SyncRoot) { ChannelFactory factory; if (!Factories.TryGetValue( typeof (T), out factory)) { factory = CreateFactoryInstance<T>(endpointConfigurationName, endpointAddress); Factories.Add( typeof (T), factory); } return (factory as ChannelFactory<T>); } } private ChannelFactory CreateFactoryInstance<T>( string endpointConfigurationName, string endpointAddress) { ChannelFactory factory = null ; factory = ! string .IsNullOrEmpty(endpointAddress) ? new ChannelFactory<T>(endpointConfigurationName, new EndpointAddress(endpointAddress)) : new ChannelFactory<T>(endpointConfigurationName); factory.Faulted += FactoryFaulted; factory.Open(); return factory; } private void ChannelFaulted( object sender, EventArgs e) { var channel = (IClientChannel)sender; try { channel.Close(); } catch { channel.Abort(); } } private void FactoryFaulted( object sender, EventArgs args) { var factory = (ChannelFactory)sender; try { factory.Close(); } catch { factory.Abort(); } Type[] genericArguments = factory.GetType().GetGenericArguments(); if ((genericArguments.Length == 1)) { Type key = genericArguments[0]; if (Factories.ContainsKey(key)) { Factories.Remove(key); } } } protected virtual void Dispose( bool disposing) { if (disposing) { lock (SyncRoot) { foreach (Type type in Factories.Keys) { ChannelFactory factory = Factories[type]; try { factory.Close(); } catch { factory.Abort(); } } Factories.Clear(); } } } } |
总结
第一种方式比较常见,第二种方式是我参考另外一个项目中的写法,其中的有一些细节我还没有搞明白,实现了这个功能后还需要再看看这部分代码,再消化消化。由于是直接在项目中,所以没有提供源代码下载,有朋友需要的话我会整理出demo,稍后放出下载链接。
C#动态调用WCF接口的更多相关文章
- C#动态调用WCF接口,两种方式任你选。
写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...
- 动态调用wcf接口服务
1.url:http://localhost:8002/名称.svc/basic(.svc结尾) 2.需要引用的命名空间System.ServiceModel 3.调用代码: public class ...
- C#动态调用WCF接口(3)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...
- C#动态调用WCF接口(2)
如何使用 1.第一种方式比较简单,而且也是大家喜欢的,因为不需要任何配置文件就可解决,只需知道服务契约接口和服务地址就可以调用. 2.使用Invoke的方式,但是需要在调用客户端配置WCF,配置后在I ...
- 动态调用WCF服务
动态调用WCF服务,只需要提供*.svc地址, 1:命名空间: using System.ServiceModel.Channels;using System.ServiceModel; 2:创建访问 ...
- 动态调用WCF
最近在做的一个项目中需要动态调用WCF地址,因为有很多终端服务器,而每台终端服务器上都部署一个WCF服务,中央服务器需要不定时调用其中某个或者多个WCF服务执行相关操作,因此添加引用及配置文件配置的方 ...
- 创建一个简单的WCF程序2——手动开启/关闭WCF服务与动态调用WCF地址
一.创建WCF服务器 1.创建WCF服务器的窗体应用程序 打开VS2010,选择文件→新建→项目菜单项,在打开的新建项目对话框中,依次选择Visual C#→Windows→Windows窗体应用程序 ...
- c# 动态调用WCF方法笔记!
//动态调用wcf方法 string url = "http://localhost:54379/ServiceWCF.svc"; IDoubleService proxy = W ...
- 动态调用WebService接口的几种方式
一.什么是WebService? 这里就不再赘述了,想要了解的====>传送门 二.为什么要动态调用WebService接口? 一般在C#开发中调用webService服务中的接口都是通过引用过 ...
随机推荐
- javascript - 浏览TOM大叔博客的学习笔记
part1 ---------------------------------------------------------------------------------------------- ...
- Parallel Python——一个简单的分布式计算系统
如何建立一个高速的分布式计算平台?Parallel python此目的. Parallel Python(http://www.parallelpython.com/content/view/15/3 ...
- Java 过滤器的作用
Servlet API 非常久曾经就已成为企业应用开发的基石,而 Servlet 过滤器则是对 J2EE 家族的相对较新的补充.在 J2EE 探索者 系列文章的最后一篇中,作者 Kyle Gabhar ...
- 在汉澳sinox2014建立ZFS高可靠文件存储系统
在汉澳sinox2014建立ZFS高可靠文件存储系统 汉澳sinox2014能够用比較小的固态硬盘安装,文件系统能够用zfs系统存放. 请准备一些硬盘,比方三块SCSI硬盘:da0,da1,da2 如 ...
- quick 2.23 它们的定义c++代码lua与总结的一些细节
它们的定义c++代码lua与总结的一些细节 参考:点击打开链接 1.自己定义 XXX.cpp .XXX.h 2.D:\quick\quick-cocos2d-x-2.2.3-rc\lib\cocos2 ...
- LA3026 - Period(KMP)
For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...
- WebCollector 2.x 新手教程
WebCollector爬虫官网:https://github.com/CrawlScript/WebCollector 技术讨论群:250108697 WebCollector 2.x教程列表 We ...
- Oracle 工艺结构
Oracle工艺结构 这个过程是动态创建,完毕任务后就消亡:而程序是静态的实体,程序是能够复制.编辑的.进程强调的是运行过程,而程序不过指令的有序集合:进程在内存中,程序在外存中. ORACLE分为用 ...
- Scrapy系列教程(2)------Item(结构化数据存储结构)
Items 爬取的主要目标就是从非结构性的数据源提取结构性数据,比如网页. Scrapy提供 Item 类来满足这种需求. Item 对象是种简单的容器.保存了爬取到得数据. 其提供了 类似于词典(d ...
- 【Android进阶】使用第三方平台ShareSDK实现新浪微博的一键分享功能
在公司最近的一个项目中,需要实现一键分享功能,在这里我使用的是第三方平台ShareSDK,将使用经验与大家分享 先看效果图 主界面 分享界面 由于第一次使用,所以需要先进行新浪授权,授权界面 分享结果 ...