Model-Driven Development is a hot topic in the developer
community, but many still don’t know how to make it work in
real-life projects. By offering developers built-in guidance,
tangible architect makes Model-Driven Development finally
accessible to everyone. I have been working on a new custom UML
designer for making the design process a lot easier than using
generic UML designer. It will be released soon. Check out the cool
app:
Category: Blog Posts
Simulate Windows Service using ASP.NET
How to run scheduled jobs from ASP.NET without requiring a
Windows Service to be installed on the server? Very often we need
to run some maintenance tasks or scheduled tasks like sending
reminder emails to users from our websites. This can only be
achieved using a windows service. ASP.NET being stateless provides
no support to run some code continuously or to run code at
scheduled time. As a result, we have to make our own Windows
Services in order to run scheduled jobs or cron jobs. But in a
shared hosted environment, we do not always have the luxury to
deploy our own windows service to our hosting provider’s web
server. We either have to buy a dedicated server which is very
costly, or sacrifice such features in our web solution. However,
running scheduled task is a very handy feature especially for
sending reminder emails to users, maintenance reports to
administrator, run cleanup operations etc. So, I will show you a
tricky way to run scheduled jobs using pure ASP.NET without
requiring any windows service. This solution runs on any hosting
service providing just ASP.NET hosting:
http://www.codeproject.com/useritems/ASPNETService.asp
If you find this article useful, please vote for me.
.NET 2.0 Process Impersonation feature not working in Windows App
.NET 2.0 introduced an interesting feature in the “Process”
class where you can specify UserName, Password and it runs the
process as the specified user. However, it only works inside
Console Application. It does not run if the application is a
Windows Application. It throws “Win32Exception Access Denied” when
the Start() is called. Is it as designed? I really need this
feature in a windows service.
Try the following code in a console app, it will run perfectly.
But change the application type to “Windows Application” and
run, it will throw exception. IF you do not set the UserName,
Password, it will run then. I am using VS 2005 Release
Candidate.
Here’s the code:
ProcessStartInfo startInfo = new
ProcessStartInfo(@”SampleEXE.exe”);
startInfo.Domain = “.”;
startInfo.UserName = “test”;
// create a secure string from the password we have
SecureString securePassword = new SecureString();
foreach (char c in “test”)
securePassword.AppendChar(c);
securePassword.MakeReadOnly();
startInfo.Password = securePassword;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.WorkingDirectory =
Path.GetDirectoryName(startInfo.FileName);
startInfo.Arguments = “Hi Hello How are you?”;
using (Process p = new Process())
{
p.StartInfo = startInfo;
p.Start();
string fileName = @”output.txt”;
using (StreamWriter writer = new StreamWriter(fileName))
{
writer.Write(p.StandardOutput.ReadToEnd());
writer.Close();
}
p.WaitForExit();
p.Close();
}