65.9K
CodeProject 正在变化。 阅读更多。
Home

IIS 中的 WCF 托管已简化

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2投票s)

2014年6月23日

CPOL

3分钟阅读

viewsIcon

11132

IIS 中的 WCF 托管已简化

这是 WCF 托管教程系列的第 3 部分,我们将实现如何在 IIS(版本 5 或 6)中托管 WCF 服务? 在本系列中,我们已经实现了以下内容:

Microsoft 在 .NET Framework v 3.0 中引入了 WCF(Windows Communication Foundation),具有许多令人兴奋的功能,但其中最令人兴奋的功能是它与 ASMX 服务ASP.NET Web Services 有何不同? 或者与 ASMX 服务相比,它提供了哪些额外的功能?

主要区别在于,ASMX 或 ASP.NET Web Service 旨在仅通过 HTTP 使用 SOAP 发送和接收消息。 而 WCF 可以使用任何格式(默认情况下为 SOAP)通过任何传输协议(HTTP、TCP/IP、MSMQ、命名管道等)交换消息。 另一篇 WCF 教程详细讨论了 WCF 与 ASMX 的区别,请点击此处查看。

因此,如果我们要在 IIS(版本 5 或 6)中托管 WCF 服务,则 HTTP 选项是唯一的可用传输协议。 在较新版本的 IIS(Internet Information Services)中,我们可以使用任何其他协议(TCP/IP、MSMQ、命名管道等)作为传输协议。

WCF RESTful 服务的实用指南

正如我们已经知道的,为了托管 WCF 服务,我们需要一个托管进程。 在 IIS(Internet Information Services)的情况下,它提供主机进程,并在第一个请求到达服务器时启动该进程。

现在,让我们首先创建一个 WCF 服务,然后将其托管在 IIS 中。

创建一个 StudentService 类库

我们在实现 WCF 自托管教程 时,已经创建了一个 StudentService 类库项目。 为了保持一致性,我们将使用相同的服务,因此请参阅该 WCF 教程 的步骤 1 以创建 WCF 服务。

在 IIS 中托管 WCF

因此,为了在 IIS 中托管我们的 WCF 服务,请按照简单的分步方法操作。

  1. 在解决方案中添加一个网站。 选择“ASP.NET 空白网站”模板。 对于 Web 位置,选择“HTTP”而不是“文件系统”,提供路径并按“确定”按钮。

    WCF Hosting in IIS

  2. StudentService 项目的引用添加到我们的网站,即 StudentIISHost

    WCF Service Reference

  3. System.ServiceModel 的引用添加到网站。
  4. 将一个新的 .svc 文件添加到我们的网站项目,如下所示:

    WCF Service

    WCF Service Host

    在上面的代码中,我们在 ServiceHost 中将 Service 指向 StudentService

  5. web.configSystem.ServiceModel 的更新配置如下:
      <system.serviceModel>
          <behaviors>
               <serviceBehaviors>
                      <behavior name=”StudentServiceBehavior”>
                                 <serviceMetadata httpGetEnabled=”true”/>
                                 <serviceDebug includeExceptionDetailInFaults=”false”/>
                      </behavior>
               </serviceBehaviors>
          </behaviors>
          <services>
                      <service behaviorConfiguration=”StudentServiceBehavior”
                                   name=”StudentService.StudentService”>
                                  <endpoint address=”https:///StudentIISHost/MyStudentHost.svc”
                                   binding=”wsHttpBinding” 
    
                                                         contract=”StudentService.IStudentService”>
                                           <identity>
                                                     <dns value=”localhost”/>
                                           </identity>
                                  </endpoint>
                                 <endpoint address=”mex” 
    
                                                        binding=”mexHttpBinding” 
    
                                                        contract=”IMetadataExchange”/>
                      </service>
           </services>
       </system.serviceModel>
  6. 使用以下路径访问托管的 WCF 服务,即 StudentService
    https:///StudentIISHost/MyStudentHost.svc

    WCF Service Running

希望本 WCF 教程能帮助您理解在 IIS(Internet Information Service)中托管 WCF 服务的实现。

其他相关文章

文章 IIS 中简化的 WCF 托管 首先发表在 WCF 教程 上。

© . All rights reserved.