WCF学习笔记(2)-WCF的通讯过程
2024-09-04 17:47:24
一、WCF中的ABC
场景:公司让你送一份合同文件,送文件的过程你可以选择的交通方式有打的,地铁或公交。
到了对方公司后,你要找到某负责人,并且要一份收到合同文件的回执和相应文件
要完成这项工作任务主要以下几个步骤
1.知道对方公司的地址
即WCF中的A,通过Address我们可以找到我们要调用的WCF服务
2.选择交通方式
即WCF中的B,通过Binding来实现Client和Service通讯的所有底层细节。
传输采用什么编码,传输使用什么协议,等等
3.到了对方公司我们能做哪些事
即WCF中的C,通过Contract我们知道了哪些事我们能做,那些事我们不能做
二、Endpoint(终结点)
WCF实现了网络系统的各个应用程序的通信。各个应用程序的通信是以”终结点”来实现的;
上面例子中的ABC即是Endpoint的组成部分,它是服务器通信调用的入口;
只有当Client与Service的终结点完全匹配的时候才能进行通信
三、代码分析
上一篇中在客户端和服务端都生成了两个配置文件
客户端配置
<?xml version="1.0" encoding="utf-8"?> <!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web> <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IUser" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:9003/User.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IUser" contract="WCFService.IUser"
name="BasicHttpBinding_IUser" />
</client>
</system.serviceModel>
</configuration>
服务器配置
<?xml version="1.0" encoding="utf-8"?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings> <system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web> <system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/>
</system.webServer> </configuration>
我们在服务端的system.serviceMode节点中并没有看到上面说的Endpoint是因为我们WCF寄宿在IIS上,而IIS默认监听的就是http协议(B确定了),而地址也是相对于IIS上文件的地址(A确定了),合同更不用说了,找到User.svc上面都有了(C确定了)。所以服务端就没有显示Endpint配置也能接收到客户端的信息
我们将服务端的配置文件改成如下文件,程序照样可以运行
<?xml version="1.0" encoding="utf-8"?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings> <system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web> <system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<!--这是新加的代码-->
<client>
<endpoint address="http://localhost:9003/User.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IUser" contract="WCFService.IUser"
name="BasicHttpBinding_IUser" />
</client>
</system.serviceModel> <system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/>
</system.webServer> </configuration>
最新文章
- Google C++单元测试框架---Gtest框架简介(译文)
- urlparse
- 计算c字符的长度,保证不超过2^30
- 夺命雷公狗—angularjs—13—post参数的接收发送
- mysql 字段编码该为utf8mb4
- 邦定lua函数到C里做为回调
- 【英语】Bingo口语笔记(48) - 关于春节的表达
- Create CSS3 Buttons Compatible with All Browsers
- 看原理图之UART
- 03_MySQL中文乱码处理_02_确保MySQL插入数据不乱码的解决方法
- git 401 错误
- HTTP Authorization
- Java基础—ClassLoader的理解(转)
- KMP算法中的next数组求解示意图
- python变量字符拼接
- flv文件解析(纯c解析代码)
- Linux 下 pushd,popd,cd- 用法
- 剑指Offer_编程题_17
- modsign: could&#39;t get uefi db list
- Linux内核分析 期末总结
热门文章
- noip 2011
- HashSet源码分析1
- Orcle定时生成表数据作业
- 在ASP.NET Core 中使用Cookie中间件 (.net core 1.x适用)
- 对一个deb包的解压、改动、又一次打包全过程方法
- Java String常见问题
- document.getElementsByClassName(";head";)[0].parentElement
- Git经常使用命令总结
- sa分析
- bzoj5311: 贞鱼