WCF学习笔记(2)-WCF的通讯过程

一、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>
原文地址:https://www.cnblogs.com/kimisme/p/5399687.html