(3)承载和运行基本的 wcf 服务

这是创建基本 Windows Communication Foundation (WCF) 服务和可以调用该服务的客户端所需的六项任务中的第三项任务。有关全部六项任务的概述,请参见入门教程主题。
本主题描述如何运行基本的 Windows Communication Foundation (WCF) 服务。此过程包含以下步骤:
为服务创建基址。


为服务创建服务主机。


启用元数据交换。


打开服务主机。



在过程后面的以下示例中提供了在此任务中编写的代码的完整清单。将下面的代码添加到 Program 类中定义的 Main() 方法。此类是在您创建 Service 解决方案时生成的。
为服务配置基址
为服务的基址创建 Uri 实例。此 URI 指定 HTTP 方案、本地计算机、端口号 8000,以及服务协定中为服务命名空间指定的服务路径 ServiceModelSample/Service。

Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");


承载服务
导入 System.ServiceModel.Description 命名空间。这行代码应该与 using

using System.ServiceModel.Description;




创建一个新的 ServiceHost 实例以承载服务。必须指定实现服务协定和基址的类型。对于此示例,基址为 http://localhost:8000/ServiceModelSamples/Service,CalculatorService 为实现服务协定的类型。
C#VB
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);


添加一个捕获 CommunicationException 的 try-catch 语句,并在接下来的三个步骤中将该代码添加到 try 块中。catch 子句应该显示错误信息,然后调用 selfHost.Abort()。

try
{
    // ...
}
catch (CommunicationException ce)
{
    Console.WriteLine("An exception occurred: {0}", ce.Message);
    selfHost.Abort();
}



添加公开服务的终结点。为此,必须指定终结点公开的协议、绑定和终结点的地址。对于此示例,将 ICalculator 指定为协定,将 WSHttpBinding 指定为绑定,并将 CalculatorService 指定为地址。在这里请注意,终结点地址是相对地址。终结点的完整地址是基址和终结点地址的组合。在此例中,完整地址是 http://localhost:8000/ServiceModelSamples/Service/CalculatorService。

selfHost.AddServiceEndpoint(
    typeof(ICalculator),
    new WSHttpBinding(),
    "CalculatorService");




注意:
从 .NET Framework 4 开始,如果未显式配置服务的任何终结点,则当打开 ServiceHost 时,运行时将添加默认终结点。此示例显式添加了一个终结点,以提供如何执行此操作的示例。有关默认终结点、绑定和行为的更多信息,请参见简化配置和 WCF 服务的简化配置。
启用元数据交换。为此,添加服务元数据行为。首先创建一个 ServiceMetadataBehavior 实例,将 HttpGetEnabled 属性设置为 true,然后为服务添加新行为。
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();


打开 ServiceHost 并等待传入消息。用户按 Enter 键时,关闭 ServiceHost。

示例

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

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


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("http://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();
            }

        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值