WCF编程初识WCF
2013-04-17 14:44 Evan.Pei 阅读(248) 评论(0) 编辑 收藏 举报Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程模型。
WCF的基本概念:
地址:定义服务的地址 绑定:定义服务的通讯方式(传输协议、编码方案)
契约:定义服务的具体实现
终结点:由地址、绑定和契约共同构成一个终结点,服务器通过终结点向客户端公开服务,客户端通过终结点调用服务。
下面通过一个简单的服务示例来认识WCF(只需让本例顺利运行即可,关于代码中的各种类型及WCF的相关概念我们将在后续介绍):
1.新建项目,名称 XfrogWCFService,解决方案名称 XfrogWCFStudy001,模板选择类库,选择.NET Framework 3.0版本
2.修改Class1.cs文件名称为 IFirstService.cs
3.添加引用 System.ServiceModel
4.修改IFirstService.cs代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace XfrogWCFService { /// <summary> /// 注意在接口上申明了ServiceContract特性,即服务契约,表明该接口是一个服务 /// </summary> [ServiceContract] public interface IFirstService { /// <summary> /// 表示该方法是IFirstService的一个服务方法,客户端可远程调用该方法。 /// </summary> /// <param name="name"></param> /// <returns></returns> [OperationContract] String GetData(String name); } } |
我们定义了一个IFirstService接口,注意在接口上申明了ServiceContract特性,即服务契约,表明该接口是一个服务。方法上声明了OperationContract特性,表示该方法是IFirstService的一个服务方法,客户端可远程调用该方法。
5.添加一个新类,文件名为FirstService.cs,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System; using System.Collections.Generic; using System.Text; namespace XfrogWCFService { public class FirstService : IFirstService { string IFirstService.GetData(String name) { return String.Format( "Hello,Baron {0},Welcome To WCF!" , name); } } } |
OK,到此我们的服务代码已经编写完成,下面我们必须为服务提供一个运行的宿主,通过该宿主程序来启动我们的服务。
6.在同一解决方案下新建一个项目,名称为Host,类型为控制台应用程序
7.Host项目中添加引用,引用项目XfrogWCFService,然后再添加引用:System.ServiceModel
8.修改Program.cs代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using XfrogWCFService; namespace Host { class Program { static void Main( string [] args) { using (ServiceHost host = new ServiceHost( typeof (FirstService))) { host.Open(); Console.WriteLine( "服务已启动,按任意键中止..." ); Console.ReadKey( true ); host.Close(); } } } } |
以上,我们已经实现了服务以及为服务提供了一个运行宿主,即契约部分已经完成,下面我们为服务指定地址及绑定,本步骤可通过WCF的管理工具,
或直接编写配置文件来完成。我们先采用手工编写配置文件的方式:
9.新建项,选择应用程序配置文件,文件名App.config保持不变。
10.修改app.config内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <? xml version="1.0" encoding="utf-8" ?> < configuration > < system.serviceModel > < services > < service name="XfrogWCFService.FirstService" behaviorConfiguration="behaviorConfiguration"> < host > < baseAddresses > < add baseAddress="http://192.168.10.58:8000/"/> </ baseAddresses > </ host > < endpoint address="http://192.168.10.58:8000/" binding="basicHttpBinding" contract="XfrogWCFService.IFirstService" name="BasicHttpBinding_IFirstService"></ endpoint > </ service > </ services > < behaviors > < serviceBehaviors > < behavior name="behaviorConfiguration"> < serviceMetadata httpGetEnabled="true"/> </ behavior > </ serviceBehaviors > </ behaviors > </ system.serviceModel > </ configuration > |
11.设置Host项目为启动项目,启动调试。控制台上显示服务已启动后,打开浏览器输入服务地址:http://localhost:8000/ ,
浏览器中会打开我们的服务页面,这表示我们的服务已经启动成功,客户端可通过该地址访问我们的服务了。 下面,我们将创建一个客户端来访问我们的服务
12.在同一解决方案下新建一个项目,名称为Client,类型为控制台应用程序
13. 我们将使用微软的svcutil工具生成FirstService服务的客户端代理类,通过开始菜单/Microsoft Visual Studio 2008/Visual Studio Tools/Visual Studio 2008命令提示,启动命令环境。
14.确认FirstService服务已启动
15.切换当前路径到解决方案目录: cd G:\Study\WCF\XfrogWCFStudy001 g:
16.输入命令: svcutil http://localhost:8000/?wsdl /o:FirstServiceClient.cs 执行成功后,会在解决方案目录下生成两个文件:FirstServiceClient.cs 和output.config
17.中止Host项目的调试,回到Client项目,选择添加 现有项 ,然后选择这两个文件,添加后,将output.config重命名为App.config 18.Client项目中添加引用,选择System.ServiceModel
19.修改program.cs代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Client1 { public class Program { static void Main( string [] args) { String key = "" ; while (String.Compare(key, "Q" , true ) != 0) { FirstServiceClient client = new FirstServiceClient(); Console.WriteLine(client.GetData(key)); key = Console.ReadLine(); } } } } |
20.Host项目上单击右键,选择调试—>启动新实例,待服务启动完成后,在Client项目上单击右键,选择调试—>启动新实例。输入任意字符回车,Client将调用FirstService服务GetData方法,返回一个字符串。输入q退出Client。
21,以下是本人的注释
21.1 Client项目单独一个解决方案,Host和XfrogWCFService 一个解决方案,这样运行起来才能看到效果。
21.2 如果要在asp.net中调用wcf服务得在web.config中添加:(也就Client项目中的App.config文件里的内容)
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 | < system.serviceModel > < bindings > < basicHttpBinding > < binding name="BasicHttpBinding_IFirstService" 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 > </ bindings > < client > < endpoint address="http://192.168.10.58:8000/" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFirstService" contract="IFirstService" name="BasicHttpBinding_IFirstService" /> </ client > </ system.serviceModel > |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构