WCF学习笔记(一) 之 开门见山
WCF由 .NET Framework 3.0 (大概在07年前后)开始引入,时隔五年多,才开始学习,再看到一些大牛在几年前已经 对WCF有比较深入了解,并写了不少博客,顿感学习之迟钝、技术之落伍——这其中有些人可能是对新技术的狂热和兴趣,接触和了解的 比较早,有些人只会等需要用到此新技术时才会去学习,像我。转入正题,正如这篇博客的标题一样,我将会以此篇博客为开端,不断跟大家分享我学习WCF的"所得"——即我眼中的WCF,需要强调的是:我肯定不是权威,我只想把我所知道的用比较通俗的语言写出来,让复杂的东西变的简单易懂点儿,期望每篇博客至少会让你有一点儿"收获",这其中难免会有理解错误的地方,希望大家能提出来"指正",不拍被拍砖, 但拍砖请友善,因为大家的支持和建议——将是推动我更好更多的写"WCF学习笔记"此系列的博客的强大动力!
本文目录 :
WCF简介和由来 WCF基础关键点(核心) 创建一个WCF服务(HelloWorld WCF)WCF简介和由来
WCF(Windows Communication Foundation)是基于Windows平台下开发和部署服务的软件开发包。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案,且能与已有系统兼容协作。WCF是微软分布式应用程序开发的集大成者,它整合了.Net平台下所有的和分布式系统有关的技术,例如.Net Remoting、ASMX、WSE和MSMQ。以通信(Communiation)范围而论,它可以跨进程、跨机器、跨子网、企业网乃至于 Internet;以宿主程序而论,可以以ASP.NET,EXE,WPF,Windows Forms,NT Service,COM+作为宿主(Host)。WCF可以支持的协议包括TCP,HTTP,跨进程以及自定义,安全模式则包括SAML, Kerberos,X509,用户/密码,自定义等多种标准与模式。也就是说,在WCF框架下,开发基于SOA的分布式系统变得容易,微软将所有与此相关的技术要素都包含在内,掌握了WCF,就相当于掌握了叩开SOA大门的钥匙。
随着 SOP 和 SOA 的思想越来越成为潮流,也是现今为了提供分布式部署、高性能和可扩展性的软件开发的必然趋势,WCF将会在以后的.net舞台上扮演举足轻重的角色!
WCF基础关键点(核心)
如图:
用用例图来描述WCF的核心——最直接的原因是:我感觉这样更方便、直观,以下具体说明:
服务寄宿——首先,‘寄宿’一词可能就会让有些人感觉有些模糊难懂,我们可以将其理解为"托管"(在.net中‘托管’一词想必大家不会陌生),就是说WCF服务不能独立存在,必须存在(寄宿或托管)于某个进程中。
终结点:描述了一个服务的地址、绑定类型(是http还是tcp等)、服务契约(服务约定接口)。
创建一个WCF服务(HelloWorld WCF)—— IIS寄宿WCF服务
本示例采用的是 IIS寄宿 ,并介绍http(basicHttpBinding)和tcp(netTcpBinding)两种绑定类型终结点的服务引用!
1 .创建WCF服务端 ,如图:
2. 创建具体的WCF服务 ,如图:
添加后会在项目中增加Service1.svc和 服务契约 IService1.cs接口文件,如图:
服务契约 IService1接口的实现类Service1就是 服务类型, 代码如下图:
3. 在IIS中寄宿WCF服务
a.为了便于测试,在本地host中配置:127.0.0.1 www.wcf.dev
b.在IIS中添加服务网站,并指向前面创建好的WCF服务端
c.为了让此WCF服务支持NetTcpBinding绑定,需如下图设置:
设置当前WCF服务网站可以被访问的协议类型为:http,net.tcp
4. 创建WCF服务客户端
服务客户端基本上不受什么限制,可以灵活的选择web、WPF/WinForm或控制台项目,本示例选用的web项目作为服务客户端。服务引用如下图:
引用服务后,会自动在Web.config中添加如下服务引用配置
< system.webServer > < modules runAllManagedModulesForAllRequests ="true" /> </ system.webServer > < system.serviceModel > < bindings > < basicHttpBinding > < binding name ="BasicHttpBinding_IService1" closeTimeout ="00:01:00" openTimeout ="00:01:00" receiveTimeout ="00:10:00" sendTimeout ="00:01:00" allowCookies ="false" bypassProxyOnLocal ="false" hostNameComparisonMode ="StrongWildcard" maxBufferSize ="65536" maxBufferPoolSize ="524288" maxReceivedMessageSize ="65536" messageEncoding ="Text" textEncoding ="utf-8" transferMode ="Buffered" useDefaultWebProxy ="true" > < readerQuotas maxDepth ="32" maxStringContentLength ="8192" maxArrayLength ="16384" maxBytesPerRead ="4096" maxNameTableCharCount ="16384" /> < security mode ="None" > < transport clientCredentialType ="None" proxyCredentialType ="None" realm ="" /> < message clientCredentialType ="UserName" algorithmSuite ="Default" /> </ security > </ binding > </ basicHttpBinding > < netTcpBinding > < binding name ="NetTcpBinding_IService1" closeTimeout ="00:01:00" openTimeout ="00:01:00" receiveTimeout ="00:10:00" sendTimeout ="00:01:00" transactionFlow ="false" transferMode ="Buffered" transactionProtocol ="OleTransactions" hostNameComparisonMode ="StrongWildcard" listenBacklog ="10" maxBufferPoolSize ="524288" maxBufferSize ="65536" maxConnections ="10" maxReceivedMessageSize ="65536" > < readerQuotas maxDepth ="32" maxStringContentLength ="8192" maxArrayLength ="16384" maxBytesPerRead ="4096" maxNameTableCharCount ="16384" /> < reliableSession ordered ="true" inactivityTimeout ="00:10:00" enabled ="false" /> < security mode ="Transport" > < transport clientCredentialType ="Windows" protectionLevel ="EncryptAndSign" /> < message clientCredentialType ="Windows" /> </ security > </ binding > </ netTcpBinding > </ bindings > < client > < endpoint address ="http://www.wcf.dev/Service1.svc" binding ="basicHttpBinding" bindingConfiguration ="BasicHttpBinding_IService1" contract ="MyService.IService1" name ="BasicHttpBinding_IService1" /> < endpoint address ="net.tcp://127.0.0.1:808/Service1.svc" binding ="netTcpBinding" bindingConfiguration ="NetTcpBinding_IService1" contract ="MyService.IService1" name ="NetTcpBinding_IService2" > </ endpoint > < endpoint address ="net.tcp://127.0.0.1/Service1.svc" binding ="netTcpBinding" bindingConfiguration ="NetTcpBinding_IService1" contract ="MyService.IService1" name ="NetTcpBinding_IService1" > </ endpoint > </ client > </ system.serviceModel >
到这里,服务已可以在客户端调用,代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.ServiceModel; 8 9 namespace Web_Client 10 { 11 public partial class _Default : System.Web.UI.Page 12 { 13 protected void Page_Load( object sender, EventArgs e) 14 { 15 #region regName 16 // MyService.Service1Client client = new MyService.Service1Client("NetTcpBinding_IService1"); 17 // Response.Write(client.GetData(123)); 18 // string str = client.GetColorName(MyService.Color.Orange); 19 // Response.Write("<br/>" + str); 20 #endregion 21 Test( " BasicHttpBinding_IService1 " ); 22 Test( " NetTcpBinding_IService1 " ); 23 } 24 25 private void Test( string endpointConfigurationName) 26 { 27 using (ChannelFactory<MyService.IService1> channelFactory = new ChannelFactory<MyService.IService1> (endpointConfigurationName)) 28 { 29 Response.Write( " <br/>------------endpointConfigurationName is : " + channelFactory.Endpoint.Name); 30 Response.Write( " <br/>------------endpoint Address is : " + channelFactory.Endpoint.Address); 31 MyService.IService1 calculator = channelFactory.CreateChannel(); 32 Response.Write( " <br/> " + calculator.GetData(DateTime.Today.Year)); 33 MyService.CompositeType compositeType = calculator.GetDataUsingDataContract( new MyService.CompositeType() { BoolValue = true , StringValue = " This Is Test, " + DateTime.Now.ToString() }); 34 Response.Write( " <br/> " + compositeType.StringValue); 35 } 36 } 37 } 38 }
测试结果如下图:
好了,这篇博客就写到这儿,如果有不清楚的地方,可以给我留言!
文后附上本文的Demo: wcfDemo_Basic.rar
分类: WCF
标签: IIS寄宿WCF服务 , WCF教程 , WCF核心 , WCF绑定tcp协议 , WCF学习笔记 , WCF开发经验
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于WCF学习笔记(一) 之 开门见山的详细内容...