入门代码教程第三节 如何:运行基本的服务

本文介绍如何使用Windows Communication Foundation (WCF)搭建一个基础服务。包括配置服务基址、创建服务主机、添加服务终结点及启用元数据交换等步骤。通过示例代码展示了如何实现加减乘除运算。

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

  • 为服务创建基址。

  • 为服务创建服务主机。

  • 启用元数据交换。

  • 打开服务主机。

在过程后面的以下示例中提供了用于此任务的代码。

 

为服务配置基址

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

 

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

承载服务

  1. 创建一个新的 ServiceHost 实例以承载服务。 必须指定实现服务协定和基址的类型。 对于此示例,我们将基址指定为 http://localhost:8000/ServiceModelSamples/Services,并将 CalculatorService 指定为实现服务协定的类型。

ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
2.添加一个捕获 CommunicationExceptiontry-catch 语句,并在接下来的三个步骤中将该代码添加到 try 块中。
3.添加公开服务的终结点。 为此,必须指定终结点公开的协议、绑定和终结点的地址。 对于此示例,将 ICalculator 指定为协定,将 WSHttpBinding 指定为绑定,并将 CalculatorService 指定为地址。 在这里请注意,我们指定的是相对地址。 终结点的完整地址是基址和终结点地址的组合。 在此例中,完整地址是 http://localhost:8000/ServiceModelSamples/Services/CalculatorService
4.启用元数据交换。 为此,必须添加服务元数据行为。 首先创建一个 ServiceMetadataBehavior 实例,将 HttpGetEnabled 属性设置为 true,然后为服务添加新行为。
5.打开 ServiceHost 并等待传入消息。 用户按 Enter 键时,关闭 ServiceHost
示例

 
ContractedBlock.gif ExpandedBlockStart.gif Code
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 1 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();
            }
        }
    }
}
服务运行后,下一步是创建一个客户端以访问该服务。

转载于:https://www.cnblogs.com/tomkillua/archive/2008/09/25/1298278.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值