开发手册 欢迎您!
软件开发者资料库

WCF - WAS主机

WCF WAS托管 - 从简单和简单的步骤学习WCF从基本到高级概念,包括概述,Versus Web服务,开发人员工具,架构,创建wcf服务,托管wcf服务,IIS托管,自托管,WAS托管,窗口服务托管,消费wcf服务,服务绑定,实例管理,事务,ria服务,安全性,异常处理。

要理解WAS托管的概念,我们需要理解系统的配置方式以及如何创建服务合同,从而实现与托管服务的不同绑定.

首先,为非协议启用WCF.在我们开始创建服务之前,我们需要配置系统以支持WAS.以下是配置WAS&minus的步骤;

  • 单击"开始"菜单 → 控制面板 → 单击"程序和功能",然后单击左窗格中的"打开或关闭Windows组件".

  • 展开"Microsoft .Net Framework 3.0"并启用"Windows通信"基础HTTP激活"和"Windows Communication Foundation非HTTP激活".

  • 接下来,我们需要将Binding添加到默认网站.例如,我们将默认网站绑定到TCP协议.转到开始菜单 → 程序 → 配件.右键单击"命令提示符",然后从上下文菜单中选择"以管理员身份运行".

  • 执行以下命令 :

C:\Windows\system32\inetsrv> appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp',bindingInformation='808:*']

这命令通过修改位于"C:\ WINDOWH \ system32 \inetsrv\config"目录中的applicationHost.config文件,将net.tcp站点绑定添加到默认网站.同样,我们可以为默认网站添加不同的协议.

创建WAS托管服务

Step-1 : 打开Visual Studio 2008,然后单击New →  WebSite并从模板和Location选择WCF服务,如下所示 :

Wcf托管服务WAS 1

Step-2 : 通过创建IMathService接口来创建合同.将ServiceContract属性添加到接口,将OperationContract属性添加到方法声明.

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;// NOTE: You can use the "Rename" command on the "Refactor" menu to // change the interface name "IService" in both code and config file // together.[ServiceContract]Public interface IMathService {   [OperationContract]   int Add(int num1, int num2);   [OperationContract]   int Subtract(int num1, int num2); }

Step-3 :  IMathService接口的实现显示在下面 :

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;// NOTE: You can use the "Rename" command on the "Refactor" menu to // change the class name "Service" in code, svc and config file // together.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 : 服务文件如下所示.

Wcf Hosting Services WAS 2

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

Step-5 : 在web.Config文件中,使用'netTcpBinding'绑定创建端点,并使用Metadata Exchange点发布服务元数据.因此,创建Metadata Exchange端点,地址为"mex",绑定为"mexTcpBinding".如果不发布服务元数据,我们就无法使用net.tcp地址创建代理,例如 :

svcutil.exe net.tcp://localhost/WASHostedService/MathService.svc).


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

启用不同的绑定到托管服务

  • 转到开始菜单 → 程序 → 配件.右键单击"命令提示符",然后从上下文菜单中选择"以管理员身份运行".

  • 执行以下命令 -

C:\Windows\system32\inetsrv>appcmd set app "Default Web Site/WASHostedService" /enabledProtocols:http,net.tcp

它将产生以下输出 :

Wcf托管服务WAS 6

d="LA_COLLECT" src="//sdk.51.la/js-sdk-pro.min.js">