كارت dvb , كارت دي وي بي , دی وی بی , رسيور , رسیور
| |
اين يك بخش از موضوع ويندوز سرويس چيست؟ است كه در انجمن مقالات برنامه نویسی مطرح گرديده و اين انجمن نيز زير مجموعهي برنامه نویسی است: ترجمه: كامران عبدي قيداري Fardis_online@yahoo.com در اين مقاله چگونگي ايجاد، نصب و ديباگ برنامه هاي كاربردي اي موسوم به ويندوز سرويس توضيح داده خواهد شد. كه شامل كلاس هاي فضاي كاري System.ServiceProcess.ServiceBase نيز مي شود. Windows Service چيست؟ برنامه اي است كه به محض بالا آمدن ويندوز شروع مي شود ...
| ثبت نام | پست جدید | All Albums | Blogs | راهنما | فهرست کاربران | تقویم | جستجو | ارسالهاي امروز | نشانه گذاري انجمن ها به عنوان خوانده شده |
| | ||||||||
| اطلاعيههاي سايت |
|
| LinkBack | ابزارهای موضوع |
| | #1 | |||||||||||
| عضو جديد تاریخ عضویت: November 28th, 2006
نوشته ها: 9
سطح دانش: 1 [ ]سابقه در سایت: 0 / 21 Thanks: 2
Thanked 16 Times in 8 Posts
قدرت اعتبار: 0 | ترجمه: كامران عبدي قيداري Fardis_online@yahoo.com در اين مقاله چگونگي ايجاد، نصب و ديباگ برنامه هاي كاربردي اي موسوم به ويندوز سرويس توضيح داده خواهد شد. كه شامل كلاس هاي فضاي كاري System.ServiceProcess.ServiceBase نيز مي شود. Windows Service چيست؟ برنامه اي است كه به محض بالا آمدن ويندوز شروع مي شود و در بك گراند تا زماني كه ويندوز در حال اجراست، اجرا مي شود. ويندوز سرويس بسيار شبيه به مفهوم daemon در سيستم عامل يونيكس است. (بطور مثال syslogd برنامه اي است كه براي هندل كردن لاگ سيستم بكار مي رود) ويندور سرويس معمولاً براي مدت طولاني روي سيستم عامل در حال اجراست و براي اجرا روي سرورها مناسب است. برنامه اي است كه اينترفيس ندارد و نيز هيچ خروجي ويژوالي ندارد. و هر پيغامي كه نياز باشد براي كاربر داده شود در event log نوشته مي شود. به محض اينكه ويندوز سرويس نصب شد خواص آن از طريق: ControlPannel | Administrative tools | Services قابل تغيير است. ويندوز اينترفيسي بنام Service Control Manager جهت مديريت، ايجاد، آغاز و پايان دادن به كار سرويس ها دارد. كه در سيستم عامل ويندوز ويستا اين كار به آساني از طريق MSConfig امكان پذير شده است. برنامه كاربردي اي كه قصد دارد تبديل به سرويس شود بايد طوري نوشته شده باشد كه امكان هندل كردن پيغام هاي start | stop | pause | … را از طريق Srvice Control Manager ويندوز داشته باشد. Permissions and implementation ويندوز سرويس بطور پيش فرض بعنوان كاربر مجازي "LocalSystem" اجرا مي شود. كه مجوز دسترسي مدير سيستم را دارد. Working directory اغلب C:\Winwods است و Temp Directory پيش فرض C:\WINDOWS\TEMP. است. از آنجايي كه LocalSystem يك كاربر واقعي نيست براي ذخيره ديتاي كاربر مشكلاتي وجود دارد. و نيز از آنجا كه اين كاربر Home directory ندارد به فايلهاي به اشتراك گذاشته شده در شبكه دسترسي ندارد اگر يك سرويس نياز به دسترسي به يك فايل روي شبكه داشت بايد طوري پيكربندي شود كه با دامنه يك كاربر كه به فايل هاي روي شبكه دسترسي دارد، اجرا شود. مي توان يك سرويس را طوري تنظيم كرد كه بعنوان يك كاربر اجرا شود ولي بايد كلمه عبور آن كاربر را براي آن سرويس ذخيره كرد. و بايد اين را در نظر داشت كه در صورت تغيير كلمه عبور ديگر آن سرويس كار نخواهد كرد و بايد كلمه عبور ذخيره شده براي آن سرويس را نيز تغيير داد. ايجاد يك ويندوز سرويس سرويسي كه ما ايجاد خواهيم كرد هيچ كاربردي ندارد و صرفاً بعنوان يك دمو ايجاد مي شود. وقتي سرويس شروع بكار كرد يك log entry در بانك اطلاعاتي ايجاد خواهد كرد به اين مفهوم كه سرويس شروع بكار كرد. اين سرويس تا زماني كه در حال اجراست در فواصل زماني معيني يك entry در بانك ايجاد مي كند. و هنگامي كه كار سرويس پايان يافت entry پاياني را در بانك اطلاعات ايجاد خواهد نمود. و نيز بطور اتوماتيك در صورتي كه آغاز و پايان موفقيت آميزي داشته باشد يك entry در بانك ايجاد خواهد كرد. Visual Studio.Net ايجاد ويندوز سرويس را ساده كرده است: 1. Start a new project 2. Select Windows Service from the list of available project templates 3. The designer will open in design mode 4. Drag a Timer object from the Components tab in the Toolbox onto the design surface (Warning: make sure you use the Timer from the Components tab and not the one from the Windows Forms tab) 5. Through the Timer properties set the Enabled property to False and the Interval property to 30000 milliseconds 6. Switch to the code behind view (F7) to add functionality to the service 1. از منوي فايل New Project را انتخاب كنيد. 2. نوع پروژه را Windows Service انتخاب كنيد 3. يك فايل Srvice1.cs در مد Design ايجاد خواهد شد 4. يك شئ Timer از قسمت Toolbox | Component به روي صفحه بكشيد. ( مطمئن شويد كه اين شئ را از قسمت Component انتخاب مي كنيد نه از فسمت Windows Forms) 5. مشخصات زير را براي شئ Timer ست كنيد: Enabled = false Interval = 300000 6 . كليد F7 را بزنيد تا توابع مربوط به سرويس را ايجاد كنيد. Makeup of a Windows Service در كلاس Code behind لازم است كه سرويس شما متد هاي زير را override كند. • OnStart - control the service startup • OnStop - control the service stoppage Sample Database Table ****** كد زير كه T-SQL است كه براي ايجاد يك جدول در بانك اطلاعات Sql Server مورد استفاده قرار خواهد گرفت. CREATE TABLE [dbo].[MyServiceLog] ( [in_LogId] [int] IDENTITY (1, 1) NOT NULL, [vc_Status] [nvarchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [dt_Created] [datetime] NOT NULL ) ON [PRIMARY] Sample Windows Service using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.ServiceProcess; namespace CodeGuru.MyWindowsService { public class MyService : System.ServiceProcess.ServiceBase { private System.Timers.Timer timer1; /// <remarks> /// Required designer variable. /// </remarks> private System.ComponentModel.Container components = null; public MyService() { // This call is required by the Windows.Forms // Component Designer. InitializeComponent(); } // The main entry point for the process static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.timer1 = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize) (this.timer1)).BeginInit(); // // timer1 // this.timer1.Interval = 30000; this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed); // // MyService // this.ServiceName = "My Sample Service"; ((System.ComponentModel.ISupportInitialize) (this.timer1)).EndInit(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } /// <summary> /// Set things in motion so your service can do its work. /// </summary> protected override void OnStart(string[] args) { this.timer1.Enabled = true; this.LogMessage("Service Started"); } /// <summary> /// Stop this service. /// </summary> protected override void OnStop() { this.timer1.Enabled = false; this.LogMessage("Service Stopped"); } /* * Respond to the Elapsed event of the timer control */ private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { this.LogMessage("Service Running"); } /* * Log specified message to database */ private void LogMessage(string Message) { SqlConnection connection = null; SqlCommand command = null; try { connection = new SqlConnection( "Server=localhost;Database=SampleDatabase;Integrated Security=false;User Id=sa;Password command = new SqlCommand( "INSERT INTO MyServiceLog (vc_Status, dt_Created) VALUES ('" + Message + "',getdate())", connection); connection.Open(); int numrows = command.ExecuteNonQuery(); } catch( Exception ex ) { System.Diagnostics.Debug.WriteLine(ex.Message); } finally { command.Dispose(); connection.Dispose(); } } } Install the Windows Service ويندوز سرويس با يك برنامه كاربردي معمولي تحت ويندوز تفاوت دارد اجراي اين نوع برنامه با اجراي ساده يك exe امكان پذير نيست. ويندوز سرويس بايستي با برنامه اي بنام InstallUtil.exe كه توسط فريم ورك دات نت ارائه مي شود و يا از طريق يك پروژه deployment نظير Microsoft Installer (MSI) نصب شود. Add an Installer صرفاً داشتن يك برنامه ويندوز سرويس براي برنامه InstallUtil جهت نصب كافي نيست. شما بايد يك Installer به سرويس خود اضافه كنيد تا برنامه نصب كننده پيكربندي لازم براي نصب سرويس شما را بداند. 1. Switch to the design view for the service 2. Right click and select Add Installer 3. Switch to the design view of the ProjectInstaller that is added 4. Set the properties of the serviceInstaller1 component 1. ServiceName = My Sample Service 2. StartType = Automatic 5. Set the properties of the serviceProcessInstaller1 component 1. Account = LocalSystem 6. Build the Solution كد زير بطور اتوماتيك توسط ويژوال استوديو به ProjectInstaller.cs اضافه خواهد شد: using System; using System.Collections; using System.ComponentModel; using System.Configuration.Install; namespace CodeGuru.MyWindowsService { /// <summary> /// Summary description for ProjectInstaller. /// </summary> [RunInstaller(true)] public class ProjectInstaller : System.Configuration.Install.Installer { private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; private System.ServiceProcess.ServiceInstaller serviceInstaller1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public ProjectInstaller() { // This call is required by the Designer. InitializeComponent(); // TODO: Add any initialization after the InitComponent call } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); // // serviceProcessInstaller1 // this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; // // serviceInstaller1 // this.serviceInstaller1.ServiceName = "My Sample Service"; this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; // // ProjectInstaller // this.Installers.AddRange(new System.Configuration.Install.Installer[] {this.serviceProcessInstaller1, this.serviceInstaller1}); } #endregion } } Use InstallUtil to Install the Windows Service اكنون كه پروژه Build شد بايد جهت استفاده نصب شود. گامهاي زير شما را براي نصب سرويس راهنمايي مي كند: 1. Open a Visual Studio .NET Command Prompt 2. Change to the bin\Debug directory of your project location (bin\Release if you compiled in release mode) 3. Issue the command InstallUtil.exe MyWindowsService.exe to register the service and have it create the appropriate registry entries 4. Open the Computer Management console by right clicking on My Computer on the desktop and selecting Manage 5. In the Services section underneath Services and Applications you should now see your Windows Service included in the list of services 6. Start your service by right clicking on it and selecting Start هرگاه سرويس شما نياز به تغيير داشت بايد ابتدا سرويس را Uninstall كرده، تغييرات را اعمال كنيد سپس دوباره سرويس را نصب كنيد. قبل از Uninstall سرويس بستن Services management console ايده خوبي است. در غير اينصورت ممكن است هنگام Uninstall دچار مشكل شويد. براي Uninstall سرويس همان دستور كه هنگام رجيستر بكار برده شد را بكار بريد با اين تفاوت كه به انتهاي آن /u نيز اضافه كنيد. …bin/debug/ InstallUtil.exe WindowsService1.exe /u اشكال زدايي ويندوز سرويس Debug the Windows Service نظير ديگر كارها، اشكال زدايي ويندوز سرويس نيز با ديگر برنامه هاي معمولي متفاوت است. براي اشكال زدايي ويندوز سرويس كارهاي بيشتري لازم است كه انجام شود. اشكال زدايي برنامه مثل ساير برنامه ها از طريق اجرا در محيط برنامه نويسي ميسر نيست. ابتدا بايد سرويس نصب و اجرا شود (همانطور كه در بخش هاي قبل توضيح داده شد.) جهت debug به محض اجراي سرويس، شما بايد Visual Studio را به پروسس اجرا ضميمه كنيد. به خاطر داشته باشيد كه هر گونه تغيير در كد برنامه مستلزم اين خواهد بود كه سرويس را Uninstall و دوباره نصب كنيد. Attach to a Running Windows Service دستورالعمل زير فرض مي كند كه شما سرويس را نصب و اجرا كرده ايد. 1. Load the project into Visual Studio 2. Click on the Debug menu 3. Click on theAttach to Process menu item 4. Make sure the Show system processes is selected 5. Locate your process in the Available Processes list based on the name of your executable and click on it 6. Click the Attach button 7. Click OK 8. Click Close 9. Set a break point in the timer1_Elapsed method and wait for it to execute Summary You should now have a rough idea of what windows services are, how to create, install, and debug them. There is additional functionality with Windows Services that you can explore. This functionality includes the capability to pause (OnPause) and resume (OnContinue). The ability to pause and resume are not enabled by default and are setup through the Windows Service properties. Source: http://www.developer.com/net/csharp/...0918_2173801_1 | |||||||||||
| |
| برچسب ها |
| چيست؟ , ويندوز , سرويس |
| ابزارهای موضوع | |
| |
| ||||
| موضوع | نویسنده موضوع | انجمن | پاسخ ها | آخرين نوشته |
| نحوه راه اندازي يك Isp | Kamran1358 | سخت افزار | 1 | Friday 23 February 2007 10:06 PM |
| چرا ويستا؟ | majidsat | سیستم های عامل | 3 | Monday 29 January 2007 10:36 PM |
| تنظيمات بهتر براي ويندوز Xp | vqorbani | موضوعات متفرقه - مرتبط با کامپيوتر | 0 | Friday 10 November 2006 07:17 AM |
| رجيستري چيست؟ | sei_pm | مباحث ویندوز (Windows) | 1 | Monday 21 August 2006 01:27 PM |
| نگاهي به Storage Server 2003 | atn_1366 | ساخت شبکه NetWork | 0 | Wednesday 26 July 2006 11:35 AM |
تمامي قوانين اين سايت از جمهوري اسلامي ايران پيروي مي کند و هرگونه مطالب مخالف قوانين ايران و بنر يا لينک مستهجن در اين سايت جايي ندارد