第四章:WCF托管(3)

原文:http://www.wcftutorial.net/Introduction-to-WCF.aspx

Windows Activation Service

Windows Activation service随着Windows Vista和Windows Server 2008一起发布的。IIS7中可以启用它,WAS比比起IIS要强大很多,它支持HTTP,TCP和命名管道,但是IIS仅仅支持HTTP,它可以被单独安装和配置。

将WCF托管在WAS中有很多优点,比如进程回收,隔离,空闲时间管理,通用配置系统,WAS托管服务可以按照以下步骤进行创建。
    1.WCF配置为HTTP以外协议
    2.创建WAS托管服务
    3.创建服务的不同绑定

WCF配置为HTTP以外协议

在创建服务之前,我们需要去配置系统以便支持WAS,按照下面的步骤我们就可以配置WAS了。
    1.开始->控制面板->应用程序和功能->打开或关闭Windows功能
    2.点开Microsoft .Net Framework 3.0,启用Windows Communication Foundation HTTP Activation和Windows Communication Foundation Non- HTTP Activation
    3.下一步我们需要添加绑定到默认Web站点,我们将向默认站点添加TCP协议作为实例,开始->所有程序->附件->右键以管理员身份运行命令行工具
    4.执行如下命令
    5.C:\Windows\system32\inetsrv> appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp',bindingInformation='808:*']
这个命令向默认Web站点添加了net.tcp的绑定,通过更改C:\Windows\system32\inetsrv\config目录下面配置文件applicationHost.config,同样的,我们可以向默认Web站点添加其他不同的协议。

创建WAS托管服务

Step 1: 接下来让我们创建一个服务,打开VS2008,新建->网站,从模板中选择WCF服务,并且像下列所示将位置选在HTTP上



Step 2: 创建IMathService的契约类,并且添加ServiceContract属性到接口上,OperationContract到方法声明上。

IMathService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

[ServiceContract]
public interface IMathService
{
    [OperationContract]
    int Add(int num1, int num2);

    [OperationContract]
    int Subtract(int num1, int num2);
}


Step 3: 实现IMathService接口

MathService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

public class MathService : IMathService
{
    public int Add(int num1, int num2)
    {
        return num1 + num2;
    }

    public int Subtract(int num1, int num2)
    {
        return num1 - num2;
    }
}


Step 4: Service文件如下所示

MathService.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MathService" CodeBehind="~/App_Code/MathService.cs" %>


Step 5: 在web.config文件中,创建netTcpBinding绑定的终结点,并且服务元数据通过Metadata Exchange point进行发布。所以创建名为mex的Metada Exchange end point,并且它的绑定是mexTcpBinding。如果没有公布的服务元数据,我们将没有办法创建基于net.tcp地址的代理类(比如svcutil.exe net.tcp://localhost/WASHostedService/MathService.svc)

Web.Config

<system.serviceModel>
    <services>
        <service name="MathService" behaviorConfiguration="ServiceBehavior">
            <!-- Service Endpoints -->
            <endpoint binding="netTcpBinding" contract="IMathService"></endpoint>
            <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                <!-- To avoid disclosing metadata information, set the value below 
                to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true"/>
                <!-- To receive exception details in
                faults for debugging purposes, set the value below to true. 
                Set to false before deployment to avoid disclosing 
                exception information -->
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>


创建服务的不同绑定
1.开始->所有程序->附件->命令行工具,右键 以管理员身份运行
2.执行C:\Windows\system32\inetsrv>appcmd set app "Default Web Site/WASHostedServcie" /enabledProtocols:http,net.tcp

输出如下所示


Step 6: 现在服务已经准备好被使用了,下一步我们将使用服务工具创建供客户端程序使用的代理类。创建代理类使用VS命令行工具,并且执行如下命令
svcutil.exe net.tcp://localhost/WASHostedService/MathService.svc

代理类和配置文件会在相应的地方生成



Step 7: 创建如下的客户端应用程序,并且添加参照System.ServiceModel



Step 8: 将代理类和配置文件加入到客户端程序中,创建MathServiceClient的对象,调用方法

Program.cs

class Program
{
    static void Main(string[] args)
    {
        MathServiceClient client = new MathServiceClient();
        Console.WriteLine("Sum of two number 5,6");
        Console.WriteLine(client.Add(5,6));
        Console.ReadLine();
    }
}


输出如下所示:


这篇教程清楚的解释了将WCF托管在WAS中的步骤,接下俩我们看如何将服务托管在Windows Service中

猜你喜欢

转载自foreversky12.iteye.com/blog/2308499