教程:定义 Windows Communication Foundation 服务协定

本教程介绍创建基本 Windows Communication Foundation (WCF) 应用程序所需的五个任务中的首个任务。 有关教程概述,请参阅教程:Windows Communication Foundation 应用程序入门

创建 WCF 服务时,第一个任务是定义服务协定。 服务协定指定服务支持的操作。 可以将操作视为一个 Web 服务方法。 通过定义 C# 或 Visual Basic 接口来创建服务协定。 接口具有以下特征:

本教程介绍如何执行下列操作:

  • 创建“WCF 服务库”项目。
  • 定义服务协定接口。

创建 WCF 服务库项目并定义服务协定接口

  1. 以管理员的身份打开 Visual Studio。 若要执行此操作,请在“开始”菜单中选择 Visual Studio 程序,然后从快捷菜单中选择“更多”>“以管理员身份运行”。

  2. 创建“WCF 服务库”项目。

    1. 从“文件”菜单中选择“新建”>“项目” 。

    2. 在“新建项目”对话的左侧,展开“Visual C#”或“Visual Basic”,然后选择“WCF”类别。 在窗口中心部分,Visual Studio 显示了项目模板列表。 选择“WCF 服务库”。

      备注

      如果没有显示 WCF 项目模板类别,则你可能需要安装 Visual Studio 的 Windows Communication Foundation 组件。 在“新建项目”对话框中,在左侧选择“打开 Visual Studio 安装程序”链接。 选择“单个组件”选项卡,然后在“开发活动”类别下找到并选择“Windows Communication Foundation”。 选择“修改”以开始安装组件。

    3. 在窗口底部,输入“GettingStartedLib”作为“名称”,输入“GettingStarted”作为“解决方案名称”。

    4. 选择“确定”。

      Visual Studio 创建的此项目包含三个文件:IService1.cs(或适用于 Visual Basic 项目的 IService1.vb)、Service1.cs(或适用于 Visual Basic 项目的 Service1.vb)和 App.config。Visual Studio 将这些文件定义如下:

      • IService1 文件包含服务协定的默认定义。
      • Service1 文件包含服务协定的默认实现。
      • App.config 文件包含使用 Visual Studio WCF 服务主机工具加载默认服务所需的配置信息。 有关 WCF 服务主机工具的详细信息,请参阅 WCF 服务主机 (WcfSvcHost.exe)

      注意

      如果你安装了具有 Visual Basic 开发人员环境设置的 Visual Studio,则可能会隐藏解决方案。 如果是这种情况,请从“工具”菜单选择“选项”,然后在“选项”窗口中,选择“项目和解决方案”>“常规”。 选择“始终显示解决方案”。 此外,请验证是否选择了“创建后保存新项目”。

  3. 从“解决方案资源管理器”中,打开“IService1.cs”或“IService1.vb”文件,然后使用以下代码替换其代码:

    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
    

    本协定定义了一个在线计算器。 注意,ICalculator 接口使用 ServiceContractAttribute 属性(简化为 ServiceContract)进行标记。 此属性定义命名空间,以消除协定名称的歧义。 该代码将每个计算器操作标记为 OperationContractAttribute 属性(简化为 OperationContract)。

后续步骤

在本教程中,你了解了如何执行以下操作:

  • 创建“WCF 服务库”项目。
  • 定义服务协定接口。

请继续阅读下一篇教程,了解如何实现 WCF 服务协定。