Tech blog

Solving problems

About
Contact

Archive for the ‘development’ tag

Visual Studio 2008 and Windows Service project and setup

leave a comment

Problem is that Visual Studio 2008 does not have Windows Service project and you can not add it and create a setup for this project.

To override this use manual approach:

1. Create your service class manually:

using System;
using System.Diagnostics;
using System.ServiceProcess;

namespace WindowsService
{
    class WindowsService : ServiceBase
    {
        ///
        /// Public Constructor for WindowsService.
        /// - Put all of your Initialization code here.
        ///
        public WindowsService()
        {
            this.ServiceName = "My Windows Service";
            this.EventLog.Log = "Application";

            // These Flags set whether or not to handle that specific

            //  type of event. Set to true if you need it, false otherwise.

            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;
        }

        ///
        /// The Main Thread: This is where your Service is Run.
        ///
        static void Main()
        {
            ServiceBase.Run(new WindowsService());
        }

        ///
        /// Dispose of objects that need it here.
        ///
        ///
Whether
        ///    or not disposing is going on.
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        ///
        /// OnStart(): Put startup code here
        ///  - Start threads, get inital data, etc.
        ///
        ///

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
        }

        ///
        /// OnStop(): Put your stop code here
        /// - Stop threads, set final data, etc.
        ///
        protected override void OnStop()
        {
            base.OnStop();
        }

        ///
        /// OnPause: Put your pause code here
        /// - Pause working threads, etc.
        ///
        protected override void OnPause()
        {
            base.OnPause();
        }

        ///
        /// OnContinue(): Put your continue code here
        /// - Un-pause working threads, etc.
        ///
        protected override void OnContinue()
        {
            base.OnContinue();
        }

        ///
        /// OnShutdown(): Called when the System is shutting down
        /// - Put code here when you need special handling
        ///   of code that deals with a system shutdown, such
        ///   as saving special data before shutdown.
        ///
        protected override void OnShutdown()
        {
            base.OnShutdown();
        }

        ///
        /// OnCustomCommand(): If you need to send a command to your
        ///   service without the need for Remoting or Sockets, use
        ///   this method to do custom methods.
        ///
        ///
Arbitrary Integer between 128 & 256
        protected override void OnCustomCommand(int command)
        {
            //  A custom command can be sent to a service by using this method:
            //#  int command = 128; //Some Arbitrary number between 128 & 256
            //#  ServiceController sc = new ServiceController("NameOfService");
            //#  sc.ExecuteCommand(command);

            base.OnCustomCommand(command);
        }

        ///
        /// OnPowerEvent(): Useful for detecting power status changes,
        ///   such as going into Suspend mode or Low Battery for laptops.
        ///
        ///
The Power Broadcast Status
        /// (BatteryLow, Suspend, etc.)
        protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
        {
            return base.OnPowerEvent(powerStatus);
        }

        ///
        /// OnSessionChange(): To handle a change event
        ///   from a Terminal Server session.
        ///   Useful if you need to determine
        ///   when a user logs in remotely or logs off,
        ///   or when someone logs into the console.
        ///
        ///
The Session Change
        /// Event that occured.
        protected override void OnSessionChange(
                  SessionChangeDescription changeDescription)
        {
            base.OnSessionChange(changeDescription);
        }
    }
}

2. Add installer class:

using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService
{
    [RunInstaller(true)]
    public class WindowsServiceInstaller : Installer
    {
        ///
        /// Public Constructor for WindowsServiceInstaller.
        /// - Put all of your Initialization code here.
        ///
        public WindowsServiceInstaller()
        {
            ServiceProcessInstaller serviceProcessInstaller =
                               new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();

            //# Service Account Information
            serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
            serviceProcessInstaller.Username = null;
            serviceProcessInstaller.Password = null;

            //# Service Information
            serviceInstaller.DisplayName = "My New C# Windows Service";
            serviceInstaller.StartType = ServiceStartMode.Automatic;

            //# This must be identical to the WindowsService.ServiceBase name
            //# set in the constructor of WindowsService.cs
            serviceInstaller.ServiceName = "My Windows Service";

            this.Installers.Add(serviceProcessInstaller);
            this.Installers.Add(serviceInstaller);
        }
    }
}

3. Use Framework 2.0 instalutil.exe to create Installation:

Install.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /i WindowsService.exe
echo ---------------------------------------------------
echo Done.

Uninstall.bat

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /u WindowsService.exe
echo ---------------------------------------------------
echo Done.
VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Written by Avivo

October 21st, 2009 at 2:19 pm

Hosting ASP.NET MVC on IIS7 and “Could Not Load Type” error

one comment

  1. If you want to host ASP.NET MVC on IIS7 don’t forget to copy also MVC DLLs to bin directory when publishing.
    • If you have ASP.NET 3.5 SP1 installed on server copy only System.Web.Mvc
    • If you don’t have ASP.NET 3.5 SP1 installed on server copy
      System.Web.Mvc
      System.Web.Routing
      System.Web.Abstractions
  2. You need to copy also web.config that is under directory where your Views are – it is not included in your project by default so it will not be published. Copy this manually or include this web.config in your project and then Publish.
VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Written by Avivo

September 14th, 2009 at 4:15 pm

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 0

3 comments

If you get an error:

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 0

just add this code in HEAD section of your page/masterpage:

<script language="javascript" type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
function endRequest(sender, args)
{
	// Check to see if there's an error on this request.
	if (args.get_error() != undefined)
	{
	  //$get('Error').style.visibility = "visible";
	  // Let the framework know that the error is handled,
	  // so it doesn't throw the JavaScript alert.
	  args.set_errorHandled(true);
	}
}
</script>
VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Written by Avivo

June 16th, 2009 at 7:48 pm

ArrayOfXElement not recognized by Silverlight 2.0

one comment

Last weekend I was working on a Silverlight 2.0 game that loads and saves scores to an external database. Database is accessed via web service which has two methods: GetScore and SaveScore. GetScore returns DataSet and SaveScore returns nothing.

Web service proxy is generated as usual – right click on the project node in VS2008, “Add Service Reference…”, enter url, proxy class is generated and the methods are accessible in the code. But when I tried to build the project an error occured: “The type or namespace name ‘ArrayOfXElement’ does not exist in the namespace” which means that ArrayOfXElement is unrecognized class for the compiler. Searching on the Google reveals that this happens when service method returns DataSet, so ArrayOfXElement is actually DataSet and DataSets are not supported in Silverlight 2.0.

A quick solution is to replace all ‘ArrayOfXElement’ words with ‘object’. Project would build that way but the data would have no meaning.

Another solution is to change the web service return type but this is not always editable by the client side developer.

Finally, workaround idea is to get DataSet schema and convert it to a class. How to do this?
1. Create a dummy WPF project.
2. Add web service reference to the WPF project. Return type is DataSet instead of ArrayOfXElement.
3. Write some code to call a web service methods that returns DataSet then extract xsd schema and save it to the disk. Build and run. Sample:
GameServiceClient client = new GameServiceClient();
DataSet ds = client.GetScore(1, “abc”, “”, DateTime.Now);
ds.WriteXmlSchema(@”C:\Temp\Score.xsd”);
4. So you have the xsd file. Now find ‘xsd.exe’ which is located in the same directory as ‘svcutil.exe’ (e.g. C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin). Run it with arguments:
xsd C:\Temp\Score.xsd /c
5. xsd tool generates Score.cs file which contains tables as classes and columns as properties. Include the generated file in the Silverlight project.
6. Replace all ‘ArrayOfXElement’ words with ‘NewDataSet’ or other class name, depends on the retrieved DataSet name and generated code.

Note that this is just an idea. So far the sample project builds fine but there is a problem with cross-domain security. Another story.

Url to the web service in this example:
http://www.silverlightclub.com/apps/GameService.asmx

References:
http://silverlight.net/forums/t/60518.aspx
http://blogs.msdn.com/suwatch/archive/2009/01/21/wcf-silverlight-exception-and-serialization.aspx

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Written by Avivo

March 21st, 2009 at 8:26 pm

Project for European parlament

leave a comment

After we created the best scenario for interactive Flash game for competition in knowledge about European Union, we got this interesting project from European parlament.

We developed the game, organized precontest and contest among Slovenian high school grades and best 5 grades got a main prize – free touristic trip to Strasbourgh.

We will publish the results on Monday – 9th February 2009.

Url to our game is: www.evrosola.si

Here are some screenshots of our game:

VN:F [1.9.3_1094]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Written by Avivo

February 7th, 2009 at 3:49 pm