HOW TO:在 IIS 中裝載 WCF 服務

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

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

如需這個範例的原始檔複本,請參閱使用內嵌程式碼的 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. 使用陳述式將以下加入至 Service.cs 檔案的最上方。

    using System;
    using System.ServiceModel;
    
  7. 使用陳述式之後,加入下列命名空間宣告。

    namespace Microsoft.ServiceModel.Samples
    {
    }
    
  8. 在命名空間宣告內定義服務合約,如下列程式碼所示。

    <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
    
    [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
        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
    
    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>
          <!-- This section is optional with the default configuration
            model introduced in .NET Framework 4 -->
          <service name="Microsoft.ServiceModel.Samples.CalculatorService">
    
            <!-- This endpoint is exposed at the base address provided by host:                                        https://localhost/servicemodelsamples/service.svc  -->
            <endpoint address=""
                      binding="wsHttpBinding"
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />
    
            <!-- The mex endpoint is exposed at https://localhost/servicemodelsamples/service.svc/mex -->
            <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>
      </system.serviceModel>
    
    </configuration>
    

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

  11. 若要確認服務裝載正確,請開啟 Internet Explorer 的執行個體,然後瀏覽到服務的 URL:https://localhost/IISHostedCalc/Service.svc

範例

以下是裝載於 IIS 之計算機服務的完整程式碼清單。

Imports System
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
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;
     }
  } 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Microsoft.ServiceModel.Samples.CalculatorService">

        <!-- This endpoint is exposed at the base address provided by host:                                        https://localhost/servicemodelsamples/service.svc  -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />

        <!-- The mex endpoint is explosed at https://localhost/servicemodelsamples/service.svc/mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

</configuration>

另請參閱

概念

在網際網路資訊服務中裝載
裝載服務
WCF 服務與 ASP.NET

其他資源

Windows Communication Foundation 安全性
Windows Server AppFabric 主控功能