WCF的WindowsService开发参考【附源码】
2011-05-08 01:03 通用C#系统架构 阅读(5061) 评论(13) 编辑 收藏 举报首先看图:
1:源码结构图
2:配置文件参考
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="DotNet.Service.ExceptionService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ExceptionService/" binding="basicHttpBinding" contract="DotNet.IService.IExceptionService">
</endpoint>
</service>
<service name="DotNet.Service.FileService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/FileService/" binding="basicHttpBinding" contract="DotNet.IService.IFileService">
</endpoint>
</service>
<service name="DotNet.Service.FolderService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/FolderService/" binding="basicHttpBinding" contract="DotNet.IService.IFolderService">
</endpoint>
</service>
<service name="DotNet.Service.ItemDetailsService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ItemDetailsService/" binding="basicHttpBinding" contract="DotNet.IService.IItemDetailsService">
</endpoint>
</service>
<service name="DotNet.Service.ItemsService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ItemsService/" binding="basicHttpBinding" contract="DotNet.IService.IItemsService">
</endpoint>
</service>
<service name="DotNet.Service.LogOnService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/LogOnService/" binding="basicHttpBinding" contract="DotNet.IService.ILogOnService">
</endpoint>
</service>
<service name="DotNet.Service.LogService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/LogService/" binding="basicHttpBinding" contract="DotNet.IService.ILogService">
</endpoint>
</service>
<service name="DotNet.Service.MessageService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/MessageService/" binding="basicHttpBinding" contract="DotNet.IService.IMessageService">
</endpoint>
</service>
<service name="DotNet.Service.ModuleService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ModuleService/" binding="basicHttpBinding" contract="DotNet.IService.IModuleService">
</endpoint>
</service>
<service name="DotNet.Service.OrganizeService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/OrganizeService/" binding="basicHttpBinding" contract="DotNet.IService.IOrganizeService">
</endpoint>
</service>
<service name="DotNet.Service.ParameterService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ParameterService/" binding="basicHttpBinding" contract="DotNet.IService.IParameterService">
</endpoint>
</service>
<service name="DotNet.Service.PermissionItemService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/PermissionItemService/" binding="basicHttpBinding" contract="DotNet.IService.IPermissionItemService">
</endpoint>
</service>
<service name="DotNet.Service.PermissionService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/PermissionService/" binding="basicHttpBinding" contract="DotNet.IService.IPermissionService">
</endpoint>
</service>
<service name="DotNet.Service.RoleService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/RoleService/" binding="basicHttpBinding" contract="DotNet.IService.IRoleService">
</endpoint>
</service>
<service name="DotNet.Service.SequenceService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/SequenceService/" binding="basicHttpBinding" contract="DotNet.IService.ISequenceService">
</endpoint>
</service>
<service name="DotNet.Service.StaffService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/StaffService/" binding="basicHttpBinding" contract="DotNet.IService.IStaffService">
</endpoint>
</service>
<service name="DotNet.Service.UserService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/UserService/" binding="basicHttpBinding" contract="DotNet.IService.IUserService">
</endpoint>
</service>
<service name="DotNet.Service.UserCenterDbHelperService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/UserCenterDbHelperService/" binding="basicHttpBinding" contract="DotNet.IService.IDbHelperService">
</endpoint>
</service>
<service name="DotNet.Service.BusinessDbHelperService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/BusinessDbHelperService/" binding="basicHttpBinding" contract="DotNet.IService.IDbHelperService">
</endpoint>
</service>
<service name="DotNet.Service.WorkFlowActivityAdminService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/WorkFlowActivityAdminService/" binding="basicHttpBinding" contract="DotNet.IService.IWorkFlowActivityAdminService">
</endpoint>
</service>
<service name="DotNet.Service.WorkFlowCurrentService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/WorkFlowCurrentService/" binding="basicHttpBinding" contract="DotNet.IService.IWorkFlowCurrentService">
</endpoint>
</service>
<service name="DotNet.Service.WorkFlowProcessAdminService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/WorkFlowProcessAdminService/" binding="basicHttpBinding" contract="DotNet.IService.IWorkFlowProcessAdminService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Internet">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug httpHelpPageEnabled="True" includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
<configuration>
<system.serviceModel>
<services>
<service name="DotNet.Service.ExceptionService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ExceptionService/" binding="basicHttpBinding" contract="DotNet.IService.IExceptionService">
</endpoint>
</service>
<service name="DotNet.Service.FileService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/FileService/" binding="basicHttpBinding" contract="DotNet.IService.IFileService">
</endpoint>
</service>
<service name="DotNet.Service.FolderService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/FolderService/" binding="basicHttpBinding" contract="DotNet.IService.IFolderService">
</endpoint>
</service>
<service name="DotNet.Service.ItemDetailsService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ItemDetailsService/" binding="basicHttpBinding" contract="DotNet.IService.IItemDetailsService">
</endpoint>
</service>
<service name="DotNet.Service.ItemsService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ItemsService/" binding="basicHttpBinding" contract="DotNet.IService.IItemsService">
</endpoint>
</service>
<service name="DotNet.Service.LogOnService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/LogOnService/" binding="basicHttpBinding" contract="DotNet.IService.ILogOnService">
</endpoint>
</service>
<service name="DotNet.Service.LogService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/LogService/" binding="basicHttpBinding" contract="DotNet.IService.ILogService">
</endpoint>
</service>
<service name="DotNet.Service.MessageService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/MessageService/" binding="basicHttpBinding" contract="DotNet.IService.IMessageService">
</endpoint>
</service>
<service name="DotNet.Service.ModuleService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ModuleService/" binding="basicHttpBinding" contract="DotNet.IService.IModuleService">
</endpoint>
</service>
<service name="DotNet.Service.OrganizeService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/OrganizeService/" binding="basicHttpBinding" contract="DotNet.IService.IOrganizeService">
</endpoint>
</service>
<service name="DotNet.Service.ParameterService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/ParameterService/" binding="basicHttpBinding" contract="DotNet.IService.IParameterService">
</endpoint>
</service>
<service name="DotNet.Service.PermissionItemService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/PermissionItemService/" binding="basicHttpBinding" contract="DotNet.IService.IPermissionItemService">
</endpoint>
</service>
<service name="DotNet.Service.PermissionService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/PermissionService/" binding="basicHttpBinding" contract="DotNet.IService.IPermissionService">
</endpoint>
</service>
<service name="DotNet.Service.RoleService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/RoleService/" binding="basicHttpBinding" contract="DotNet.IService.IRoleService">
</endpoint>
</service>
<service name="DotNet.Service.SequenceService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/SequenceService/" binding="basicHttpBinding" contract="DotNet.IService.ISequenceService">
</endpoint>
</service>
<service name="DotNet.Service.StaffService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/StaffService/" binding="basicHttpBinding" contract="DotNet.IService.IStaffService">
</endpoint>
</service>
<service name="DotNet.Service.UserService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/UserService/" binding="basicHttpBinding" contract="DotNet.IService.IUserService">
</endpoint>
</service>
<service name="DotNet.Service.UserCenterDbHelperService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/UserCenterDbHelperService/" binding="basicHttpBinding" contract="DotNet.IService.IDbHelperService">
</endpoint>
</service>
<service name="DotNet.Service.BusinessDbHelperService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/BusinessDbHelperService/" binding="basicHttpBinding" contract="DotNet.IService.IDbHelperService">
</endpoint>
</service>
<service name="DotNet.Service.WorkFlowActivityAdminService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/WorkFlowActivityAdminService/" binding="basicHttpBinding" contract="DotNet.IService.IWorkFlowActivityAdminService">
</endpoint>
</service>
<service name="DotNet.Service.WorkFlowCurrentService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/WorkFlowCurrentService/" binding="basicHttpBinding" contract="DotNet.IService.IWorkFlowCurrentService">
</endpoint>
</service>
<service name="DotNet.Service.WorkFlowProcessAdminService" behaviorConfiguration="Internet">
<endpoint address="http://localhost:8888/DotNet.Service/WorkFlowProcessAdminService/" binding="basicHttpBinding" contract="DotNet.IService.IWorkFlowProcessAdminService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Internet">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug httpHelpPageEnabled="True" includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
3:核心代码部分
//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd.
//--------------------------------------------------------------------
using System;
using System.Configuration;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.ServiceProcess;
using System.Diagnostics;
using System.Windows.Forms;
namespace DotNet.WindowsService
{
using DotNet.Utilities;
/// <summary>
/// CommonService
///
/// 修改纪录
///
/// 2008.03.06 版本:1.0 JiRiGaLa 创建Windows服务。
///
/// 版本:1.0
///
/// <author>
/// <name>JiRiGaLa</name>
/// <date>2007.08.15</date>
/// </author>
/// </summary>
public partial class CommonService : ServiceBase
{
public CommonService()
{
InitializeComponent();
}
/// <summary>
/// 服务数组
/// </summary>
ServiceHost[] serviceHosts = null;
protected override void OnStart(string[] args)
{
// 是否检查在线状态
BaseSystemInfo.CheckOnLine = false;
BaseSystemInfo.AllowNullPassword = true;
// 是否记录日志
BaseSystemInfo.RecordLog = true;
// 是否需要注册
BaseSystemInfo.NeedRegister = false;
// 读取配置文件
ConfigHelper.GetConfig(Application.StartupPath + "\\" + ConfigHelper.FileName);
// 读取配置文件
Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup serviceModelSectionGroup = (ServiceModelSectionGroup)configuration.GetSectionGroup("system.serviceModel");
// 开启每个服务
int i = 0;
serviceHosts = new ServiceHost[serviceModelSectionGroup.Services.Services.Count];
foreach (ServiceElement serviceElement in serviceModelSectionGroup.Services.Services)
{
string assemblyString = serviceElement.Name.Substring(0, serviceElement.Name.LastIndexOf('.'));
var serviceHost = new ServiceHost(Assembly.Load(assemblyString).GetType(serviceElement.Name), serviceElement.Endpoints[0].Address);
serviceHost.Opened += delegate { Console.WriteLine("第{0}服务:{1}", i+1, serviceHost.BaseAddresses[0]); };
serviceHost.Open();
serviceHosts[i] = serviceHost;
i++;
}
base.OnStart(args);
}
protected override void OnStop()
{
Trace.WriteLine("Shutting down ServiceHost...");
if (serviceHosts != null)
{
for (int i = serviceHosts.Length-1; i >= 0; i--)
{
serviceHosts[i].Close();
serviceHosts[i] = null;
}
}
base.OnStop();
}
}
}
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd.
//--------------------------------------------------------------------
using System;
using System.Configuration;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.ServiceProcess;
using System.Diagnostics;
using System.Windows.Forms;
namespace DotNet.WindowsService
{
using DotNet.Utilities;
/// <summary>
/// CommonService
///
/// 修改纪录
///
/// 2008.03.06 版本:1.0 JiRiGaLa 创建Windows服务。
///
/// 版本:1.0
///
/// <author>
/// <name>JiRiGaLa</name>
/// <date>2007.08.15</date>
/// </author>
/// </summary>
public partial class CommonService : ServiceBase
{
public CommonService()
{
InitializeComponent();
}
/// <summary>
/// 服务数组
/// </summary>
ServiceHost[] serviceHosts = null;
protected override void OnStart(string[] args)
{
// 是否检查在线状态
BaseSystemInfo.CheckOnLine = false;
BaseSystemInfo.AllowNullPassword = true;
// 是否记录日志
BaseSystemInfo.RecordLog = true;
// 是否需要注册
BaseSystemInfo.NeedRegister = false;
// 读取配置文件
ConfigHelper.GetConfig(Application.StartupPath + "\\" + ConfigHelper.FileName);
// 读取配置文件
Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup serviceModelSectionGroup = (ServiceModelSectionGroup)configuration.GetSectionGroup("system.serviceModel");
// 开启每个服务
int i = 0;
serviceHosts = new ServiceHost[serviceModelSectionGroup.Services.Services.Count];
foreach (ServiceElement serviceElement in serviceModelSectionGroup.Services.Services)
{
string assemblyString = serviceElement.Name.Substring(0, serviceElement.Name.LastIndexOf('.'));
var serviceHost = new ServiceHost(Assembly.Load(assemblyString).GetType(serviceElement.Name), serviceElement.Endpoints[0].Address);
serviceHost.Opened += delegate { Console.WriteLine("第{0}服务:{1}", i+1, serviceHost.BaseAddresses[0]); };
serviceHost.Open();
serviceHosts[i] = serviceHost;
i++;
}
base.OnStart(args);
}
protected override void OnStop()
{
Trace.WriteLine("Shutting down ServiceHost...");
if (serviceHosts != null)
{
for (int i = serviceHosts.Length-1; i >= 0; i--)
{
serviceHosts[i].Close();
serviceHosts[i] = null;
}
}
base.OnStop();
}
}
}
4:安装服务、卸载服务的命令
installutil /i "D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WindowsService\bin\Debug\DotNet.WindowsService.exe"
installutil /u "D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WindowsService\bin\Debug\DotNet.WindowsService.exe"
installutil /u "D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WindowsService\bin\Debug\DotNet.WindowsService.exe"
5:安装服务
6:运行情况
7:运行客户端,搞定!
8:全部功能调试ok,源码参考下载【WindowsService部分】
/Files/jirigala/JiRiGaLa.DotNet.WindowsService.rar
心情压抑时,看看女儿的照片,逗逗女儿啥烦恼都没了,像我不哈哈,小小可爱。
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。