Take a look at GW

【C#】关于WCF你需要知道的!

虽然 WCF(Windows Communication Foundation) 已经不在是C#的最新技术,但很多老项目依然在用WCF。这边文章会让你快速知道WCF相关的知识。

1. 环境

以下是我的开发环境:

Visual Studio 2017

Dotnet framework 4.8

 

记得启动Windows Features.

 

 

 

2. 一个简单的WCF案例

微软提供了完整了WCF的文档,你可以从Windows Communication Foundation (WCF) samples 中微软的文档,也可以直接跳转到文档的Getting Started Sample。一步步的建好class文件,svc文件,以及web.config文件。

 

其中Web.config文件中的 endpoint 表示的是service的进入点,这个非常重要。如果没有给service配置endpoint的话, wcf就不知道暴漏出那些svc,service也就无法访问。

 

3. 使用Visual studio创建WCF

 这里我会使用Visual Studio 来创建一个WCF程序。

 然后你会看见项目文件IService1.cs, Service1.svc, Web.config

 Service1.svc文件内容,可以看出WcfService1.Service1的CodeBehind是Service1.svc.cs

而 Service1.svc.cs 有实现自 IService1.cs

 

 要运行起来可以按下F5,便可以运行

 然后会跳出WCF Test Client ,你就可以在这里测试你的WCF程序啦。

4. 使用WcfTestClient.exe程序来测试你的程序

如果你的Visual Studio 不是2017的话,你按F5很可能打不开WCF Test Client.  这时候你可以在你本地找到WcfTestClient.exe程序,然后手动打开进行测试。可以参考 WCF Test Client (WcfTestClient.exe)   WCFTestClient 的在每个人的本地有点不同,笔者本地可以在 C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\WcfTestClient.exe  目录下找到。

 

5. 通过Visual Studio 发布的WCF 直接部署到IIS上

通过  Visual Studio 发布后的WCF,在Web.config中是没有给Service配置Endpoint的。所以托管到IIS后,默认是可以打开的,但如果不能打开,可以配置endpoint。

 发布后的文件,

打开Web.config

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3   <appSettings>
 4     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
 5   </appSettings>
 6   <system.web>
 7     <compilation targetFramework="4.8" />
 8     <httpRuntime targetFramework="4.8" />
 9   </system.web>
10   <system.serviceModel>
11     <behaviors>
12       <serviceBehaviors>
13         <behavior>
14           <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
15           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
16           <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
17           <serviceDebug includeExceptionDetailInFaults="false" />
18         </behavior>
19       </serviceBehaviors>
20     </behaviors>
21     <protocolMapping>
22       <add binding="basicHttpsBinding" scheme="https" />
23     </protocolMapping>
24     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
25   </system.serviceModel>
26   <system.webServer>
27     <modules runAllManagedModulesForAllRequests="true" />
28     <!--
29         To browse web app root directory during debugging, set the value below to true.
30         Set to false before deployment to avoid disclosing web app folder information.
31       -->
32     <directoryBrowse enabled="true" />
33   </system.webServer>
34 </configuration>
35 <!--ProjectGuid: 5E6D2C78-A51C-4AE4-9FB6-324936C66DCD-->

可以看出并没有给Service配置任何Endpoint, 部署到IS后,默认可以打开。

 如果你遇到Service无法访问,这时候可以手动给Web.config的Service加上endpoint, 显示指定让Service暴漏出去让网络可以访问。

 

 

然后需要配置Service 和 behavior

1       <service
2           name="WcfService1.IService1"
3           behaviorConfiguration="TestServiceBehavior">
4         <endpoint address="mex"
5                   binding="mexHttpBinding"
6                   contract="IMetadataExchange" />
7       </service>

需要注意IMetadadtaExchange是内置的contact类型,你可以直接使用。和hebavior

        <behavior name="TestServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>

更改后,完整的Web.config如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3   <appSettings>
 4     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
 5   </appSettings>
 6   <system.web>
 7     <compilation targetFramework="4.8" />
 8     <httpRuntime targetFramework="4.8" />
 9   </system.web>
10   <system.serviceModel>
11     <services>
12       <service
13           name="WcfService1.IService1"
14           behaviorConfiguration="TestServiceBehavior">
15         <endpoint address="mex"
16                   binding="mexHttpBinding"
17                   contract="IMetadataExchange" />
18       </service>
19     </services>
20 
21 
22     <behaviors>
23       <serviceBehaviors>
24         <behavior name="TestServiceBehavior">
25           <serviceMetadata httpGetEnabled="True"/>
26           <serviceDebug includeExceptionDetailInFaults="False" />
27         </behavior>
28 
29         <behavior>
30           <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
31           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
32           <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
33           <serviceDebug includeExceptionDetailInFaults="false" />
34         </behavior>
35       </serviceBehaviors>
36     </behaviors>
37 
38     <protocolMapping>
39       <add binding="basicHttpsBinding" scheme="https" />
40     </protocolMapping>
41 
42     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
43 
44   </system.serviceModel>
45   <system.webServer>
46     <modules runAllManagedModulesForAllRequests="true" />
47     <!--
48         To browse web app root directory during debugging, set the value below to true.
49         Set to false before deployment to avoid disclosing web app folder information.
50       -->
51     <directoryBrowse enabled="true" />
52   </system.webServer>
53 </configuration>
54 <!--ProjectGuid: 5E6D2C78-A51C-4AE4-9FB6-324936C66DCD-->
View Code

 

posted @ 2024-04-07 15:28  HDWK  阅读(245)  评论(0编辑  收藏  举报