WCF宿主在IIS, Application, WindowsService, WAS

一、宿主在IIS

1、新建一WCF项目。

2、将WCF项目发布于IIS中。

3、新建客户端项目并添加服务引用。

二、宿主在Application

1、新建一类库,编写WCF服务。

2、新建一控制台应用程序(或其他应用程序),承载已编写好的WCF服务。

步骤如下:

a、添加WCF类库项目的引用。

b、编写承载函数。

示例:

//创建一个新的 ServiceHost 实例以承载服务

using(ServiceHost host=new ServiceHost(typeof(HelloService.SayHelloService)))

{

host.Open();

Console.Write("Host has started...");

Console.ReadLine();

}

c、配置App.Config

示例:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.serviceModel>

<services>

<service name="HelloService.SayHelloService" behaviorConfiguration="serviceBehavior">

<endpoint binding="basicHttpBinding" contract="HelloService.ISayHelloService" address="HelloService" />

<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />

<host>

<baseAddresses>

<add baseAddress="http://localhost:8000"/>

</baseAddresses>

</host>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="serviceBehavior">

<serviceMetadata httpGetEnabled="true" />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

3、新建一客户端应用程序

a、添加第2步骤中如 :baseAddress="http://localhost:8000"/ 的WCF引用,并配置App.Config。

b、编写函数调用服务。

示例:

static void Main(string[] args)

{

SayHelloServiceReference.SayHelloServiceClient proxy = new HelloClient.SayHelloServiceReference.SayHelloServiceClient();

string s = proxy.SayHello("wangwei");

Console.WriteLine(s);

Console.ReadLine();

}

注:注意解决方案中项目的启动顺序,及启动项的设置。

三、宿主在WindowsService中

1、新建一类库,编写WCF服务。

2、新建一Windows服务程序,承载已编写好的WCF服务,并配置。

示例:

/**/

/// <summary>

/// 初始化 System.Configuration.Install.Installer 类的新实例。

/// </summary>

[RunInstaller(true)]

public class ProjectInstaller : Installer

{

private ServiceProcessInstaller process;

private ServiceInstaller service;

/**/

/// <summary>

/// 构造函数

/// </summary>

public ProjectInstaller()

{

process = new ServiceProcessInstaller();

process.Account = ServiceAccount.LocalSystem;

service = new ServiceInstaller();

//设置服务名称(显示在控制面板服务列表中的服务名称)

service.ServiceName = "WCFByWindowsService";

//设置服务描述(显示在控制面板服务列表中的服务描述)

service.Description = "WCF服务宿主在WindowsService的服务";

base.Installers.Add(process);

base.Installers.Add(service);

}

}

public partial class WindowsService : ServiceBase

{

public ServiceHost serviceHost = null;

/// <summary>

/// 主函数

/// </summary>

public static void Main()

{

ServiceBase.Run(new WindowsService());

}

public WindowsService()

{

InitializeComponent();

}

protected override void OnStart(string[] args)

{

if (serviceHost != null)

{

serviceHost.Close();

}

//创建ServiceHost

serviceHost = new ServiceHost(typeof(HelloService.SayHelloService));

serviceHost.Open();

}

protected override void OnStop()

{

if (serviceHost != null)

{

serviceHost.Close();

serviceHost = null;

}

}

}

App.Config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.serviceModel>

<services>

<!--name - 提供服务的类名-->

<!--behaviorConfiguration - 指定相关的行为配置-->

<service name="HelloService.SayHelloService" behaviorConfiguration="HelloServiceBehavior">

<!--address - 服务地址-->

<!--binding - 通信方式-->

<!--contract - 服务契约-->

<endpoint address="" binding="wsHttpBinding" contract="HelloService.ISayHelloService" />

<!--元数据交换的endpoint-->

<!--注:address是mex,它会和host/baseAddresses节点中的baseAddress做拼接,即提供元数据交换的地址为:http://localhost:12345/Binding/mex-->

<endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />

<host>

<baseAddresses>

<add baseAddress="http://localhost:11233/WCFByWindowsServiceHost/"/>

</baseAddresses>

</host>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="HelloServiceBehavior">

<serviceMetadata httpGetEnabled="True"/>

<serviceDebug includeExceptionDetailInFaults="False" />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

3、安装服务

本服务路径:"E:\VsProjects\WCFStudyII\WCFByWindowsServiceHost\Host\bin\Debug\Host.exe"

4、新建客户端应用程序,并引用服务并配置。如:

baseAddress="http://localhost:11233/WCFByWindowsServiceHost/

5、编写客户端程序调用服务。

四、宿主在WAS(Windows 进程激活服务)中

。。。