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

By Mohammad Mahdi Ramezanpour at December 30, 2008 18:51
Filed Under: SmartDevices, 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

By Mohammad Mahdi Ramezanpour at December 28, 2008 21:40
Filed Under: WCF

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

By Mohammad Mahdi Ramezanpour at December 25, 2008 10:56
Filed Under: Other

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

By Mohammad Mahdi Ramezanpour at December 21, 2008 18:19
Filed Under: SQL Server

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

By Mohammad Mahdi Ramezanpour at December 19, 2008 20:28
Filed Under: Other

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.

Quote of the day