如何:承载和运行基本的 Windows Communication Foundation 服务

这是创建基本 Windows Communication Foundation (WCF) 服务和可以调用该服务的客户端所需的六项任务中的第三项任务。有关全部六项任务的概述,请参见入门教程主题。

本主题描述如何运行基本的 Windows Communication Foundation (WCF) 服务。此过程包含以下步骤:

  • 为服务创建基址。

  • 为服务创建服务主机。

  • 启用元数据交换。

  • 打开服务主机。

在过程后面的以下示例中提供了在此任务中编写的代码的完整清单。将下面的代码添加到 Program 类中定义的 Main() 方法。此类是在您创建 Service 解决方案时生成的。

为服务配置基址

  1. 为服务的基址创建 Uri 实例。此 URI 指定 HTTP 方案、本地计算机、端口号 8000,以及服务协定中为服务命名空间指定的服务路径 ServiceModelSample/Service。

    Dim baseAddress As New Uri("https://localhost:8000/ServiceModelSamples/Service")
    
    Uri baseAddress = new Uri("https://localhost:8000/ServiceModelSamples/Service");
    

承载服务

  1. 导入 System.ServiceModel.Description 命名空间。这行代码应该与 usingimports 语句的其余部分一起放置在 Program.cs/Program.vb 文件的顶部。

    Imports System.ServiceModel.Description
    
    using System.ServiceModel.Description;
    
  2. 创建一个新的 ServiceHost 实例以承载服务。必须指定实现服务协定和基址的类型。对于此示例,基址为 https://localhost:8000/ServiceModelSamples/ServiceCalculatorService 为实现服务协定的类型。

    Dim selfHost As New ServiceHost(GetType(CalculatorService), baseAddress)
    
    ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
    
  3. 添加一个捕获 CommunicationExceptiontry-catch 语句,并在接下来的三个步骤中将该代码添加到 try 块中。catch 子句应该显示错误信息,然后调用 selfHost.Abort()

    Try
        ' ...
    Catch ce As CommunicationException
        Console.WriteLine("An exception occurred: {0}", ce.Message)
        selfHost.Abort()
    End Try
    
    try
    {
        // ...
    }
    catch (CommunicationException ce)
    {
        Console.WriteLine("An exception occurred: {0}", ce.Message);
        selfHost.Abort();
    }
    
  4. 添加公开服务的终结点。为此,必须指定终结点公开的协议、绑定和终结点的地址。对于此示例,将 ICalculator 指定为协定,将 WSHttpBinding 指定为绑定,并将 CalculatorService 指定为地址。在这里请注意,终结点地址是相对地址。终结点的完整地址是基址和终结点地址的组合。在此例中,完整地址是 https://localhost:8000/ServiceModelSamples/Service/CalculatorService

    ' Add a service endpoint
    selfHost.AddServiceEndpoint( _
        GetType(ICalculator), _
        New WSHttpBinding(), _
        "CalculatorService")
    
    selfHost.AddServiceEndpoint(
        typeof(ICalculator),
        new WSHttpBinding(),
        "CalculatorService");
    
    ms730935.note(zh-cn,VS.100).gif注意:
    从 .NET Framework 4 开始,如果未显式配置服务的任何终结点,则当打开 ServiceHost 时,运行时将添加默认终结点。此示例显式添加了一个终结点,以提供如何执行此操作的示例。有关默认终结点、绑定和行为的更多信息,请参见简化配置WCF 服务的简化配置

  5. 启用元数据交换。为此,添加服务元数据行为。首先创建一个 ServiceMetadataBehavior 实例,将 HttpGetEnabled 属性设置为 true,然后为服务添加新行为。有关发布元数据时出现的安全问题的更多信息,请参见Security Considerations with Metadata

    ' Enable metadata exchange
    Dim smb As New ServiceMetadataBehavior()
    smb.HttpGetEnabled = True
    selfHost.Description.Behaviors.Add(smb)
    
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    selfHost.Description.Behaviors.Add(smb);
    
  6. 打开 ServiceHost 并等待传入消息。用户按 Enter 键时,关闭 ServiceHost

    selfHost.Open()
    Console.WriteLine("The service is ready.")
    Console.WriteLine("Press <ENTER> to terminate service.")
    Console.WriteLine()
    Console.ReadLine()
    
    ' Close the ServiceHostBase to shutdown the service.
    selfHost.Close()
    
    selfHost.Open();
    Console.WriteLine("The service is ready.");
    Console.WriteLine("Press <ENTER> to terminate service.");
    Console.WriteLine();
    Console.ReadLine();
    
    // Close the ServiceHostBase to shutdown the service.
    selfHost.Close();
    

验证服务是否正常运行

  1. 从 Visual Studio 内部运行 service.exe。在 Windows Vista 上运行时,必须使用管理员特权运行服务。由于 Visual Studio 是使用管理员特权运行的,因此 service.exe 也是使用管理员特权运行的。也可以启动新的命令提示,使用管理员特权运行它,并在其中运行 service.exe。

  2. 打开 Internet Explorer,并浏览到服务的调试页(网址为 https://localhost:8000/ServiceModelSamples/Service)。

示例

下面的示例包括本教程中前面步骤的服务协定和实现,并将服务承载于控制台应用程序中。将以下内容编译为可执行文件 Service.exe。

在编译代码时,务必引用 System.ServiceModel.dll

Imports System
Imports System.ServiceModel
Imports System.ServiceModel.Description

Module Service
    ' Define a service contract.
    <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

    ' Service class that implements the service contract.
    ' Added code to write output to the console window.
    Public Class CalculatorService
        Implements ICalculator
        Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
            Dim result As Double = n1 + n2
            Console.WriteLine("Received Add({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        End Function

        Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Dim result As Double = n1 - n2
            Console.WriteLine("Received Subtract({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        End Function

        Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Dim result As Double = n1 * n2
            Console.WriteLine("Received Multiply({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        End Function

        Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
            Dim result As Double = n1 / n2
            Console.WriteLine("Received Divide({0},{1})", n1, n2)
            Console.WriteLine("Return: {0}", result)
            Return result
        End Function
    End Class

    Class Program
        Shared Sub Main()
            ' Step 1 of the address configuration procedure: Create a URI to serve as the base address.
            Dim baseAddress As New Uri("https://localhost:8000/ServiceModelSamples/Service")

            ' Step 2 of the hosting procedure: Create ServiceHost
            Dim selfHost As New ServiceHost(GetType(CalculatorService), baseAddress)
            Try

                ' Step 3 of the hosting procedure: Add a service endpoint.
                ' Add a service endpoint
                selfHost.AddServiceEndpoint( _
                    GetType(ICalculator), _
                    New WSHttpBinding(), _
                    "CalculatorService")

                ' Step 4 of the hosting procedure: Enable metadata exchange.
                ' Enable metadata exchange
                Dim smb As New ServiceMetadataBehavior()
                smb.HttpGetEnabled = True
                selfHost.Description.Behaviors.Add(smb)

                ' Step 5 of the hosting procedure: Start (and then stop) the service.
                selfHost.Open()
                Console.WriteLine("The service is ready.")
                Console.WriteLine("Press <ENTER> to terminate service.")
                Console.WriteLine()
                Console.ReadLine()

                ' Close the ServiceHostBase to shutdown the service.
                selfHost.Close()
            Catch ce As CommunicationException
                Console.WriteLine("An exception occurred: {0}", ce.Message)
                selfHost.Abort()
            End Try
        End Sub
    End Class
using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Microsoft.ServiceModel.Samples
{
    // Define a service contract.
    [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);
    }

    // Service class that implements the service contract.
    // Added code to write output to the console window.
    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
            Uri baseAddress = new Uri("https://localhost:8000/ServiceModelSamples/Service");

            // Step 2 of the hosting procedure: Create ServiceHost
            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            try
            {


                // Step 3 of the hosting procedure: Add a service endpoint.
                selfHost.AddServiceEndpoint(
                    typeof(ICalculator),
                    new WSHttpBinding(),
                    "CalculatorService");


                // Step 4 of the hosting procedure: Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5 of the hosting procedure: Start (and then stop) the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }

        }
    }
}
ms730935.note(zh-cn,VS.100).gif注意:
此类服务需要在计算机上注册 HTTP 地址以进行侦听的权限。管理员帐户具有此权限,但必须授予非管理员帐户对 HTTP 命名空间的权限。有关如何配置命名空间保留的更多信息,请参见配置 HTTP 和 HTTPS。在 Visual Studio 下运行时,必须使用管理员特权运行 service.exe。

此时服务正在运行。继续执行如何:创建 Windows Communication Foundation 客户端中的步骤。有关疑难解答信息,请参见入门教程疑难解答

另请参见

任务

入门示例
自承载