Wednesday, February 24, 2010

Creating a C# Windows Service

Creating a Windows Service can be simple if you have all the right information. What I will show you today will teach you how to create a Windows Service and run that service on Windows XP.

Let's begin:
Launch Visual Studio





Step 1: Create a Skeleton Project

File > New > Project

A new project dialog box will appear giving you options for creating a new project.

Let's narrow down the project type.

From the tree view on the left side of the dialog box select Windows.

This will only display Templates for creating Windows projects. Select Windows Service





Step 2: Project Name & Settings

Give your windows project a name, then click OK to create your project.


Your project Window Window will look like this:



Set your ServiceName to your own name so that you will recognize it when you look at services in Computer Managerment under Services tree. I used LMCListener as my service name. It might be difficult to find the properties to do that. Here is what you do. When the project wizard is completely launched, the first window that appears is the Design View for Service1.cs. Your properies will most likely not display the Service Processes so, simply move your mouse to the Service1.cs tab at the top of your project winds, then in the Properties you will then see ServiceName at the bottom of the list.











Step 3: Adding code to the code view, right click on the gray area of the Service1.cs window and choose View Code

That will give you the following:







I've provided you with some sample code to help you create your first project. Here's the code below:


protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Started \n");
m_streamWriter.Flush();
m_streamWriter.Close();
}

protected override void OnStop()
{
FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Stopped \n"); m_streamWriter.Flush();
m_streamWriter.Close();
}


Now Build your application which will produce an exe file located in the debug > bin folder of your project.



Step 4: The next step would be to Install and Run the Service.


You will do that using an install utility provided as part of the .NET Framework. But before we can install the service let's add some additional features to our service.

I'm not saying that our service is nonfunctional. It will work as is, but what if we want to have the service description and other things show up in our processes panel, or Services Panel. Here's the trick.



If you are not on the Service1.cs Design window, then go there. Now right click on the gray area of the window and choose Add Installer. The Installer objects will be added to your Design window at the top left corner.

There will be two files in that location: ServiceProcessInstaller1 and ServiceInstaller1, both will need their properties configured.

ServiceProcessInstaller1 - Account, this is the user account that the service will run under, set this to LocalSystem.


ServiceInstaller1 - Description, this is the description that will appear in the services admin tool. Set this to Empty Service Description

ServiceInstaller1 - Display Name, this is the service name that will appear in the services admin tool. Set this to Empty Service

ServiceInstaller1 - ServiceName, Set this to EmptyService.

ServiceInstaller1 - StartUpType, this determines whether the service will start automatically with windows, set this to Manual



Alright, thats it, you cannot run a service as you would a windows program. The .net framework has provided a tool that will install the service for you. The InstallUtil.exe program is located in the framework folder. From the command prompt type:

%Windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe C:\YourServiceName .exe

where C:\YourServiceName .exe is the path to you service, to uninstall the service type:

%Windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u C:\YourServiceName .exe

Once your service is installed you will find it in the Services admin tool by typing Services.msc at the run prompt:

Note make sure C:\YourServiceName contains the entire path to the exe file your created.

Monday, February 22, 2010

Fake A Call Palm WebOS Application

I developed my third Palm App which was published to the App Catalog. See _here: for a look at the app and its roadmap.

It had a rating of 5 on the 5th day of release. But today, someone posted but did not rate. It's so interesting to me that people would take the time out to destroy another persons reputation to promote their own interest.

Someone did the same thing with my first two applications by telling people, the app destroyed my phone or I downloaded this app several times and it doesn't work.

People are driven by perception and do think for themselves. Palm checks every application that comes through. If the application would destroy a mobile phone, then Palm would be liable and replace the phone and remove the application.

There are legal restrictions and such that we developers must abide by. The tactics of other developers who are competing, well God is watching. One thing that I know for sure, that Whom The Lord Blesses, no man can curse. God will turn their curse against them.

So, for those who are developers or non-developers who wish to harm another person at their own interest. You will suffer, it may not be financially, or something you see with your eyes. But you will not know true peace.

Think of it like this. If a chain is around your neck all your life and a free person came along and said that you are bound. Would you know it? Of course not, you've never known freedom. Some people in boundage will work to have freedom while still in chains. And will associate change as freedom.

Anyway, I'm gone for today. Hey, check out my church website too:

Notepad++ and XML Formatting

I downloaded Notepad++ to use as part of my development environment. I like the convenience of the app. It works a thousand times better than the regular notepad that comes with windows and it also does things that you would not expect from a name Notepad.

I guess when the mind thinks notepad, we think limited app.

Anyway, lately I've been working with lots of XML inside of a table. I've been copying xml files and pasting them into Visual Studio and working from their. But VS 2008 takes a long time to load and all I want to do is look at the XML file and code against it.

So I opened Notepad++. This is when I ran into a problem. The text pasted as one long string on line 1.

I searched for a method for formatting the xml. The web suggested Tidy. I downloaded the command line tidy.exe and that was out of scope. I didn't want to use two apps to accomplish formatting text, so I did additional searches and discovered that, in the new addition of Notepad++, tidy is included. That is version 5.6.6.

This is how to format your XML files.
In Notepad++ choose
> TextFX
> HTML Tidy
>Tidy Reindent XML

and the beauty of a thing is revealed.

Thursday, February 18, 2010

Palm WebOS

My experiences programming for Palm WebOS has been an interesting adventure. I'm enjoying this new path of programming for mobile devices. I've written several programs, off and on, for Windows mobile devices, but none that I sold to others. I wanted to code for the iPhone, but I like things to be simple and coding for the iPhone took lots of my time.

I already knew HTML, Javascript, JAVA, C#, CSS, ADO.NET, ASP.NET, AJAX, SQL, MYSQL, PHP, and some C and C++. So I needed a language that didn't require me to start over again.

Palm WebOS became exciting to me. I was one of the first developers to port my application to the Pre. It was difficult at first because I needed to learn the syntax of a few languages I've never encountered before.

SQLite, Mojo, JSON, Prototype! What in the world are these? One of the problems learning a new language when you know so many is that you have to almost become a child to learn the new, otherwise you will automatically try to solve the new development problems using the same language structure of the old.

Just like not knowing about coding, and learning a new language has its restraints and learning curve.

What I've learned about UI development for Palm WebOS has increased my UI development for desktop and web apps; I don't have words to express the appreciation.

Programming for the Pre and Pixie requires a UI developer to create an environment that is simple and yet communicates exactly what the user needs to know. Don't make me think.

If a person has to think about what they are doing, then the application is not working. I've purchased and downloaded free applications from the Palm Catalog and could not figure out what to do.

There was one gun application that tried to tell me to cock the rifle by holding the phone up and pushing forward. It didn't work. I couldn't figure it out. I read in detail, wrote it down and nothing worked. Either the app was broke or I was dumb.

Anyway, I've been developing UI and coding for more than 10 years and learned a great deal about what people want and how to speak to them with my applications. I am happy that Palm came out with such a great roadmap.

It doesn't take a lot of my time to develop a great application. I created my third application in 4 days. It's called Fake A Ring!

To all those who wish to learn and make money having fun. It's a great place to be.

Large file uploads in ASP.NET

Uploading files via the FileUpload control gets tricky with big files. The default maximum filesize is 4MB - this is done to prevent denial of service attacks in which an attacker submitted one or more huge files which overwhelmed server resources. If a user uploads a file larger than 4MB, they'll get an error message: "Maximum request length exceeded."
Increasing the Maximum Upload Size

The 4MB default is set in machine.config, but you can override it in you web.config. For instance, to expand the upload limit to 20MB, you'd do this:



Since the maximum request size limit is there to protect your site, it's best to expand the file-size limit for specific directories rather than your entire application.

That's possible since the web.config allows for cascading overrides. You can add a web.config file to your folder which just contains the above, or you can use the tag in your main web.config to achieve the same effect: