教學課程:定義 Windows Communication Foundation 服務合約Tutorial: Define a Windows Communication Foundation service contract

本教學課程說明建立基本 Windows Communication Foundation (WCF)應用程式所需的五個工作中的第一個。This tutorial describes the first of five tasks required to create a basic Windows Communication Foundation (WCF) application. 如需教學課程的總覽,請參閱教學課程:開始使用 Windows Communication Foundation 應用程式For an overview of the tutorials, see Tutorial: Get started with Windows Communication Foundation applications.

當您建立 WCF 服務時,您的第一項工作是定義服務合約。When you create a WCF service, your first task is to define a service contract. 服務合約會指定服務所支援的作業。The service contract specifies what operations the service supports. 作業可以視為一種 Web 服務方法。An operation can be thought of as a Web service method. 您可以藉由定義C#或 Visual Basic 介面來建立服務合約。You create service contracts by defining a C# or Visual Basic interface. 介面具有下列特性:An interface has the following characteristics:

在本教學課程中,您將了解如何:In this tutorial, you learn how to:

  • 建立WCF 服務程式庫專案。Create a WCF Service Library project.
  • 定義服務合約介面。Define a service contract interface.

建立 WCF 服務程式庫專案和定義服務合約介面Create a WCF Service Library project and define a service contract interface

  1. 以系統管理員身分開啟 Visual Studio。Open Visual Studio as an administrator. 若要這麼做,請在 [開始] 功能表中選取 [Visual Studio] 程式,然後從快捷方式功能表選取 [更多 > 以系統管理員身分執行]。To do so, select the Visual Studio program in the Start menu, and then select More > Run as administrator from the shortcut menu.

  2. 建立WCF 服務程式庫專案。Create a WCF Service Library project.

    1. 從 [檔案] 功能表選取 [新增] > [專案]。From the File menu, select New > Project.

    2. 在 [新增專案] 對話方塊左側,展開 [視覺效果C# ] 或 [ Visual Basic],然後選取 [ WCF ] 分類。In the New Project dialog, on the left-hand side, expand Visual C# or Visual Basic, and then select the WCF category. Visual Studio 會在視窗的中間區段顯示專案範本清單。Visual Studio displays a list of project templates in the center section of the window. 選取 [ WCF 服務程式庫]。Select WCF Service Library.

      注意

      如果您沒有看到 [ WCF專案範本] 類別目錄,您可能需要安裝 Visual Studio 的Windows Communication Foundation元件。If you don't see the WCF project template category, you may need to install the Windows Communication Foundation component of Visual Studio. 在 [新增專案] 對話方塊中,選取左側的 [開啟 Visual Studio 安裝程式] 連結。In the New Project dialog box, select the Open Visual Studio Installer link on the left side. 選取 [個別元件] 索引標籤,然後尋找並選取 [開發活動] 類別底下的 [ Windows Communication Foundation ]。Select the Individual components tab, and then find and select Windows Communication Foundation under the Development activities category. 選擇 [修改] 開始安裝元件。Choose Modify to begin installing the component.

    3. 在視窗的下方區段中,針對 [名稱] 輸入GettingStartedLib ,然後在 [方案名稱] 中輸入GettingStartedIn the bottom section of the window, enter GettingStartedLib for the Name and GettingStarted for the Solution name.

    4. 選取 [確定]。Select OK.

      Visual Studio 會建立專案,其中包含三個檔案: IService1.cs (或 Visual Basic 專案的IService1 )、 Service1.cs (或 Visual Basic 專案的Service1 )和app.config。Visual Studio 定義這些檔案,如下所示:Visual Studio creates the project, which has three files: IService1.cs (or IService1.vb for a Visual Basic project), Service1.cs (or Service1.vb for a Visual Basic project), and App.config. Visual Studio defines these files as follows:

      • IService1檔案包含服務合約的預設定義。The IService1 file contains the default definition of the service contract.
      • Service1檔案包含服務合約的預設執行。The Service1 file contains the default implementation of the service contract.
      • App.config檔案包含使用 Visual Studio WCF 服務主機工具載入預設服務時所需的設定資訊。The App.config file contains the configuration info needed to load the default service with the Visual Studio WCF Service Host tool. 如需 WCF 服務主機工具的詳細資訊,請參閱Wcf 服務主機(WcfSvcHost .exe)For more information about the WCF Service Host tool, see WCF Service Host (WcfSvcHost.exe).

      注意

      如果您已使用 Visual Basic 的開發人員環境設定來安裝 Visual Studio,解決方案可能會隱藏起來。If you installed Visual Studio with Visual Basic developer environment settings, the solution might be hidden. 如果是這種情況,請從 工具 功能表中選取 選項,然後在 選項 視窗中選取 專案和方案 > 一般If this is the case, select Options from the Tools menu, then select Projects and Solutions > General in the Options window. 選取 [永遠顯示方案]。Select Always show solution. 此外,請確認已選取 [建立時儲存新專案]。Also, verify that Save new projects when created is selected.

  3. 方案總管開啟IService1.csIService1檔案,並將其程式碼取代為下列程式碼:From Solution Explorer, open the IService1.cs or IService1.vb file, and replace its code with the following code:

    using System;
    using System.ServiceModel;
    
    namespace GettingStartedLib
    {
            [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
            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);
            }
    }
    
    Imports System.ServiceModel
    
    Namespace GettingStartedLib
    
        <ServiceContract(Namespace:="http://Microsoft.ServiceModel.Samples")> _
        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
    End Namespace
    

    本合約定義了線上計算機。This contract defines an online calculator. 請注意,ICalculator 介面會標示 ServiceContractAttribute 屬性(簡化為 ServiceContract)。Notice the ICalculator interface is marked with the ServiceContractAttribute attribute (simplified as ServiceContract). 這個屬性會定義命名空間來區分合約名稱。This attribute defines a namespace to disambiguate the contract name. 程式碼會使用 OperationContractAttribute 屬性來標示每個計算機作業(簡化為 OperationContract)。The code marks each calculator operation with the OperationContractAttribute attribute (simplified as OperationContract).

後續步驟Next steps

在本教學課程中,您將了解如何:In this tutorial, you learned how to:

  • 建立 WCF 服務程式庫專案。Create a WCF Service Library project.
  • 定義服務合約介面。Define a service contract interface.

請前進到下一個教學課程,以瞭解如何執行 WCF 服務合約。Advance to the next tutorial to learn how to implement the WCF service contract.