共用方式為


作法:在 IIS 中裝載 WCF 服務

本主題概述需要進行哪些基本步驟,才能建立裝載在 Internet Information Services (IIS) 中的 Windows Communication Foundation (WCF) 服務。 本主題假設您熟悉 IIS,而且了解如何使用 IIS 管理工具建立與管理 IIS 應用程式。 如需 IIS 的詳細資訊,請參閱 Internet Information Services。 在 IIS 環境中執行的 WCF 服務能夠充分善用 IIS 功能,例如處理序回收、閒置關機、處理序健康狀態監控,以及訊息啟動。 這個裝載選項要求必須正確設定 IIS,但不要求您將任何裝載程式碼撰寫為應用程式的一部分。 IIS 裝載只能和 HTTP 傳輸一起使用。

如需 WCF 和 ASP.NET 互動方式的詳細資訊,請參閱 WCF 服務和 ASP.NET。 如需如何設定安全性的詳細資訊,請參閱安全性

如需此範例的來源複本,請參閱使用內嵌程式碼裝載 IIS

若要建立 IIS 裝載的服務

  1. 確認您的電腦上已安裝 IIS 且正在執行中。 如需如何安裝與設定 IIS 的詳細資訊,請參閱安裝與設定 IIS 7.0

  2. 為您的應用程式檔案建立稱為 "IISHostedCalcService" 新的資料夾,確保 ASP.NET 能夠存取資料夾內容,然後使用 IIS 管理工具來建立實際位於此應用程式目錄中的新 IIS 應用程式。 建立應用程式目錄的別名時,請使用 "IISHostedCalc"。

  3. 在應用程式目錄中建立名為 "service.svc" 的新檔案。 加入下列 @ServiceHost 項目以編輯此檔案。

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

  5. 在 App_Code 子目錄中建立名為 Service.cs 的程式碼檔案。

  6. 將下列 using 指示詞新增至Service.cs檔案頂端。

    using System;
    using System.ServiceModel;
    
  7. 在指示詞後面 using 新增下列命名空間宣告。

    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);
    }
    
    
    <ServiceContract()> _
    Public Interface ICalculator
    
        <OperationContract()> _
        Function Add(ByVal n1 As Double, _
                       ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Divide(ByVal n1 As Double, _
                        ByVal n2 As Double) As Double
    End Interface
    
    
  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;
       }
    }
    
    Public Class CalculatorService
        Implements ICalculator
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2
        End Function
    
        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2
        End Function
    
        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function
    
        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2
        End Function
    End Class
    
    
  10. 在應用程式目錄中建立名為 "Web.config" 的檔案,並將下列組態程式碼加入至該檔案中。 在執行階段,WCF 基礎結構會使用這項資訊以建構用戶端應用程式可通訊的端點。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors">
    
            <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc -->
            <endpoint address=""
                      binding="wsHttpBinding"
                      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>
    
        <behaviors>
          <serviceBehaviors>
            <behavior name="CalculatorServiceBehaviors">
              <!-- Add the following element to your service behavior configuration. -->
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    
    </configuration>
    

    此範例會在組態檔中明確地指定端點。 如果您沒有將任何端點加入至服務中,執行階段會為您加入預設端點。 如需預設端點、繫結和行為的詳細資訊,請參閱簡化的組態WCF 服務的簡化組態

  11. 若要確認服務裝載正確,請開啟瀏覽器,然後瀏覽到服務的 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;
     }
  }
}

Imports System.ServiceModel

Namespace Microsoft.ServiceModel.Samples

    <ServiceContract()> _
    Public Interface ICalculator

        <OperationContract()> _
        Function Add(ByVal n1 As Double, _
                       ByVal n2 As Double) As Double

        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double

        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double

        <OperationContract()> _
        Function Divide(ByVal n1 As Double, _
                        ByVal n2 As Double) As Double
    End Interface


    Public Class CalculatorService
        Implements ICalculator
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2
        End Function

        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2
        End Function

        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function

        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2
        End Function
    End Class

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors">

        <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  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>

    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehaviors">
          <!-- Add the following element to your service behavior configuration. -->
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

另請參閱