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

WCF - IIS主机

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

在IIS(Internet信息服务)中托管WCF服务是一个循序渐进的过程. IIS Hosting在下面详细说明了所需的编码以及截图以了解该过程.

步骤1 : 启动Visual Studio 2012,然后单击File → 新的 → 网站.选择"WCF服务",将位置选为http.这将在IIS中托管服务.单击确定.

Wcf托管服务IIS 1

步骤2 : 接口背后的代码如下所示.

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 interface name "IService" in both code and config file // together.[ServiceContract]Public interface IService {   [OperationContract]   String GetData(int value);   [OperationContract]   CompositeType GetDataUsingDataContract(CompositeType composite);   // TODO: Add your service operations here}// Use a data contract as illustrated in the sample below to add // composite types to service operations.[DataContract]Public class CompositeType {   Bool boolValue = true;   String stringValue = "Hello ";   [DataMember]   Public bool BoolValue {      get { return boolValue; }      set { boolValue = value; }   }   [DataMember]   Public string StringValue {      get { return stringValue; }      set { stringValue = value; }   } }

第3步 : 类文件背后的代码如下所示.

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 Service : IService {   Public string GetData(int value) {      Return string.Format("You entered: {0}", value);   }   Public CompositeType GetDataUsingDataContract(CompositeType composite) {      if(composite == null) {         thrownewArgumentNullException("composite");      }            if(composite.BoolValue) {         composite.StringValue += "Suffix";      }      return composite;   } }

第4步 : 服务文件(.svc)包含服务名称和文件名后面的代码.此文件用于了解服务.

Wcf Hosting Services IIS 4


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

第5步 : 配置文件中提到了服务器端配置.这里,只提到一个配置为'wsHttpBinding'的端点;我们也可以有多个具有不同绑定的端点.由于我们要在IIS中托管,我们只能使用http绑定.

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

第6步 : 您需要提及服务文件名以及配置文件中提到的地址.这里给出了IIS的屏幕截图.

点击Start →  run →  inetmgr将打开以下窗口.

Wcf Hosting Service IIS 6

第7步 : 运行将生成以下屏幕的应用程序.

Wcf Hosting Service IIS 7