在 IIS 中承載 WCF 服務

本主題概述了建立 Internet 信息服務 (IIS) 中承載的 Windows Communication Foundation (WCF) 服務所需的基本步驟。 本主題假設您熟悉 IIS 且瞭解如何使用 IIS 管理工具建立和管理 IIS 應用程序。 有關如下內容的詳細信息請參閱 IIS Internet Information Services AWCF在 IIS 環境中運行的服務充分利用 IIS 功能,如進程回收、 空閒關閉、 進程情況監視和基於消息的激活。 此宿主選項要求正確配置 IIS,但不須要編寫任何承載代碼做爲應用程序的一部分。 只能夠將 IIS 宿主與 HTTP 傳輸協議一塊兒使用。c#

有關如下內容的詳細信息如何WCF和ASP.NET進行交互,請參閱WCF 服務和 ASP.NET。 有關如下內容的詳細信息配置安全性,請參閱安全安全

此示例的源副本,請參閱IIS 承載使用內聯代碼session

建立由 IIS 承載的服務

  1. 確認 IIS 已經安裝並在計算機上運行。 有關如下內容的詳細信息安裝和配置 IIS,請參閱安裝和配置 IIS 7.0ide

  2. 爲應用程序文件建立一個稱爲「IISHostedCalcService」的新文件夾,確保 ASP.NET 有權訪問該文件夾的內容,並使用 IIS 管理工具建立一個物理上位於此應用程序目錄中的新 IIS 應用程序。 當爲應用程序目錄建立別名時,請使用「IISHostedCalc」。工具

  3. 在應用程序目錄中建立一個名爲「service.svc」的新文件。 編輯此文件添加如下代碼@ServiceHost元素。ui

    <%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>  
    
  4. 在應用程序目錄中建立 App_Code 子目錄。spa

  5. 在 App_Code 子目錄中建立名爲 Service.cs 的代碼文件。debug

  6. 將下面的 using 語句添加到 Service.cs 文件的最前面。code

    using System;  
    using System.ServiceModel;  
    
  7. 將下面的命名空間聲明添加到 using 語句後面。orm

    namespace Microsoft.ServiceModel.Samples  
    {  
    }  
    
  8. 定義命名空間聲明中的服務協定,以下面的代碼所示。

     
      [ServiceContract]
      public interface ICalculator
      {
         [OperationContract]
         double Add(double n1, double n2);
         [OperationContract]
         double Subtract(double n1, double n2);
         [OperationContract]
         double Multiply(double n1, double n2);
         [OperationContract]
         double Divide(double n1, double n2);
      }
    
  9. 在服務協定定義後實現服務協定,以下面的代碼所示。

     
      public class CalculatorService : ICalculator
      {
         public double Add(double n1, double n2)
         {
            return n1 + n2;
         }
         public double Subtract(double n1, double n2)
         {
            return n1 - n2;
         }
         public double Multiply(double n1, double n2)
         {
            return n1 * n2;
         }
         public double Divide(double n1, double n2)
         {
            return n1 / n2;
         }
      } 
    
  10. 在應用程序目錄中建立一個名爲「Web.config」的文件,並將下面的配置代碼添加到該文件中。 在運行時,WCF 基礎結構使用這些信息來構造客戶端應用程序可與其通訊的終結點。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service 
              name="Microsoft.ServiceModel.Samples.CalculatorService"
              behaviorConfiguration="CalculatorServiceBehavior">
            <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc  -->
            <!-- specify customBinding binding and a binding configuration to use -->
            <endpoint address=""
                      binding="customBinding"
                      bindingConfiguration="Binding1" 
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />
            <!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->
            <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>
    
        <!-- custom binding configuration - configures HTTP transport, reliable sessions -->
        <bindings>
          <customBinding>
            <binding name="Binding1">
              <reliableSession />
              <security authenticationMode="SecureConversation"
                         requireSecurityContextCancellation="true">
              </security>
              <compositeDuplex />
              <oneWay />
              <textMessageEncoding messageVersion="Soap12WSAddressing10" writeEncoding="utf-8" />
              <httpTransport authenticationScheme="Anonymous" bypassProxyOnLocal="false"
                            hostNameComparisonMode="StrongWildcard" 
                            proxyAuthenticationScheme="Anonymous" realm="" 
                            useDefaultWebProxy="true" />
            </binding>
          </customBinding>
        </bindings>
    
        <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
        <behaviors>
          <serviceBehaviors>
            <behavior name="CalculatorServiceBehavior">
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

    此示例顯式指定配置文件中的終結點。 若是您不但願向服務添加任何終結點,則運行時爲您添加默認終結點。 有關如下內容的詳細信息默認終結點、 綁定和行爲,請參閱簡化配置WCF 服務的簡化配置

  11. 爲了確保正確承載該服務,請打開 Internet Explorer 的實例,導航到該服務的 URL:http://localhost/IISHostedCalc/Service.svc,若是一切正常會以下圖:

示例

下面是 IIS 承載的計算器服務的代碼的完整列表。

using System;
using System.ServiceModel;

namespace Microsoft.ServiceModel.Samples
{

  [ServiceContract]
  public interface ICalculator
  {
     [OperationContract]
     double Add(double n1, double n2);
     [OperationContract]
     double Subtract(double n1, double n2);
     [OperationContract]
     double Multiply(double n1, double n2);
     [OperationContract]
     double Divide(double n1, double n2);
  }


  public class CalculatorService : ICalculator
  {
     public double Add(double n1, double n2)
     {
        return n1 + n2;
     }
     public double Subtract(double n1, double n2)
     {
        return n1 - n2;
     }
     public double Multiply(double n1, double n2)
     {
        return n1 * n2;
     }
     public double Divide(double n1, double n2)
     {
        return n1 / n2;
     }
  } 
相關文章
相關標籤/搜索