Monday, 9 May 2011

Enable Telnet for Windows 7

Using Telnet on Windows 7

I have just upgraded my PC to Windows 7 and after moving all my .NET projects over was in the process of trying to debug a Web Service I had written to run locally on my PC that was returning an error.

I went to my command prompt and typed in a Telnet command to test my service only to be met with a response that no such program existed.

I am guessing this is for security reasons but to enable Telnet on Windows 7 you need to do the following:

  1. Go to Start.
  2. Choose the Control Panel option.
  3. Choose Programs and Features.
  4. Choose the "Turn Windows features on or off" option by clicking the link on the left side menu.
  5. A little pop-up window will appear and load in a number of features and programs.
  6. Check Telnet Client (and / or ) Telnet Server if you require it.
  7. Press the OK button.
  8. If you are using a Anti Virus program like Kaspersky then you might have to allow it to update the Registry.
After a few seconds of loading Telnet will be loaded and you can then use it from your command prompt in the normal way.


Thursday, 7 April 2011

Moving a C# Windows Service to Windows 7

Problems moving a Windows Service to Windows 7

I have just been given a new PC at work that runs 64bit Windows 7. I have to say so far I quite impressed with the OS and the great thing about Windows 7 is that I can now install IE 9.0 which is blindingly fast and they have finally seen the light and made it standards compliant with support for the DOM 2 event model.

One of the jobs I struggled with today though was making a Windows Service that I had created on my old XP machine in Visual Studio 2010 run on my new box.

The service is basically a BOT that makes SOAP requests behind the scenes to a 3rd party server.

The code was working fine as I had moved it all from a standalone console application that worked but whenever my service harness EXE tried to run I would just get bland _COMPlusExceptionCode errors and nothing of any meaning in the event log.

Debugging a Windows Service was a right pain in the behind and after many uninstalls, installs and lots of shouting I finally got it working.

These steps might not work for everyone but I had to do all of the following to get my service working.

1. Access Rights. Even though I supposedly had administrative rights on my PC I couldn't even install the service with the installUtil [path to exe] command without having to right click on the correct Visual Studio command prompt and choosing "Run as administrator".

However even after doing this I was still having problems starting the service and I was getting the standard

The service did not respond to the start or control request in a timely fashion.

Error message whenever I tried starting the service from the service administrative tool or from my desktop wrapper application.

After some step through debugging I found an "Access Denied" error message was occurring on the ServiceController.Start() method call so I went into the User Account Control Settings and turned the scrolling control right down to "Never Notify". As I wasn't allowed the logon details for the Local System account I was using my own login details for the service as this seemed to be the only way to get round this error message with a reboot.

2. Rebuilding the Service as an x64 solution and not x86. I tried this due to a weird win32 error I was seeing and just put 2 and 2 together and presumed that something was not being run correctly due to it now being on a 64 bit machine.

3. Adding the following code to the config file:

<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>


I obtained this clue from the following site: stackoverflow.com and it seemed to be the final part of the jigsaw that finally got my service and the wrapper application working on my new machine.

I am fully aware that I am not describing all the reasons behind the decisions I made but I am not exactly aware of the whys and hows at this point in time. However if I come up with further answers I will let you know. Plus if anyone else has any useful links or tips about this issue please add them to the comment section.

Broken Apple Touch Icon Images

iPhone Touch Icon LINK tag doesn't work

I recently created an iPhone friendly webpage so that I could access certain stats from one of my websites on my iPhone .

The page was in a subfolder off the main website which was only half related to the stats I wanted to show. Therefore when I created the icon I wanted to use for the Home Screen I used a LINK element to point to the URI of the image I wanted to use so that the main site could show it's own image if it ever needed to.

So I created an icon and referenced in my page like so many examples on the web tell you to:

<link ref="apple-touch-icon" sizes="57x57" href="http://www.somewbesite.com/mysubdomain/apple-touch-icon.png">

However when I tried to add my web page to the Home Screen the icon didn't show and instead iPhone created it's own icon using a screenshot of the page in question.

At first I thought it might just take a little time to kick in but even two months after creation the icon was still the screenshot image and not the icon I had created.

The .PNG file was not corrupt and I tried numerous different locations, images and attribute settings to no avail.

However today I just gave up and placed the image in question into the root of the main site e.g

http://www.somewbesite.com/apple-touch-icon.png

Then low and behold the icon immediately showed up on my Home Screen!

What is the point of the LINK element if you cannot point to locations apart from the root of the site?

Surely Apples own iPhone technology is capable of looking for it's own touch screen image tags?

Am I going crazy or is something amiss?

Tuesday, 29 March 2011

Why I hate Open Source development

Why I hate Open Source

I don't often use my blog to rant about things but I am proper pissed off today and I am going to vent my spleen on the topic of open source.

Why am I pissed off? Well apart from the fact that I have a serious health issue that my GP won't refer me to the relevant clinic due to our primary care trust not willing to pay for the treatment I am getting more and more annoyed with open source coding and the numpties that contact me constantly asking me to do their work for them for free.

Don't get me wrong. From a coders point of view it is great to be able to take code from a wide variety of sources and then use it for free or more often than not tweak it to accommodate the changes you require for your own system. I have no problem with that aspect of it whatsoever. If I didn't want people to use my code I wouldn't have put it online for everyone to download.

However my inbox is a testimony to the fact that there seems to be a lot of people out there who
a) can't code.
and
b) expect you to do all their work for them for free.

I am pretty sure there are people all across the world (cough India) that are basically making a living by using other peoples code and then getting people to fix, upgrade, tweak and build all their work for them for diddly squat. These people really tweak my nips!

If you can't code and have put a stupid $50 bid on Rentacoder.com to develop a whole website by next week and expect me to do all your work for you just because you found a piece of my code that does 90% of what you need and you require the other 10% doing for free then you can take a running jump.

People like you are the reason good coders work 50 hour weeks for tech companies and then spent all their free time trying to come up with something that might make some money if the world of programming actually behaved like any other kind of market place.

From the point of view of actually making real money I cannot think of a worse idea f0r anyone than open source coding. Off the top of my head I cannot think of any other business model that acts in a similar way and leaves the person who produced the goods with bog all and the customer with all their hearts desire at a cost of zilch!

Giving your code away for free in the hope that people either donate you some money, hire your services on unrelated matters or if you have created an application pay for value added services is akin to a builder spending months building a house, giving it away to the owner for free and then hoping that the new occupants either throw a few quid your way out the kindness of their heart or hire you to build them an extension.

What grates me the most however is the non coders who take all the hard work that you didn't have to make public and then expect you to either add in all the features that they require for free and then complain when you tell them that you can't help out due to pesky little problems such as having a full time job or wanting a life.

Personally I thought the whole idea of open source code was that you took the original code and made of it what you could. I only really got into PHP / MySQL within the last year and in that time I have made a number of sites and Wordpress plugins that are all well reviewed, have been downloaded tens of thousands of times yet have earned me less money from donations than any Saturday Mall Chugger would with their mouth glued shut.

The only reason I created my plugins in the first place was due to a lack of features in those that existed already.

I didn't hassle the developers of the plugins by email, twitter and message board asking them to do my work for me or expect them to add in my custom features for free like many people seem to. No I took their good ideas and made my own versions and then put them up online so others could do the same.

I can totally understand the fact that many people are not capable of developing their own code or taking other peoples code and tweaking it but it is really starting to grate me when I receive email after email asking when I am going to do X or Y and in a tone that seems to expect that I have nothing better to do than do all their work for them.

Having emails in broken English that say "I need your plugin to do BLAH and need it done by Monday very urgent please help!" is not the right way to go about getting things done.

I have even had people ask me how to steal my own code so they could leach my bandwidth by hosting it on their own site.

And worst of the whole lot is the two people now who have literally begged me for a feature to be implemented in one of my tools and then when I implemented it they don't even give even the most basic donation.

I mean if I have just spent 5+ hours adding in code that makes your life easier surely that is worth at least a score or a tenner. Even the nice bloke who donated me a solitary pound the other day ranks higher in my estimation than the scrotes who take the piss like that.

And before I get inundated with a load more emails promising me donations as soon as I just do X Y and Z I want to say NO NO NO. I've had it up to here with the whole shabang.

Server bills don't get paid by magic you know so the next version of any plugin releases are all going to be paid only. If people really want these features then they will be willing to pay, either that or learn how to code. It's not like there aren't enough Google 101 guide's on the web to get started with.

Rant over.

Monday, 14 March 2011

SQL Varchar Comparison Ignores Right Hand White Space

MS SQL Server Text Comparisons

I was doing some work in SQL 2008 today that involved some data cleaning from a scraper that was extracting specific data from a webpage and trying to match it in a DB. The data was very dirty and not consistent in the slightest so I was running it through a number of custom parser functions to try and find matches.

Whilst doing this I came across something that I certainly thought was odd and hadn't seen before. I don't know if this is a well know "quirk" in MS SQL or not but a couple of colleagues had never seen it either.

It involved string comparisons where one string had white space to the right hand side of it and the other had no space either side and a string comparison matched.

This only occurs when the white space is to the right hand side of the text and not to the left hand side as the following test shows.

I had never come across this quirk before so I thought I would make a note of it incase others hadn't either. I ran this test on 2008 and 2005 with the same results.

DECLARE @Test1 varchar(100),
@Test2 varchar(100),
@Test3 varchar(100)

SELECT @Test1 = 'Stratford ',
@Test2 = ' Stratford',
@Test3 = ' Stratford '

IF @Test1 = 'Stratford'
PRINT 'Is Stratford'
ELSE
PRINT 'Is not Stratford'

IF @Test2 = 'Stratford'
PRINT 'Is Stratford'
ELSE
PRINT 'Is not Stratford'

IF @Test3 = 'Stratford'
PRINT 'Is Stratford'
ELSE
PRINT 'Is not Stratford'

SELECT '"' + @Test1 + '"', '"' + @Test2 + '"', '"' + @Test3 + '"'




The output I get is the following


Is Stratford
Is not Stratford
Is not Stratford

and then a recordset.

"Stratford    " "   Stratford" "   Stratford   "
Maybe this is a well known behaviour but I wasn't expecting it to behave like this so it threw me off a bit during my cleanup process.


Thursday, 10 March 2011

Problems and solutions whilst creating an ASP.NET Web Service

Problems setting up an ASP.NET Web Service

I was recently following a guide to setting up a .NET web service in Visual Studio 2010 and I ran into a few problems.

The guide I was using was Microsoft's own Knowledge Base article which runs through the setting up of a very simple Maths based web service.

Missing Web Service Project Template

The first problem was trying to find the ASP.NET Web Service project template on the start up page of Visual Studio. Step 2 of the guide says:

On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click ASP.NET Web Service under Templates. Type MathService in the Location text box to change the default name (WebService1) to MathService.
However in Visual Studio 2010 this option was missing. I found out that the reason was due to the default Framework being set to .NET 4.0. The solution is to change the framework to .NET 3.5 and low and behold the Web Service template option was available to select.

I don't know the reasons behind this so don't ask and maybe it's possible to create a web service in .NET 4.0 some other way. If I find out I will update this article.


How to resolve "Could not create type MathServices.service1" error

The second problem I ran into was due to the advice given in step 3 which was to rename the default web service to something else.

Change the name of the default Web service that is created from Service1.asmx to MathService.asmx.
This is fine until you actually build the project and try and access the service on your localhost as it says to do in Step 7.

Browse to the MathService.asmx Web service page to test the Web service. If you set the local computer to host the page, the URL is http://localhost/MathService/MathService.asmx.
When the page loads I was met with an ASP.NET error along the following lines:

Server Error in '/' Application.

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Could not create type 'MathService.Service1'.

Source Error:


Line 1: <%@ WebService Language="C#" CodeBehind="MathService.asmx.cs" Class="MathService.Service1" >

Source File: /MathService.asmx Line: 1



The reason behind this was that even though I had changed the name of the service I had not changed the code in the .ASMX file to point to the new codebehind. Therefore you need to right click on the .ASMX file in the project explorer and choose the "View Markup" option. Once that is open just change the code to point to the new class like so.

<@ WebService Language="C#" CodeBehind="MathService.asmx.cs" Class="MathService.MathService" %>


Incorrect location for the web service

The knowledge base guide I was following said that the web service would be located on my local machine at the following location:
http://localhost/MathService/MathService.asmx

However when trying this gave me a 404 error. So instead after building the application I clicked the play button which took me to the following location:
http://localhost:1271/MathService.asmx

As you can see this differs due to the port number and missing sub directory. I am pretty sure I haven't missed any steps out along the way and the namespace for my Web Service was setup correctly so I am not sure why this differs from the example.


Creating a Consumer application for the web service

The previous step was important because the next part of the guide was on creating a consumer console application to make use of the web service.

In Visual Studio you select the "Add Service Reference" option from Project menu and then click the "Advanced" button.

On the next page choose the "Add Web Reference" button at the bottom and then on the next page you need to enter the location of your web service in the "URL" box at the top and then hit the little green arrow to the side of it. This will search for the reference at the specified location.

Make sure you know where the web service is located before trying this as the location of my own local web service was not where the knowledge base article said it would be:
http://localhost/MathService/MathService.asmx
instead it was located at
http://localhost:1271/MathService.asmx
Once the web service is found you can add it in the normal manner.

After that change, a re-save and a re-build everything worked fine.

Hopefully this might help others out there who are following the same guide if they run into the same problems.


Sunday, 20 February 2011

Remotely Reboot Windows PC

How to remotely restart a Windows PC

I have been experiencing lots of problems with my work PC latley.

Usually these problems happen at the weekend and they turn my PC which usually performs like a bag of shit into a much larger and more non responsive bag of shit that makes any kind of work on it virtually impossible to carry out.

This means that not only does Sods Law come into effect which make me lose lots of money from my Betfair Bot not being able to place bets (why can it never fail during periods that I would have placed bad bets!). But it also means that if I manage to terminal service into the machine that it cannot connect to any other server on the network, access the internet, receive updates or send and recieve email.

I haven'nt got to the bottom of the problem yet but one thing I managed to find out today which might be useful for others to know is how to reboot a remote machine remotely from the command line.

If you can connect to the VPN that the remote PC is on and can PING the machine then you can try restarting your problematic PC. As we all know from any first line technical support a reboot is always the first port of call in any technical emergency and for Windows machines it seems to solve around 99% of all problems.

So this is the command I used from the command prompt.

shutdown -m \\pieceofshitPC -r -f

The -r flag tells it to restart rather than just shut down and the -f flag tells it to forcably shut down any running applications.

The other option is to just run

shutdown -i

from the RUN prompt and the Shutdown.exe application will open which will give you a graphical interface to manage the shut down.

This offers features such as being able to log a reason for the shutdown and specifying a time period for the system to wait before trying the reboot or restart command.

This GUI interface is just a wrapper to save those who are loath to open command prompts but if you want to use the command prompt the following flags are possible.

-i : Display GUI interface, must be the first option
-l : Log off (cannot be used with -m option)
-s : Shutdown the computer
-r : Shutdown and restart the computer
-a : Abort a system shutdown
-m \\computername : Remote computer to shutdown/restart/abort
-t xx : Set timeout for shutdown to xx seconds
-c “comment” : Shutdown comment (maximum of 127 characters)
-f : Forces running applications to close without warning
-d [u][p]:xx:yy : The reason code for the shutdown



Happy rebooting!