Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for December, 2010

Creating Window Service with .NET VS2008

Posted by Viral Sarvaiya on December 31, 2010


Hi,

Last post of year 2010, and wish u happy new year…

today i post the basic thing of the window service.

What is a Windows Service?

Windows Service applications are long-running applications that are ideal for use in server environments. The applications do not have a user interface or produce any visual output. Any user messages are typically written to the Windows Event Log.

Windows Services are controlled through the Service Control Manager where they can be stopped, paused, and started as needed.

Create a Windows Service

The service we will create does nothing really useful other than serve as a demonstration. When the service is started we will log an entry in a database indicating that it has started. The service will create a database entry on a specified interval during the time in which it is running. The service will create a final database entry when stopping. The service will also automatically log an entry in the Windows Application Log when it is successfully started or stopped.

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. Take the time control to the designer(make sure it is not windows control, it has to


public System.Timers.Timer timer1;

5. set the Enabled property to False and the Interval property to 30000 milliseconds

now come to service.cs page..

there are class that extend ServiceBase class with there are constractor and 2 mehtod already created is start and stop.

6. Create table “ServiceLog” as per blow


CREATE TABLE MyServiceLog (
Id int IDENTITY (1, 1) NOT NULL,
Status varchar (100),
Created datetime NOT NULL
)

7. Create event timer’s ElapsedEventHandler timer1_Elapsed event

in the service.cs file


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Data.SqlClient;

namespace WindowsServiceTest
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
this.LogMessage("Service Started");
}

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("packet size=4096;server=ServerName;persist security info=False;database=DataBaseName;uid=Username;pwd=Password;");
command = new SqlCommand("INSERT INTO MyServiceLog (Status, 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();
}
}
}
}

8. Now Install the Windows Service

The Windows Service should be installed by using the InstallUtil.exe provided with the .NET Framework or through a deployment project such as a Microsoft Installer (MSI) file.

You must also add an Installer to your Windows Service so that the InstallUtil, or any other installation program, knows what configuration settings to apply to your service.

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

The file ProjectInstaller.cs automatically made.

Install service with InstallUtil.exe

1. Open Visual Studio .NET command prompt

2. Go to the debug or release folder

3. Run command InstallUtil.exe MyWindowsService.exe

4. Open services from control panel -> administrative tools

5. Find the Service and right click on it and start the service.

6. Check the database. log will inserted to database in every 3 second

Enjoy Window service.

http://msdn.microsoft.com/en-us/library/d56de412

Advertisements

Posted in .Net, Visual Studio, Window Service | Tagged: , , | 1 Comment »

Create shortcut of application programmatically

Posted by Viral Sarvaiya on December 28, 2010


Hi,

Merry Christmas to all…

In my one of the application I get one new thing that I share here,

I m creating desktop icon  of one application from the already running application.

Here is the code…

For that you have add reference of “IWshRuntimeLibrary”


using IWshRuntimeLibrary;

and then call below function to the click event of the button from where u want to create the desktop icon.

private static void CreateShortcutOfApplication()
 {
 // Create a new instance of WshShellClass
 WshShell = new WshShellClass();

 // Create the shortcut
 IWshRuntimeLibrary.IWshShortcut MyShortcut;

 //Get Path of Desktop Folder of Current User
 string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

 // Choose the path for the shortcut and name of the shortcut
 MyShortcut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(path + @"\ShortcutName.lnk");

 // Where the shortcut should point to
 MyShortcut.TargetPath = Application.StartupPath + "\\ApplicationName.exe";

 //Set Working Directory of Shortcut
 MyShortcut.WorkingDirectory = Application.StartupPath;

 // Description for the shortcut
 MyShortcut.Description = "Description of the Application";

 // Location for the shortcut's icon
 MyShortcut.IconLocation = Application.StartupPath + @"\Application.exe";

 // Create the shortcut at the given path
 MyShortcut.Save();
 }

And when you click on this button then you get the icon of that application in desktop.

Hope this will helpful to you…

Posted in ASP.NET | Tagged: , , , , | 1 Comment »

Different Routes for Different Hosts Using Constraints

Posted by Viral Sarvaiya on December 7, 2010


Something we discussed that I’ve come across before is using different routes depending upon the domain that the end user is browsing. That might be different based on language (browsing your “.co.uk” site gets English URLs, browsing “.fr” gets French), or perhaps based on brand. Maybe some functionality on your web site is only available to certain sub-brands. Or perhaps you want to direct users that visit uat.mydomain.com to new functionality so they can test it.

For More Details.

http://blogs.msdn.com/b/simonince/archive/2010/12/02/different-routes-for-different-hosts-using-constraints.aspx

Posted in ASP.NET | Tagged: , , , , | Leave a Comment »

 
%d bloggers like this: