写在前面
接触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 | //第一种方式stringurl ="http://localhost:3000/DoubleService.svc";IDoubleService proxy = WcfInvokeFactory.CreateServiceByUrl<IDoubleService>(url);intresult = 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><endpoint address="http://localhost:3000/DoubleService.svc"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 | publicclassWcfInvokeFactory{#region WCF服务工厂publicstaticT CreateServiceByUrl<T>(stringurl){returnCreateServiceByUrl<T>(url,"basicHttpBinding");}publicstaticT CreateServiceByUrl<T>(stringurl,stringbing){try{if(string.IsNullOrEmpty(url))thrownewNotSupportedException("This url is not Null or Empty!");EndpointAddress address =newEndpointAddress(url);Binding binding = CreateBinding(bing);ChannelFactory<T> factory =newChannelFactory<T>(binding, address);returnfactory.CreateChannel();}catch(Exception ex){thrownewException("创建服务工厂出现异常.");}}#endregion#region 创建传输协议/// <summary>/// 创建传输协议/// </summary>/// <param name="binding">传输协议名称</param>/// <returns></returns>privatestaticBinding CreateBinding(stringbinding){Binding bindinginstance =null;if(binding.ToLower() =="basichttpbinding"){BasicHttpBinding ws =newBasicHttpBinding();ws.MaxBufferSize = 2147483647;ws.MaxBufferPoolSize = 2147483647;ws.MaxReceivedMessageSize = 2147483647;ws.ReaderQuotas.MaxStringContentLength = 2147483647;ws.CloseTimeout =newTimeSpan(0, 30, 0);ws.OpenTimeout =newTimeSpan(0, 30, 0);ws.ReceiveTimeout =newTimeSpan(0, 30, 0);ws.SendTimeout =newTimeSpan(0, 30, 0);bindinginstance = ws;}elseif(binding.ToLower() =="nettcpbinding"){NetTcpBinding ws =newNetTcpBinding();ws.MaxReceivedMessageSize = 65535000;ws.Security.Mode = SecurityMode.None;bindinginstance = ws;}elseif(binding.ToLower() =="wshttpbinding"){WSHttpBinding ws =newWSHttpBinding(SecurityMode.None);ws.MaxReceivedMessageSize = 65535000;ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;bindinginstance = ws;}returnbindinginstance;}#endregion} |
第二种调用方式
1 2 3 4 5 6 7 8 9 10 知识推荐
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8
不良信息举报平台
互联网安全管理备案
Copyright 2023 www.wodecom.cn All Rights Reserved |