{"id":20382889,"url":"https://github.com/zfb132/wcfservicetest","last_synced_at":"2026-05-29T21:31:05.658Z","repository":{"id":130412871,"uuid":"351719222","full_name":"zfb132/WcfServiceTest","owner":"zfb132","description":"学习创建使用Windows Communication Foundation项目","archived":false,"fork":false,"pushed_at":"2021-03-26T08:59:12.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-15T07:57:25.223Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zfb132.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-26T08:54:54.000Z","updated_at":"2021-03-26T08:59:14.000Z","dependencies_parsed_at":"2024-03-13T14:50:08.644Z","dependency_job_id":null,"html_url":"https://github.com/zfb132/WcfServiceTest","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"7a7fd9d13d89acd8166a3dc37a1c77909d60b38e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfb132%2FWcfServiceTest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfb132%2FWcfServiceTest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfb132%2FWcfServiceTest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfb132%2FWcfServiceTest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zfb132","download_url":"https://codeload.github.com/zfb132/WcfServiceTest/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241935269,"owners_count":20044826,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T02:19:26.820Z","updated_at":"2025-12-02T23:03:06.534Z","avatar_url":"https://github.com/zfb132.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 教程\n[官方中文教程](https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/getting-started-tutorial)  \n\n按顺序介绍  \n* 整个解决方案名称为`WcfServiceTest`  \n* 服务协定的项目名称为`WcfServiceLibrary`  \n* 托管服务的控制台应用程序项目名称为`ConsoleAppHost`  \n* WCF客户端的控制台应用程序项目名称为`ConsoleAppClient`\n\n### 1. 定义、实现和配置服务协定\n在解决方案`WcfServiceTest`中创建名为`WcfServiceLibrary`的`WCF 服务库`项目  \n其中  \n* `IService1.cs`文件包含服务协定的默认定义\n* `Service1.cs`文件包含服务协定的默认实现\n* `App.config`文件包含用 VISUAL Studio WCF 服务主机工具加载默认服务所需的配置信息\n\n### 2. 托管和运行基本服务\n#### 2.1 创建和配置用于托管服务的控制台应用程序项目  \n1. 在解决方案`WcfServiceTest`中创建名为`ConsoleAppHost`的`控制台应用 (.NET Framework)`项目\n2. 在`ConsoleAppHost`项目，添加引用\n    * 项目 --\u003e 解决方案 --\u003e `WcfServiceLibrary`\n    * 程序集 --\u003e 框架 --\u003e `System.ServiceModel`\n\n\n#### 2.2 添加用于托管服务的代码  \n1. 创建类的实例 Uri 以承载服务的基址，基址的格式如下所示：  \n`\u003ctransport\u003e://\u003cmachine-name or domain\u003e\u003c:optional port #\u003e/\u003coptional URI segment\u003e`  \n计算器服务的基址使用`HTTP`传输、机器名（域名或ip）`localhost`、端口`8000`和`URI`段`WcfServiceTest`\n2. 创建用于`ServiceHost`承载服务的类的实例。 构造函数采用两个参数：实现服务协定的类的类型和服务的基址\n3. 创建`ServiceEndpoint`实例。服务终结点由地址、绑定和服务协定组成，它的构造函数由服务协定接口类型、绑定和地址组成。服务协定是在服务类型中定义和实现的 `ICalculator`；此示例的绑定为`WSHttpBinding`，它是内置绑定并连接到符合`WS-*`规范的终结点；将地址追加到基址以标识终结点。该代码将地址指定为`CalculatorService`，将终结点的完全限定地址指定为 `http://localhost:8000/WcfServiceTest/CalculatorService/`\n4. 启用元数据交换。客户端使用元数据交换生成代理来调用服务操作。若要启用元数据交换，需创建一个`ServiceMetadataBehavior`实例，将其`HttpGetEnabled`属性设置为`true`，然后将该`ServiceMetadataBehavior`对象添加到实例的集合中`Behaviors ServiceHost`\n5. 打开`ServiceHost`以侦听传入的消息。应用程序等待用户按`enter`。在应用程序实例化后`ServiceHost`，它将执行`try/catch`块\n\n**注意**：添加`WCF`服务库时，如果你通过启动服务主机进行调试，则`Visual Studio`会为你托管它。若要避免冲突，可以阻止`Visual Studio`承载`WCF`服务库  \n* 在解决方案资源管理器中选择`WcfServiceLibrary`项目，然后从快捷菜单中选择`属性`。\n* 选择`Wcf 选项`并取消选中`在调试同一解决方案中的另一个项目时启动 WCF 服务主机`\n\n#### 2.3 验证服务是否正常运行  \n#### 2.4 服务托管计划步骤  \n\n### 3. 创建WCF客户端\n通过从 WCF 服务中检索元数据来创建客户端，使用 Visual Studio 添加服务引用，后者从服务的 MEX 终结点中获取元数据。 然后，Visual Studio 将使用你选择的语言生成客户端代理的托管源代码文件。 它还会创建一个客户端配置文件（App.config）。 此文件使客户端应用程序能够连接到终结点上的服务\n#### 3.1 创建并配置 WCF 客户端的控制台应用程序项目\n1. 在解决方案`WcfServiceTest`中创建名为`ConsoleAppClient`的`控制台应用 (.NET Framework)`项目\n2. 在`ConsoleAppClient`项目，添加引用\n    * 程序集 --\u003e 框架 --\u003e `System.ServiceModel`\n\n3. 在`ConsoleAppClient`项目，添加服务引用：点击`发现`，选择`CalculatorService`，保留默认生成的命名空间，选择`确定`\n\n#### 3.2 核对客户端配置文件\n`ConsoleAppClient`项目的`App.config`文件，一般不用修改，核对下即可\n\n### 4. 使用WCF客户端\n`ConsoleAppClient`项目的`Program.cs`文件的`using ConsoleAppClient.ServiceReference1;`代码的功能  \n导入`Visual Studio`生成的带有添加服务引用函数的代码。此代码实例化`WCF 代理`，并调用计算器服务公开的每个服务操作。然后，它会关闭代理并结束程序\n\n### 5. 测试WCF客户端\n保存并生成解决方案，然后运行测试，有两种方法：  \n* 从 Visual Studio 测试应用程序\n    * 选择`WcfServiceLibrary`项目，然后从快捷菜单中选择`设为启动项目`。\n    * 从`启动项目`中，从下拉列表中选择`WcfServiceLibrary`，然后选择`运行`或按`F5`\n\n* 在命令提示符下测试应用程序\n    * 以管理员身份打开命令提示符，然后导航到解决方案目录\n    * 若要启动服务：输入  \n    `ConsoleAppHost\\bin\\Debug\\ConsoleAppHost.exe`  \n    * 若要启动客户端，则打开另一个命令提示符，导航到解决方案目录，然后输入  \n    `ConsoleAppClient\\bin\\Debug\\ConsoleAppClient.exe`\n\n输出示例：  \n`ConsoleAppHost.exe`的输出  \n```txt\nThe service is ready.\nPress \u003cEnter\u003e to terminate the service.\n\nReceived Add(100,15.99)\nReturn: 115.99\nReceived Subtract(145,76.54)\nReturn: 68.46\nReceived Multiply(9,81.25)\nReturn: 731.25\nReceived Divide(22,7)\nReturn: 3.14285714285714\n```\n`ConsoleAppClient.exe`的输出  \n```txt\nAdd(100,15.99) = 115.99\nSubtract(145,76.54) = 68.46\nMultiply(9,81.25) = 731.25\nDivide(22,7) = 3.14285714285714\n\nPress \u003cEnter\u003e to terminate the client.\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfb132%2Fwcfservicetest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzfb132%2Fwcfservicetest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfb132%2Fwcfservicetest/lists"}