WCF的WindowsService開發參考【附原始碼】
首先看圖:
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>
publicpartialclass CommonService : ServiceBase
{
public CommonService()
{
InitializeComponent();
}
///<summary>
/// 服務陣列
///</summary>
ServiceHost[] serviceHosts =null;
protectedoverridevoid 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);
}
protectedoverridevoid 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>
publicpartialclass CommonService : ServiceBase
{
public CommonService()
{
InitializeComponent();
}
///<summary>
/// 服務陣列
///</summary>
ServiceHost[] serviceHosts =null;
protectedoverridevoid 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);
}
protectedoverridevoid 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
心情壓抑時,看看女兒的照片,逗逗女兒啥煩惱都沒了,像我不哈哈,小小可愛。
本文轉自 jirigala 51CTO部落格,原文連結:http://blog.51cto.com/2347979/1199868,如需轉載請自行聯絡原作者
相關文章
- 量化合約系統開發 (參考版),量化合約開發策略原始碼方案原始碼
- webapp開發參考WebAPP
- NFT鑄造智慧合約平臺開發方案部署原始碼建議參考原始碼
- 秒合約系統開發邏輯實現,秒合約系統開發部分原始碼參考原始碼
- 開發技術選型參考
- 微信雲開發參考連結
- Linux 原始碼安裝Ansible 參考篇Linux原始碼
- Java Web程式開發參考手冊JavaWeb
- nft質押LP流動性挖礦系統開發規則(系統原始碼參考)原始碼
- 常用的Oracle指令碼參考Oracle指令碼
- 模擬考試參考程式碼
- sublime外掛開發教程(附原始碼)原始碼
- 敏捷開發流程管理須參考的3個要素敏捷
- BOOST庫 學習參考完全開發指南
- 微信開發常用文件及參考資料
- WindowsService服務程式開發 安裝和解除安裝Windows
- 微信小程式開發總結(附原始碼)微信小程式原始碼
- 供iOS遊戲開發新手參考的5項技巧iOS遊戲開發
- 學習 WCF (3)--開發WCF客戶程式
- Cheeper:《CQRS By Example》一書的參考程式碼開源實現
- 使用delphi開發人工智慧程式(參考資料)人工智慧
- jQuery MiniUI 開發指南+API元件參考手冊jQueryUIAPI元件
- Web開發中很實用的10個效果【附原始碼】Web原始碼
- [轉]Oracle的異常控制編碼參考Oracle
- 開發參考:介紹一款多專案java開發平臺Java
- Python 編碼風格參考Python
- iOS開發——實時監控網速(僅作參考)iOS
- 佛薩奇2.0開發參考版丨佛薩奇2.0系統開發(成熟及專案)丨佛薩奇2.0系統原始碼部署原始碼
- 迴圈互助智慧合約(FDF)系統開發(參考版)丨FDF智慧合約開發程式碼示例
- 【資料結構&演算法】09-佇列概念&參考原始碼資料結構演算法佇列原始碼
- OSI參考模型和TCP/IP參考模型模型TCP
- 基於 go 語言開發部署的部落格 免費開源供參考Go
- iOS常見基礎面試題(附參考答案)iOS面試題
- javamail參考JavaAI
- SQLAlchemy參考SQL
- 參考地址
- 開發網校系統原始碼前,需要考慮哪些關鍵點?原始碼
- 線上試題答題考試系統專案開發搭建原始碼原始碼