转自:https://www.cnblogs.com/huangxincheng/archive/2011/11/13/2247227.html
今天是系列的终结篇,当然要分享一下wcf的托管方面的知识。
wcf中托管服务一般有一下四种:
Console寄宿: 利于开发调试,但不是生产环境中的最佳实践。
winform寄宿: 方便与用户进行交互,用户想开就开,想关就关,但如果机器重启了,不得不自己手动开一下,危险+麻烦。
IIS寄宿: 此寄宿在实战项目中得到了广泛的应用。
好处有:随系统启动和停止。
iis有大量的管理策略对其进行管理。
即想利用wcf的功能,还想访问asp.net的功能。
Window Service 寄宿: 这个寄宿在实战中也是广泛运用的,同时也是随系统开启或者停止。
好了,前两种寄宿相信大家都会,在这里我就不介绍了,我主要介绍后两种寄宿。
IIS寄宿: 首先要知道寄宿于iis的3个条件: 应用程序域(相当于serviceHost)+svc文件+config的配置节
很感谢vs模板,里面已经包含用于寄宿于iis的模板,ok,上图:

下面的流程就是:
点击确定 ——> 鼠标右击wcf服务项目——>点击“发布”——>在“发布方法”中选择“文件部署”——>在"目标位置”选择“保存位置”——>点击“发布”按钮

然后我们打开本地的IIS,新建网站,修改端口为1111,然后点击确定按钮,截图如下:

前面我们已经说过寄宿在iis中的三个条件,
首先看“应用程序域”:默认新建的网站在应用程序域中的.net framework的版本是2.0,
所以我们必须修改为4.0版本。
截图如下:

然后我们看一下"svc文件": iis寄宿有一个特点就是不用指定endpoint中的是address,因为svc文件的地址就是“endpoint”的地址,
也就是说如果我想预览则需要在url中输入此文件地址。
然后我们看一下” http://localhost:1111/Service1.svc?wsdl"里面的元数据,发现binding是“basicHttpBinding”,也就是说如果
想改变这个低端的binding,就必须修改三个条件中的最后的一个。
截图如下:

最后看一下“config文件”,模板给我们生成的文件代码如下:

1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 4 <system.web> 5 <compilation debug="true" targetFramework="4.0" /> 6 </system.web> 7 <system.serviceModel> 8 <behaviors> 9 <serviceBehaviors> 10 <behavior> 11 <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> 12 <serviceMetadata httpGetEnabled="true"/> 13 <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> 14 <serviceDebug includeExceptionDetailInFaults="false"/> 15 </behavior> 16 </serviceBehaviors> 17 </behaviors> 18 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 19 </system.serviceModel> 20 <system.webServer> 21 <modules runAllManagedModulesForAllRequests="true"/> 22 </system.webServer> 23 24 </configuration>

也就是说啥也没有,要把binding改成wsHttpBinding,增加终结点即可:

1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 4 <system.web> 5 <compilation debug="true" targetFramework="4.0" /> 6 </system.web> 7 <system.serviceModel> 8 <services> 9 <service name="WcfService1.Service1"> 10 <endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint> 11 </service> 12 </services> 13 <behaviors> 14 <serviceBehaviors> 15 <behavior> 16 <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> 17 <serviceMetadata httpGetEnabled="true"/> 18 <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> 19 <serviceDebug includeExceptionDetailInFaults="false"/> 20 </behavior> 21 </serviceBehaviors> 22 </behaviors> 23 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 24 </system.serviceModel> 25 <system.webServer> 26 <modules runAllManagedModulesForAllRequests="true"/> 27 </system.webServer> 28 29 </configuration>


最后就是客户端调用:

window server寄宿:
要实现window server寄宿,也很感谢vs给我们提供了“window 服务”模板。
步骤如下: “文件”->“新建“—>“项目”->"wcf服务库"模板—>“点击确定”—>右键服务库项目添加“新建项”—>找到"window服务"—>"点击添加"。
为了方便我们将“WcfServiceLibrary1”改成Console应用程序,然后新建一个Main入口。

然后我们点击上面的“单击此处切换到代码视图”按钮,进入到代码视图,
然后我们就可以在里面的"OnStart"和“OnStop”方法中添加代码。

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Diagnostics;
6 using System.Linq;
7 using System.ServiceProcess;
8 using System.Text;
9 using System.ServiceModel;
10
11 namespace WcfServiceLibrary1
12 {
13 partial class Service2 : ServiceBase
14 {
15 public Service2()
16 {
17 InitializeComponent();
18 }
19
20 ServiceHost host;
21
22 protected override void OnStart(string[] args)
23 {
24 // TODO: 在此处添加代码以启动服务。
25 host = new ServiceHost(typeof(Service1));
26
27 host.Open();
28 }
29
30 protected override void OnStop()
31 {
32 // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
33 host.Close();
34 }
35 }
36 }

然后我们在“设计界面” 点击右键,找到”添加安装程序“,点击即可。

添加完成后会出现”servceProcessInstaller1"和“servcieInstaller1”的组件。
右击“serviceProcessInstaller1”,打开属性窗口,修改Account为LocalSystem;

然后 右击“servcieInstaller1”组件,打开属性窗口
修改ServcieName为:MYServiceHost,也就是定义我的服务名。
修改StartType为:Automatic。

最后一步就是将exe注册到window server中:

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.ServiceProcess;
6
7 namespace WcfServiceLibrary1
8 {
9 public class Program
10 {
11 public static void Main()
12 {
13 ServiceBase.Run(new Service2());
14 }
15 }
16 }

好了,现在我们可以编译项目准备安装部署了,vs给我们提供了一个InstallUtil.exe工具,
这个工具也就是真正的把我们的exe程序送到window server中。
打开cmd,要做的两件事情就是正确的找到“InstallUtil.exe“ 和我们的”WcfServiceLibrary1.exe“

ok,安装完成,现在做的就是测试工作。
看, 快看,我找到了我的window server 服务。

开启一下服务,然后预览一下wcf的address,看看是否真的启动了。

哈哈,真的可以了,window service 寄宿大功告成。
本文深入探讨了WCF服务的四种主要托管方式:Console寄宿、WinForm寄宿、IIS寄宿和Windows Service寄宿。重点介绍了IIS寄宿和Windows Service寄宿的详细配置过程,包括应用程序域设置、svc文件使用、config文件配置以及如何在IIS中部署WCF服务。
1247

被折叠的 条评论
为什么被折叠?



