Things you may need to know, when want to start working on Windows Services

As you may know, recently, I’ve done a project based on Windows Mobile for a hotel and because of their restaurants orders.

As one of their requests, they wanted me to use their previous ordering system in order to print bills. Their previous developer had been developed a parametric exe file that takes 4 parameters and then prints the bill out. So I needed to develop something on their servers in order to execute that old application. I decided to write a Windows Service that takes pocket pc requests via a wireless network connection and then print the bill out.

Working with Windows Service is not very often so you may not be able to find good articles for it but in this post I want to write about Windows Services.

Windows Service classes are very similar to normal classes except that you must implement them from the ServiceBase class. ServiceBase class is available in System.ServiceProcess namespace.

Keep in mind that you must import the System.ServiceProcess name space in your application because you will need this when want to work with Windows Services.

When you implemented your class from ServiceBase class, Visual Studio adds two methods to your class named “OnStart” and “OnStop”.

OnStart method:

This method allows you to write code you want to be executed when starting the service. You will need this method the most!

The OnStart method has an input parameter (A string array) which takes parameters that you may want to send to your service.

OnStop method:

This method will be executed when you’re stopping your service. Imagine that you’ve created a Thread in order to do something but you need to kill it when you stopped the service. In order to do such a thing, you have to make use of OnStop method.

Now what?

Adding an installer - Photo taken by myself. After writing your codes, it’s time to use your service. To make your service usable, you must add an installer to your application. Adding installer is easy when working with Microsoft Visual Studio; just right-click on your service designer and select “Add Installer” like the following picture.

When adding an installer, Visual Studio adds a class named “ProjectInstaller” and then adds two controls to your installer class named “ServiceInstaller” and “ServiceProcessInstaller”. By selecting each of those controls you can set some properties such as ServiceName, Description, DisplayName, etc.

Note: While setting the properties, don’t forget to verify the ServiceName property that must set to your service class.

You can also add your own codes to the ProjectInstaller class depend on your service.

How to install a Windows Service?

After building the service, you can access its files by going to the debug or release folder. You got two ways (Correct me if I’m wrong) to install a Windows Service.

· Programmatically.

· Manually.

Install or uninstall a Windows Service using Command Prompt - Photo taken by myself In this post, I want to install our service manually because it’s more common.

In order to install a Windows Service, you need to use Microsoft Visual Studio Command Prompt.

In the Command Prompt, we have an “installutil.exe” file that enables you to install and uninstall your Windows Services. Here is the syntax:

For install: installutils.exe [Your file path].
For uninstall: installutil.exe /u [Your file path].

A start point to work with WCF services

It’s about 3 months that I’m researching on WCF because of a reservation system I’m developing right now. We’re working on a web service which enables our customers to reserve cars, tickets, etc.; so we need something like XML Web Service to do such a thing.

WCF is a new framework for building service-oriented applications. Microsoft wanted to provide its developers with a framework to quickly get a proper service-oriented architecture up and running. Using the WCF, you will be able to take advantage of all the items that make distribution technologies powerful. WCF is the answer and the successor to all these other message distribution technologies.

In this post, I’m going to show you how to develop your first WCF application using Microsoft Visual Studio 2008.

This first step is to create a new WCF application. Visual Studio implemented a template for it so you can create it easier:

Creating a new WCF application - Photo taken by myself.

Note that WCF Service Applications are available in .NET Framework 3 and 3.5 so you cannot make a use of WCF when you’re developing applications using .NET Framework 2 or below!

When want to create a WCF application, you’ll need to follow some rules:

First Step

The first step is to create an interface and list your methods you want to use in your service:

[ServiceContract]
public interface IService1
{
 
    [OperationContract]
    string GetData(int value);
 
}

Everything is normal except that, you must use some attribute in order to create WCF service’s interface. First, you must apply an attribute named “ServiceContract” to your interface. Second, you have to apply another attribute named “OperationContract” to each of your methods you want to use in your WCF service. Note that you need to import System.ServiceModel name space to your interface in order to make use of “ServiceContract” and “OperationContract” attributes.

Second Step

Now it’s time to create our WCF service by adding a new WCF service to our application. The important part when you added a new WCF service to your application is that you must implement your WCF service class from the interface you’ve just created. After you created your service class, you can start writing codes in your specified methods:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("Here is your value: {0}", value); 
    }
 
}

Third Step

When you create a new interface and class in a WCF application, you must apply some settings to your Web.Config file. Visual Studio automatically adds some lines of settings to your Web.Config file when you create a WCF application that contains the name of your interface and your service class.

Keep in mind that, you must update those file names in your Web.Config file if you want to change the name of that Service1.cs and Service1.svc. You can also add more interfaces and services if you want but you must apply them in Web.Config file:

<system.serviceModel>
<services>
  <service name="SampleService.Service1" 
  behaviorConfiguration="SampleService.Service1Behavior">
    <endpoint address="" binding="wsHttpBinding" 
    contract="SampleService.IService1">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" 
    contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="SampleService.Service1Behavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

Merry Christmas

Merry Christmas - Photo taken from http://blueroof.files.wordpress.com/2007/12/merry-christmas.png The year 2008 is going and a new fantastic year is coming. Lots of things I did in the year 2008 and now it’s time to schedule for 2009.

Schedule for things to do and plan for new purposes.

Previously, I blogged about things I have done in the year 2008; however I had some bad times but generally, I think this year was good because I enjoyed a lot, experienced lots of things and learned a lot. I hope I can make it better in the following year (2009) and achieve more valuable things.

Anyway, Merry Christmas to all people around the world especially those who helped me a lot to think wider and do things better.

SQL Server 2005 SP3 is now up

As you may know, my main focus in programming based on databases such as SQL Server, DB2, Oracle, etc. and it’s about 3 years I’m in depth to Microsoft SQL Server.

When I started working with SQL Server 2008, I realized it has a lot of bugs and I stopped working on my new SQL Server 2008-based project until Microsoft released a Cumulative update package for SQL Server 2008 that has solved lots of bugs and more. So I continued my work on my SQL Server 2008 project. The problem was that, I’ve done a lot of projects based on SQL Server 2005 and cannot upgrade them to 2008 because of customer’s situation.

Recently, Microsoft released SP3 of SQL Server 2005 and now it’s ready to download.

SQL Server 2005 SP3 is now available for download and includes the following updates:

  • Roll-up of Quick Fix Engineering (QFE) updates completed since SQL Server 2005 SP2, as well as fixes to critical issues reported through the SQL Server community. Per revised servicing strategy for SQL Server releases, SP3 does not introduce major new features.
  • Feature Pack for SQL Server 2005 SP3 has been refreshed along with this release.
  • SP3 can be used to upgrade from any previous instance of SQL Server 2005
  • SP3 applies to:
    • SQL Server 2005 Enterprise Edition
    • SQL Server 2005 Enterprise Evaluation Edition
    • SQL Server 2005 Standard Edition
    • SQL Server 2005 Workgroup Edition
    • SQL Server 2005 Developer Edition

As Microsoft said, “SQL Server 2005 SP3 designed to work with Windows Vista and Windows Server 2008”.

Mac OS cursors instead of Windows when using Visual Studio Dark environment

Mac OS Cursors - Photo taken from http://blog.j3zu5.nl/img/macosx.jpg A while ago, Scott Hanselman posted about “Visual Studio Programmers Theme Gallery” that contains some dark themes.

Using dark themes will help a developer because human eyes are sensitive toward light colors especially white. As you know, Visual Studio contains lots of light colors and the code view is almost white. Most of developers are building a custom theme for themselves that contains dark colors as you can see in Scott’s post.

When working with dark themes and Windows default cursors, it’s hard to recognize some of the cursors such as Text Select cursor. Today I told one of my developer friends to use dark schema instead of light to have less tiredness and he told me I cannot recognize cursors; so what to do?

Every developer have his/her own solution but I myself prefer to use Mac OS cursors instead of Windows cursors because it contains both white and black colors which helps you out to recognize the cursor in any environment.

You can download Mac OS cursors in one Zip file by click here.

Useful tips about Visual Studio 2008

Performance is one of the most important advantages of any developer, in my opinion. When I’m developing applications using Visual Studio, I really like to use shortcut keys because they’ll improve my performance while programming.

About 2 months ago, Stephen Walther blogged about some tips that every developer should know about Visual Studio and because it was so handy, I decided to blog it today. If you really observe these tips, you’ll be able to improve your programming performance a lot.

In order to read Stephen’s post, just click here.

Oxite, An open source CMS based on ASP.NET MVC by Microsoft

 Oxite Logo - Photo taken from http://www.codeplex.com/oxite I know it’s a little old but in this post I want to talk about Oxite.

Oxite is a new open source blogging system based on ASP.NET MVC which built by Erik Porter, Nathan Heskew, Mike Sampson and Duncan Mackenzie.

This is a simple blog engine written using ASP.NET MVC, and is designed with two main goals:

· To provide a sample of ‘core blog functionality’ in a reusable fashion. Blogs are simple and well understood by many developers, but the set of basic functions that a blog needs to implement (trackbacks, RSS, comments, etc.) are fairly complex. Hopefully this code helps.

· To provide a real-world sample written using ASP.NET MVC.

As they said, this blog engine built not only for developers and as sample and this has been used in MIX Online too.

Here is some other information from http://visitmix.com/lab/oxite:

Oxite was developed carefully and painstakingly to be a great blogging platform, or a starting point for your own web site project with CMS needs. Its line-up of sexy attributes includes: provider-based architecture allowing you to swap out database and search providers (SQL Server DB, local and Live search providers included), built for testability and hence most likely to be voted "hottest in class" by TDD fans (repositories, everything has an interface, etc.), database file and string resource storage so that files get stored in database and strings stored for localization, built to take full advantage of ASP.NET MVC but broken into assemblies so that even ASP.NET WebForm developers can use the data backend and utility code, supports use of Visual Studio Team Suite (DB Pro, Test, etc.), and Background Services Architecture (sending trackbacks, emails, etc. all done as a background process to prevent delays on the web site itself).

Download it now and get some other information by click here

What happened this year till now?

Thanks God - Photo taken from http://stlukesgolden.net/Images/girl_praying.jpg I think it’s time to talk about things that had been happened in this year (2008).

This year started with a good point. I took MCITP (Microsoft Certified IT Professional) Database Developer certification that was so costly. This was one of those certifications that made me feel professional for a while.

After that I changed my mind and focused on Microsoft .NET Framework more than ever. Because of that and with my friend Omid Mafakher’s helps, I improved a lot in .NET platform.

I have done lots of projects such as my Pocket PC Project, a Calculation framework, some web application, etc. I tried to pick some difficult project because as a matter of fact I didn’t care about money and my main concentration was on improvement and nothing else.

After that, I changed my office to another because of some problems I had. For example, they didn’t pay my payment for 4-5 months. I was so difficult to stand so I decided change my office to another company.

The new company business was different. It was based on hotel businesses like reservations, hotel accountings and more. When I went there, I realized, I know a lot because in my opinion they don’t know what’s what. So I employed as senior software engineer. It was so great to have a very cool title in a pretty large company in 21 years old.

It’s cool but to have a nice position but so late for me as I talked to my friend Amir, “It’s getting late”, I said. I know some people who’re working on development and have wider knowledge than me and of course they are much older but I’m thinking about being the best when I’m 25.

Just 4 years left. In order to be the best, I think I must think professional, behave professional and generally, live professional.

A new Windows Live has been released

Windows Live New Look - Photo taken by myself. Today, when I wanted to start checking my email, I checked out http://home.live.com to see what’s new and I saw big changes in Windows Live. As I checked out Windows Live Team Blog lots of changes has been applied and now ready to use. For example Windows Live SkyDrive is now 25GB!

Another interesting feature that’s so similar to Facebook and Twitter is “What Are You Doing?” section that enables users to inform friends about their current status.

I wanted to write about this but Windows Live Team described it much better than. For information about Windows Live new features click here.

Manage your users with the new ASP.NET 3.5 Personalization feature – Part 1

Nearly all developers who’re developing web applications have been working with sessions, cookies, etc.

For example, when you want to authenticate your users you store you user’s authentication information in sessions and cookies. Previously, I posted about ASP.NET Form Authentication which most of ASP.NET developer are working with it because it’s one of the most stable ways for user authentications.

ASP.NET 3.5 provides a new feature named “Personalization” that has very flexible infrastructure enables you to store user’s information in server not client (Cookies store information on client computers). In this post, I’m going to show you how you can use profiles and I’m sure you’ll love it.

ASP.NET has a deep focus on web.config file. You can do lots of things in your configuration file. In order to work with profiles you need to let ASP.NET what user data you want to store. So you have to add some elements to you web.config file:

<system.web>
 
    <profile>
        <properties>
            <add name="FirstName"/>
            <add name="LastName"/>
            <add name="Age" type="System.Int32"/>
        </properties>
    </profile>
    
</system.web>

Web.Config file properties are accessible in code-behind - Photo taken by myselfAs you can see in the code, you determine what properties your profile should have. You can add them very easily and Visual Studio IntelliSense will help you too.

When you added your custom properties you can see them in code-behind so you can get or set you profile information.

Notice that “Age” property in the above code snippet, I just assigned a type attribute for it. It means that you can also specify the type of a property for example:

· string: System.String

· bool: System.Boolean

· int: System.Int32

· …

You can also assign your custom type. For example, imaging you want to store user’s cart information. You can store information in a class and then set the type of your profile property to that class. Here is an example:

<Serializable()> _
Public Class Cart
 
    Private _ProductName As String
    Private _Price As Decimal
    Private _Quantity As Int16
 
    Public Property ProductName() As String
        Get
            Return _ProductName
        End Get
        Set(ByVal value As String)
            _ProductName = value
        End Set
    End Property
 
    Public Property Price() As Decimal
        Get
            Return _Price
        End Get
        Set(ByVal value As Decimal)
            _Price = value
        End Set
    End Property
 
    Public Property Quantity() As Int16
        Get
            Return _Quantity
        End Get
        Set(ByVal value As Int16)
            _Quantity = value
        End Set
    End Property
 
End Class

Note that the class must be Serializable.

Now you can add your property with your custom type:

<add name="Cart" type="Cart" serializeAs="Binary" />

You can also provide default values for your properties. It means if you leave your property with no value, a default value will be replace with null:

<add name="Active" type="System.Boolean" defaultValue="false"/>

It possible to make your properties read-only:

<add name="Active" type="System.Boolean" readOnly="true"/>

I the next part, I’ll show you how to store profile information in a data source such as SQL Server, XML, etc.