<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Einar Egilsson &#187; Tips &amp; Tricks</title>
	<atom:link href="http://einaregilsson.com/category/tips-and-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://einaregilsson.com</link>
	<description>A site for my programming pet projects</description>
	<lastBuildDate>Fri, 10 Feb 2012 16:28:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Stop Build on first error in Visual Studio 2010</title>
		<link>http://einaregilsson.com/stop-build-on-first-error-in-visual-studio-2010/</link>
		<comments>http://einaregilsson.com/stop-build-on-first-error-in-visual-studio-2010/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 08:08:48 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=426</guid>
		<description><![CDATA[tl;dr: StopOnFirstBuildError is Visual Studio 2010 extension that cancels the rest of a solution build if a single project fails to compile, thus saving you time. Download it in the Visual Studio Gallery. Follow me on twitter @einaregilsson to get notified of updates and other cool extensions and stuff. At work I often work on a [...]]]></description>
			<content:encoded><![CDATA[<p><strong>tl;dr:</strong> StopOnFirstBuildError is Visual Studio 2010 extension that cancels the rest of a solution build if a single project fails to compile, thus saving you time. <a href="http://visualstudiogallery.msdn.microsoft.com/en-us/91aaa139-5d3c-43a7-b39f-369196a84fa5">Download it in the Visual Studio Gallery</a>.</p>
<p><em>Follow me on twitter <a href="http://twitter.com/einaregilsson">@einaregilsson</a> to get notified of updates and other cool extensions and stuff.</em></p>
<p>At work I often work on a solution that has around 25 projects. When one of the projects fails to build, Visual Studio insists on trying to build the rest of the projects, even though at that point I don&#8217;t want it to, since I&#8217;m never going to run the program when some of the projects have failed. Often the other projects depend on the project that failed, and the error list gets filled with errors from those projects that just obscure the root cause of the problem. So, I figured I could probably do something about it.</p>
<p><span id="more-426"></span>I googled around for a solution and found a <a href="http://msdn.microsoft.com/en-us/library/microsoft.build.tasks.msbuild.stoponfirstfailure.aspx">StopOnFirstFailure</a> property in MSBuild that sounded promising, but I couldn&#8217;t find any good way to hook into the VS-&gt;MSBuild relationship. I&#8217;m sure there is some way to do this, so if anyone knows, please let me know. The next thing I found was a <a href="http://stevedunns.blogspot.com/2006/10/visual-studio-build-tip.html">tip from Steve Dunn</a>. It&#8217;s a nice little macro that listens for an event that is fired after each project is built, and calls the Cancel Build command if a project has failed to build. You just open the Macro IDE, open the EnvironmentEvents.vb file and paste the following code in at the bottom:</p>
<pre class="brush: vb; title: ; notranslate">
Private Sub BuildEvents_OnBuildProjConfigDone(
                   _ ByVal Project As String,
                   _ ByVal ProjectConfig As String,
                   _ ByVal Platform As String,
                   _ ByVal SolutionConfig As String,
                   _ ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone
    If Success = False Then 'The build failed...cancel any further builds.
        DTE.ExecuteCommand(&quot;Build.Cancel&quot;)
    End If
End Sub
</pre>
<p>This does 95% of what I wanted to do. But still, I&#8217;m not a big fan of Visual Studio Macros, I prefer extensions where possible so I can easily see in one place what things I have installed instead of digging through the Macro projects. I also wanted to activate the error window after cancelling the build and be able to turn this on and off easily. So, I made an extension out of it. The main thing in it is still the macro code from Steve Dunn above, but in addition the extension:</p>
<ul>
<li>Activates the error list after cancelling the build.</li>
<li>Prints a message to the Build output window, saying why the build was cancelled.</li>
<li>Adds a menu item to the Build menu, &#8220;Stop Build on first error&#8221;, which you can use to turn the functionality on and off easily.</li>
<li>Is context aware, the menu item and functionality are only available in multi-project solutions, since it would be pretty useless in a single project solution.
</ul>
<p>And that&#8217;s it. You can <a href="http://visualstudiogallery.msdn.microsoft.com/en-us/91aaa139-5d3c-43a7-b39f-369196a84fa5">download the extension</a> in the Visual Studio Gallery, get the GPL&#8217;d source code at <a href="https://github.com/einaregilsson/StopOnFirstBuildError">https://github.com/einaregilsson/StopOnFirstBuildError</a> and follow me on Twitter <a href="http://twitter.com/einaregilsson">@einaregilsson</a> to get notified of updates and other new extensions. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/stop-build-on-first-error-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Write to a file in the Google App Engine Development server</title>
		<link>http://einaregilsson.com/write-to-a-file-in-the-googleapp-engine-development-server/</link>
		<comments>http://einaregilsson.com/write-to-a-file-in-the-googleapp-engine-development-server/#comments</comments>
		<pubDate>Thu, 16 Dec 2010 14:21:23 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=421</guid>
		<description><![CDATA[The App Engine development server tries to mimic the real environment by making it forbidden to write to the filesystem. However, for certain things you might want to do that anyway, for example to log to a file. In that case you can use this code snippet to enable writing to the filesystem:]]></description>
			<content:encoded><![CDATA[<p>The App Engine development server tries to mimic the real environment by making it forbidden to write to the filesystem. However, for certain things you might want to do that anyway, for example to log to a file. In that case you can use this code snippet to enable writing to the filesystem:</p>
<pre class="brush: python; title: ; notranslate">
if os.environ.get('SERVER_SOFTWARE','').startswith('Dev'):
    from google.appengine.tools.dev_appserver import FakeFile
    FakeFile.ALLOWED_MODES = frozenset(['a','r', 'w', 'rb', 'U', 'rU'])
</pre>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/write-to-a-file-in-the-googleapp-engine-development-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit testing model classes in Google App Engine</title>
		<link>http://einaregilsson.com/unit-testing-model-classes-in-google-app-engine/</link>
		<comments>http://einaregilsson.com/unit-testing-model-classes-in-google-app-engine/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 18:25:56 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=416</guid>
		<description><![CDATA[I&#8217;m currently writing my first Google App Engine app. I&#8217;ve created a few model classes which inherit from db.Model and I wanted to unit test them. That works fine until you try to save them to the datastore or retrieve them. Then you might get this error: BadArgumentError: app must not be empty. The problem [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently writing my first <a href="http://appengine.google.com">Google App Engine</a> app. I&#8217;ve created a few model classes which inherit from db.Model and I wanted to unit test them. That works fine until you try to save them to the datastore or retrieve them. Then you might get this error:</p>
<div style="background-color:black; color:white; padding:2px;">
BadArgumentError: app must not be empty.
</div>
<p>The problem here is that you need an environment variable with your app id, which you can set with the following code:<br />
<span id="more-416"></span></p>
<pre class="brush: python; title: ; notranslate">
os.environ['APPLICATION_ID'] = 'myapp'
</pre>
<p>After you do that you&#8217;ll get this error:</p>
<div style="background-color:black; color:white; padding:2px;">
AssertionError: No api proxy found for service &#8220;datastore_v3&#8243;
</div>
<p>To fix that one you need the following lines in your test setup:</p>
<pre class="brush: python; title: ; notranslate">
datastore_file = '/dev/null'
from google.appengine.api import apiproxy_stub_map,datastore_file_stub
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
stub = datastore_file_stub.DatastoreFileStub('myapp', datastore_file, '/')
apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
</pre>
<p>This temp datastore is persisted just while you run your tests. But you can also give the datastore_file variable a real filename and then it will save the datastore between tests, or you could create different datastore files for different scenarios.</p>
<p>Finally, to put it all together, here is the setUp method for my TestCase classes:</p>
<pre class="brush: python; title: ; notranslate">
class SomeTestCase(unittest.TestCase):
	def setUp(self):
		app_id = 'myapp'
		os.environ['APPLICATION_ID'] = app_id
		datastore_file = '/dev/null'
		from google.appengine.api import apiproxy_stub_map,datastore_file_stub
		apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
		stub = datastore_file_stub.DatastoreFileStub(app_id, datastore_file, '/')
		apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/unit-testing-model-classes-in-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easy way to have custom icons in Visual Studio AddIn</title>
		<link>http://einaregilsson.com/easy-way-to-have-custom-icons-in-visual-studio-addin/</link>
		<comments>http://einaregilsson.com/easy-way-to-have-custom-icons-in-visual-studio-addin/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 09:39:34 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=305</guid>
		<description><![CDATA[I&#8217;ve recently been developing a Visual Studio AddIn and I wanted to use custom icons for a command I had. Looking for a solution I found the offical MSDN article on the subject, that might possibly be the most misleading and useless article ever. Add the resource file in Visual Studio, then exclude from project, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been developing a Visual Studio AddIn and I wanted to use custom icons for a command I had. Looking for a solution I found the <a href="http://msdn.microsoft.com/en-us/library/ms228771.aspx">offical MSDN article on the subject</a>, that might possibly be the most misleading and useless article ever. Add the resource file in Visual Studio, then exclude from project, rename your images to numbers, edit with Notepad and then build satellite assemblies on the commandline? Really? That is a horrible way to do it and not at all necessary. I&#8217;ve found a very simple and easy way to add these icons without all that hassle.</p>
<p><span id="more-305"></span></p>
<p>There are a couple of other articles out there that approach the problem in a different way. Roy Osherove has an <a href="http://weblogs.asp.net/rosherove/archive/2008/08/02/howto-set-custom-visual-studio-addin-menu-icons-without-a-satellite-dll.aspx">article</a> where he shows how you can add icons without having satellite assemblies for them, but that has &gt; 350 lines of code for loading the images yourself including P/Invokes and all sorts of stuff, and I didn&#8217;t really need such a heavyweight solution. To be fair, his solution does give you the ability to use .ico files as well, and offers transparency, but transparency can be achieved in an easier way.</p>
<p>I also found <a href="http://www.mztools.com/articles/2005/MZ2005007.aspx">another article</a> that shows how you can get transparency by using a special color that, as far as I know, is not documented anywhere officially. It also shows you how you can build a satellite assembly from Visual Studio without resorting to Notepad and the command line tools, but this approach uses a special project for the satellite assembly, which is completely unneccessary.</p>
<p>So, finally, my approach, which is really a refinement of the last article I mentioned. If your add-in is meant for only VS2008 then you can get away with having no satellite assembly, VS2008 falls back to looking in your main assembly for the resources if it doesn&#8217;t find a satellite assembly. If you want to support VS2005 you must have a satellite assembly for it to work.</p>
<p>So, assume we have a project named MyProject which will compile to an assembly called MyProject.dll. Here&#8217;s what you do:</p>
<ol>
<li>Add a new &#8216;resources&#8217; item to the project and give it the name &#8220;MyProject.en.resx&#8221;.</li>
<li>Go the properties for the item and set the &#8220;Custom Tool&#8221; field to nothing. This will remove the auto-generated code for the .resx file</li>
<li>Double click on the .resx file, this opens up the resource editor. There you can add an existing image (Add Resource-&gt;Add existing file). The image should be a 16&#215;16 pixel bitmap image. It must be in True Color format. True Color is the same as a 24-bit bitmap.</li>
<li>If you want transparency in your image then use the special color: Red:0 , Green:254, Blue:0 . Note that it really is 254, NOT 255.</li>
<li>In the resource editor, give your image a name that is a number, e.g. 1. Note that you did not have to change the filename of your image file, just its name in the resource file.</li>
<li>Now add your command in your add-in file like so:
<pre class="brush: csharp; title: ; notranslate">
commands.AddNamedCommand2(_addInInstance, &quot;MyProject&quot;,
      &quot;MyProject&quot;, &quot;Executes the command for MyProject&quot;,
      false,
      1,
      ref contextGUIDS,
      (int)vsCommandStatus.vsCommandStatusSupported +
      (int)vsCommandStatus.vsCommandStatusEnabled,
      (int)vsCommandStyle.vsCommandStylePictAndText,
      vsCommandControlType.vsCommandControlTypeButton);
</pre>
<p>There you must send in <strong>false</strong> and then the number you gave the icon in your .resx file, e.g. 1.</li>
</ol>
<p>Now when you compile your solution, a satellite assembly with the name MyProject.resources.dll will automatically be created and put in a &#8220;en&#8221; subfolder of your output folder. If you only want to support VS2008 and don&#8217;t want satellite assemblies then you can name the .resx file just MyProject.resx instead, then no satellite assemblies will be created and the resources will be embedded in the main assembly.</p>
<p>And that&#8217;s it. Add more resources in Visual Studio, have things compile automatically and don&#8217;t touch command line tools, notepad, hundreds of lines of custom code or anything like that. I *think* it is enough to just have the &#8220;en&#8221; resources dll even if you use another locale, I&#8217;m pretty sure Visual Studio falls back to that if it doesn&#8217;t find a satellite assembly in your current culture. At least I have my Windows set up for Danish, but my &#8220;en&#8221; satellite assembly works just fine for me. Although, there doesn&#8217;t exist a Danish version of Visual Studio, maybe this works differently for localized Visual Studio, e.g. the German version. If someone has one of those and can test I&#8217;d love to know whether it falls back to &#8220;en&#8221; if nothing else is available. If it doesn&#8217;t then just create a few more MyProject.de.resx, MyProject.es.resx etc. to get more satellite assemblies. But like I said before, VS2008 and later doesn&#8217;t need this, so for that case just have the one MyProject.resx file.</p>
<p>I&#8217;ve made the small MyProject example and it can be <a href="/download/MyProjectAddIn.zip">downloaded here</a>. Let me know if this works (or doesn&#8217;t) for you. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/easy-way-to-have-custom-icons-in-visual-studio-addin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JScript REPL</title>
		<link>http://einaregilsson.com/jscript-repl/</link>
		<comments>http://einaregilsson.com/jscript-repl/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 20:09:02 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=197</guid>
		<description><![CDATA[Lately I have been doing some COM automation stuff on Windows. I&#8217;ve been using JScript (Microsoft&#8217;s JavaScript implementation) since that&#8217;s available on all Windows machines, and the other option, VBScript, is horrible. Normally I would use Python and the win32com package, but I needed to make some scripts that could work on any box without [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been doing some COM automation stuff on Windows. I&#8217;ve been using JScript (Microsoft&#8217;s JavaScript implementation) since that&#8217;s available on all Windows machines, and the other option, VBScript, is horrible. Normally I would use Python and the win32com package, but I needed to make some scripts that could work on any box without installing Python first. JScript is a pretty nice language, but it doesn&#8217;t come with a <a href="http://en.wikipedia.org/wiki/Read-eval-print_loop">REPL</a> built in, which is very handy when you&#8217;re doing experimental stuff (REPL = Read-Execute-Print-Loop). Now, writing your own REPL in a dynamic language with an <strong>eval</strong> statement is pretty easy, so I did just that. It took about 30 lines, of which about 10 are just about printing evaluated expressions nicely. <span id="more-197"></span></p>
<p>Here&#8217;s how it works. If you enter a single line that ends with ; it is evaluated immediately. If it doesn&#8217;t start with if,while,var,try,do,with,function,switch,for or print it is assumed to be an expression and its value is printed to the screen. If a new line doesn&#8217;t end with ; is is assumed to be the beginning of a larger code block. In that case the script will keep reading in code without evaluating until either you end a line with ;; or you enter a line which consists of only one ;. The script also includes a convenience function called print() to print expressions. The script just uses a single variable, _$, so it won&#8217;t conflict with variables you define when using it. Writing a single line with just the word &#8216;exit&#8217; will stop the script. Below you can see an example of the usage of the REPL. This sample is actually a online version that I did for fun, you can write in any command in it and try it out, it works exactly the same as the real script, although the online version took quite a bit more code. It even has command history, activated with the up and down arrow keys. (There are some issues in Internet Explorer, tab key doesn&#8217;t work and strings with html entities my display incorrectly, because of <a href="http://stud3.tuwien.ac.at/~e0226430/innerHtmlQuirk.html">weird behaviour with &lt;pre&gt; tags and the .innerHTML property).</a></p>
<p><a href="http://einaregilsson.com/download/jsrepl.js">Download script</a> | <a href="http://einaregilsson.com/download/jsrepl.js.html">View script syntax highlighted</a></p>
<p><iframe src="http://einaregilsson.com/repl.html" style="width:620px; height:400px;overflow-x:hidden; border-style:none; border:solid 0px black;" frameborder="0"></iframe></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/jscript-repl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extensionless urls in ASP.NET MVC on IIS 6</title>
		<link>http://einaregilsson.com/extensionless-urls-in-asp-net-mvc-on-iis-6/</link>
		<comments>http://einaregilsson.com/extensionless-urls-in-asp-net-mvc-on-iis-6/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 18:45:52 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/?p=129</guid>
		<description><![CDATA[I recently started playing around with ASP.NET MVC to build a small website. I&#8217;m pretty impressed, I like working with MVC a lot better than the web forms model. One thing that ASP.NET MVC offers is to have &#8220;pretty urls&#8221; similar to frameworks like Ruby on Rails or Django, that is, instead of urls that [...]]]></description>
			<content:encoded><![CDATA[<p>I recently started playing around with <a href="http://asp.net/mvc">ASP.NET MVC</a> to build a small website. I&#8217;m pretty impressed, I like working with MVC a lot better than the web forms model. One thing that ASP.NET MVC offers is to have &#8220;pretty urls&#8221; similar to frameworks like <a href="http://rubyonrails.org">Ruby on Rails</a> or <a href="http://djangoproject.com">Django</a>, that is, instead of urls that look like <strong>http://example.com/index.aspx?car=Ford&#038;year=1990</strong> you get urls like <strong>http://example.com/cars/Ford/1990</strong>. This works flawlessly in Visual Studio using the development webserver but there can be some complications when deploying to IIS. The new IIS 7 has support for this built in when using integrated mode, however my webhost is still on IIS 6 and there are problems there. Essentially there are two ways that can be used with IIS 6. The first way is to map all requests to the ASP.NET engine, even ones for images, css etc. That works but has some performance implications. The other way is to use a file extension in all urls, so our example url might have to be something like <strong>http://example.com/cars.aspx/Ford/1990/</strong>. That&#8217;s not horrible but not the way I want it either. So, I came up with another way. <span id="more-129"></span></p>
<p>I wrote a blog post a while ago about how to get <a href="/2007/07/30/pretty-wordpress-permalinks-on-iis/">pretty wordpress permalinks on IIS</a> using IIS&#8217;s 404 page, so I figured I might be able to hack something similar together for this. Just a bit about how the IIS 404 page works: when IIS doesn&#8217;t find an item for a request it will execute the 404 page with the missing page&#8217;s url as a querystring. So, if we make a request for <strong>http://example.com/doesntexist</strong> and the 404 page is <strong>/notfound.aspx</strong> then the server executes <strong>http://example.com/notfound.aspx?404;http://example.com/doesntexist</strong>. The important thing here is that IIS executes this in response to the original request, it does not redirect to this page. That means that even as we&#8217;re executing the 404 page the user still sees <strong>http://example.com/doesntexist</strong> in his browser url bar. (Note that when you set the 404 page in IIS it allows you to specify either <em>file</em> or <em>url</em>, you must choose <em>url</em>. Shared hosting webhosts typically offer just <em>url</em>, at least mine only does. Also, the 404 page you can specify in web.config is a completely different thing, that is redirected to and so is useless for this.)</p>
<p>When you create a new MVC project in Visual Studio you get a file called Default.aspx in your root directory. (Note: this whole article is about how things are in the MVC beta, they might change in other versions). In the codebehind file, Default.aspx.cs there is a page load method that does some rewriting and then executes an Mvc http handler. I set this file to be my 404 page in IIS (you might use a different file if you want it somewhere else) and then changed its Page_Load method to be like this:</p>
<pre class="brush: csharp; title: ; notranslate">
public void Page_Load(object sender, System.EventArgs e) {
    string querystring = Request.ServerVariables[&amp;amp;quot;QUERY_STRING&amp;amp;quot;];
    if (querystring.StartsWith(&amp;amp;quot;404;&amp;amp;quot;)) {
        int startPos = querystring.IndexOf('/', &amp;amp;quot;404;https://1&amp;amp;quot;.Length);
        HttpContext.Current.RewritePath(querystring.Substring(startPos));
    } else {
        HttpContext.Current.RewritePath(Request.ApplicationPath);
    }
    IHttpHandler httpHandler = new MvcHttpHandler();
    httpHandler.ProcessRequest(HttpContext.Current);
}
</pre>
<p>If we step through this a bit, first it checks if the querystring starts with 404; in that case the page is being executed as a 404 page, otherwise we do what this file previously did. Then we parse out the path from our querystring, rewrite it into our current http context and then execute the MvcHttpHandler on it. So, all requests with paths like <strong>/cars/Ford/1990</strong> will end up here where we parse the relevant bit out of them and execute them with MVC.</p>
<p>Now we have this working, but at the expense of ruining the real 404 system. Any 404 request will be routed through there, no matter what it&#8217;s for, so how can we handle real 404&#8242;s? What I did was to add a new Action to my HomeController called PageNotFound which simply looks like this:</p>
<pre class="brush: csharp; title: ; notranslate">
public ActionResult PageNotFound(string path) {
    ViewData[&amp;amp;quot;path&amp;amp;quot;] = &amp;amp;quot;/&amp;amp;quot; + path;
    Response.StatusCode = 404;
    return View();
}
</pre>
<p>and then I added a catch-all rout map at the very bottom of my route map, like so:</p>
<pre class="brush: csharp; title: ; notranslate">
routes.MapRoute(
    &amp;amp;quot;PageNotFound&amp;amp;quot;,                                      // Route name&amp;amp;lt;/span&amp;amp;gt;
    &amp;amp;quot;{*path}&amp;amp;quot;,                                           // URL with parameters&amp;amp;lt;/span&amp;amp;gt;
    new { controller = &amp;amp;quot;Home&amp;amp;quot;, action = &amp;amp;quot;PageNotFound&amp;amp;quot;}  // Parameter defaults&amp;amp;lt;/span&amp;amp;gt;
);
</pre>
<p>The <strong>{*path}</strong> means that this will match any possible path, having it at the bottom means that it will only ever execute if no other route matches => this is executed for pages that don&#8217;t exist. Then it passes the non-existent path to the controller, which sets the status code to 404 and adds the path to the ViewData, so the view can print a nice error message. One thing I had to change also was to remove the <em>Default</em> route from the route map, the one that had the url defined as <strong>{controller}/{action}/{id}</strong>. I removed it because it matched any url with three components and in the case when the components didn&#8217;t match any controller or action I got some ugly error message about missing controllers when what I really wanted was a 404 page. Instead I just explicitly state my routes now and usually don&#8217;t have the <strong>{controller}</strong> or <strong>{action}</strong> in the urls. With a bigger site this might be annoying and there are probably cleaner ways to handle it so any suggestions are welcome in the comments.</p>
<p>So that&#8217;s it. It&#8217;s all a big hack, but it does work without reducing performance by mapping everything to asp.net. I&#8217;m also not sure if the raw logs will show all those requests as 404&#8242;s or 200&#8242;s since my webhost doesn&#8217;t let me see raw lows for my subdomains, but I think they&#8217;ll be marked as 200. This has also only been tested when the site is in the root folder, I don&#8217;t know if it will work unchanged if the application is in a subfolder on the server. </p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/extensionless-urls-in-asp-net-mvc-on-iis-6/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Developing ASP.NET 1.1 in Visual Studio 2008</title>
		<link>http://einaregilsson.com/developing-aspnet-1-1-in-visual-studio-2008/</link>
		<comments>http://einaregilsson.com/developing-aspnet-1-1-in-visual-studio-2008/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 10:42:01 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/2008/01/15/developing-aspnet-11-in-visual-studio-2008/</guid>
		<description><![CDATA[I have an old ASP.NET 1.1 application that I have to maintain and which for reasons beyond my control can&#8217;t be updated to a later .net version. I hadn&#8217;t touched it in a few months but recently I had to make some small changes and realized I didn&#8217;t even have Visual Studio 2003 anymore. I [...]]]></description>
			<content:encoded><![CDATA[<p>I have an old ASP.NET 1.1 application that I have to maintain and which for reasons beyond my control can&#8217;t be updated to a later .net version. I hadn&#8217;t touched it in a few months but recently I had to make some small changes and realized I didn&#8217;t even have Visual Studio 2003 anymore. I got a new computer a few months ago and I have Visual Studio 2008 and IIS 7 on it but no VS 2003. I didn&#8217;t really want to install it, it&#8217;s pretty old at this point and not very well supported in Vista, and like most programmers I like to play with the shiny new toys, not the old obsolete ones. So I decided to try to maintain this application in Visual Studio 2008. Now, VS 2008 can target different versions of the .NET framework, but only 2.0, 3.0 and 3.5 so I was out of luck. But, thanks to a nice article I found by Jomo Fisher on compiling .NET 1.1 in VS2005 and some extra hacking I got it working pretty well. My setup was IIS 7 on Windows Vista, IIS 6 on Windows XP is pretty much the same although some of the options I point to may be located in different places. So, here&#8217;s what you need to do to develop ASP.NET 1.1 in Visual Studio 2008:<br />
<span id="more-57"></span></p>
<h4>IIS Setup</h4>
<p>Install the .NET 1.1 framework on your computer if it&#8217;s not already installed. Then start a command prompt as an administrator and then type  <strong>%WINDIR%Microsoft.NETFrameworkv1.1.4322aspnet_regiis -i</strong>. This will setup asp.net 1.1 to work with IIS 7. Next you should open up the IIS manager. Select &#8216;Application pools&#8217;, and under &#8216;Actions&#8217; on the right side of the screen select &#8216;Add application pool&#8217;. Give the new application pool a name, for instance &#8216;Asp.net 1.1&#8242;, set the framework version to &#8216;v1.1.4322&#8242; and pipeline mode to &#8216;classic&#8217;. Save the application pool.</p>
<h4>Compilation</h4>
<p>Jomo Fisher figured out a very nice way to use the C# 1.1 compiler in Visual Studio 2005 by manipulating some MSBuild target files. <a href="http://blogs.msdn.com/jomo_fisher/archive/2005/04/22/410903.aspx">Follow his instructions</a> for all the  projects in your solution. When that works, goto the project properties, goto the &#8216;Build&#8217; tab and set &#8216;Output path&#8217; to &#8216;bin&#8217;. Jomo&#8217;s method sets the output  path to &#8216;bin.Net1.1Debug&#8217; by default which won&#8217;t work for a web application, it needs the assemblies to be directly in the bin folder. Now you should be able to compile your web. If you try any .NET 2.0 specific stuff, like <span style="font-family:courier-new, monospace;">public class F&lt;T&gt;{}</span> you should get a compilation error.</p>
<h4>Debugging</h4>
<p>If you want to debug your web using the .NET 1.1 framework you can&#8217;t use the Visual Studio built-in webserver. That webserver uses the .NET 2.0 framework and even though you&#8217;ve compiled your assemblies using the C# 1.1 compiler, there&#8217;s still the aspx pages themselves that need to be compiled and we want that to happen with the C# 1.1 compiler too. Otherwise you might run into a situation where you think your web is completely working in asp.net 1.1, then you deploy to an actual 1.1 server and it&#8217;ll crash if you accidentally used some 2.0 specific stuff in your aspx files. So what we do is this: Goto project properties and the &#8216;Web&#8217; tab. Under the &#8216;Servers&#8217; section change it so that you&#8217;ve selected &#8216;Use IIS Web server&#8217;. This will show you your project url which you can change. Then press the &#8216;Create virtual directory&#8217; button. You MUST have started Visual Studio as administrator for this to work. Once you&#8217;ve done this you should go back to the IIS manager where you can now see your new virtual directory. Right click on it, press &#8216;Advanced Settings&#8217; and there, under &#8216;Behavior&#8217;, you can select which application pool to use. Change it so that it uses your brand new 1.1 application pool. </p>
<h4>Designer support</h4>
<p>I&#8217;ve got good news and bad news. The good news is that you will be able to view your pages in the designer and lay out things and it will generate the aspx file correctly for you. The bad news is that it won&#8217;t generate the codebehind member variables for you. But theres not much to it, all it means is that when you&#8217;ve added a button and given it an id of &#8216;btnDoStuff&#8217; then you should in your .aspx.cs file create a variable <span style="font-family:courier-new, monospace;">protected Button btnDoStuff;</span>. It&#8217;s a little annoying but you get used to it. It shouldn&#8217;t be too hard to write your own macro to generate members from tags, maybe I&#8217;ll do it someday (or not). Whenever you add a new page you&#8217;ll get compilation errors because of the partial classes. What I usually do is delete the .designer.cs file and remove the &#8216;partial&#8217; keyword from the main class. There&#8217;s also a using statement that references <span style="font-family:courier-new, monospace;">System.Web.UI.WebControls.WebParts</span> that you&#8217;ll have to delete.</p>
<p>I tried one approach which was to have a pre-build event that changed the name of the class in the .designer.cs file and removed the &#8216;partial&#8217; keyword. Then I made my normal class inherit from that class. That worked sometimes but sometimes members would just stop being generated so in the end I figured it wasn&#8217;t worth it and just started deleting the .designer.cs files instead. I still think creating your own macro to generate such a base class could be a good idea though.</p>
<h4>Other issues</h4>
<p>There are some other issues you might run into. Web.Config is one of them. You must remove stuff that wasn&#8217;t there in 1.1, like the &lt;connectionStrings /&gt; section.  Other than that I don&#8217;t know of any major changes, but then again my Web.Config is really simple. And then you have to make sure yourself that you don&#8217;t try to use any of the stuff that wasn&#8217;t in 1.1. That means no masterpages, generics, partial classes or any of the new webcontrols that came with 2.0. Another thing you should be aware of is that you always need to run Visual Studio as administrator for the debugging to work.</p>
<p>And that&#8217;s it. This worked for me, I don&#8217;t know if it&#8217;ll work for you. There&#8217;s some hassle involved, but for me, having some of the cool VS2008 stuff available (hello javascript intellisense) more than makes up for it.</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/developing-aspnet-1-1-in-visual-studio-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Run Windows Service as a console program</title>
		<link>http://einaregilsson.com/run-windows-service-as-a-console-program/</link>
		<comments>http://einaregilsson.com/run-windows-service-as-a-console-program/#comments</comments>
		<pubDate>Wed, 15 Aug 2007 17:01:27 +0000</pubDate>
		<dc:creator>einar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Tips & Tricks]]></category>

		<guid isPermaLink="false">http://tech.einaregilsson.com/2007/08/15/run-windows-service-as-a-console-program/</guid>
		<description><![CDATA[Visual Studio and the .NET framework make it really easy to create Windows Services. All you have to do is create a new project, select &#8216;Windows Service&#8217; as your project type and you&#8217;re all set. However, debugging Windows Services in Visual Studio can be a big pain. The recommended way is to use InstallUtil to [...]]]></description>
			<content:encoded><![CDATA[<p>Visual Studio and the .NET framework make it really easy to create Windows Services. All you have to do is create a new project, select &#8216;Windows Service&#8217; as your project type and you&#8217;re all set. However, debugging Windows Services in Visual Studio can be a big pain. The recommended way is to use InstallUtil to install them, and then restart the service and attach the debugger everytime you want to debug it. I wanted <a href="/projects/windows-live-bot/">Windows Live! Bot</a> to be available as a Windows Service, but I also wanted to be able to debug it without the hassle, so here&#8217;s what I came up with: <span id="more-52"></span></p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.ServiceProcess;

public partial class DemoService : ServiceBase
{
    static void Main(string[] args)
    {
        DemoService service = new DemoService();

        if (Environment.UserInteractive)
        {
            service.OnStart(args);
            Console.WriteLine(&quot;Press any key to stop program&quot;);
            Console.Read();
            service.OnStop();
        }
        else
        {
            ServiceBase.Run(service);
        }

    }
    public DemoService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        // TODO: Add code here to start your service.
    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down
        //necessary to stop your service.
    }
}
</pre>
<p>This will allow you to use your program as either a normal console program or a windows service, with no special builds, #DEBUG directives, command line parameters or anything like that. What it does is in the Main method it checks the &#8216;Environment.UserInteractive&#8217; property. This will be true when it is run from Visual Studio, or when you just click on the .exe file, but false if it&#8217;s being run as a service. When it&#8217;s run from Visual Studio or as a standalone program it will keep running until you press a key, then it will call your OnStop method and then terminate.</p>
<p>Two things to watch out for:</p>
<ol>
<li>You&#8217;ll have to right click on your project in Visual Studio, choose Properties and select the Output type as &#8216;Console application&#8217; for this to work.</li>
<li>If your Main method is not in your service class, you&#8217;ll have to add public methods to your class that can start and stop it, for instance add a <strong>public void StartConsole(string[] args)</strong> that just calls your OnStart, since OnStart and OnStop are protected methods and as such not accessible from other classes.</li>
</ol>
<h3>UPDATE: 16.08.2007</h3>
<p>Reader <a href="http://theimes.com/">Anderson Imes</a> (who has a rather nice <a href="http://theimes.com/archive/2006/12/28/Debugging-Windows-Services-is-a-Pain.aspx">debugging solution of his own</a>) pointed out some things to fix. One thing is that a Windows Service can run many services in the same process, the ServiceBase.Run method can take an array of ServiceBase objects. I still think that the code above is the best way to do it if you have just a single service, just keep the Main method in the service class. But if you&#8217;ve got multiple service objects then that doesn&#8217;t make sense anymore, we want to put our Main method somewhere else. And then we have the problem of not being able to call OnStart in the service objects, since it&#8217;s a protected method. We could define a public StartConsole method in all of them that calls the OnStart method like I described above, but that will get tiresome if you have many objects. Well, there is another way to do it with Reflection which I´ve written here below. The new code is basically the same as the first version, except that it has an array of ServiceBase objects, called servicesToRun, and loops through it, calling each object&#8217;s OnStart method through some neat Reflection tricks. When the user presses a button it will stop all services by calling their Stop method, which in turn calls the OnStop method that all Services must define. </p>
<pre class="brush: csharp; title: ; notranslate">
using System.ServiceProcess;
using System;
using System.Reflection;

static class Program
{
    static void Main(string[] args)
    {
        ServiceBase[] servicesToRun = new ServiceBase[] { new Service1()
                                                        , new Service2()
                                                        , new Service3()};

        if (Environment.UserInteractive)
        {
            Type type = typeof(ServiceBase);
            BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
            MethodInfo method = type.GetMethod(&quot;OnStart&quot;, flags);

            foreach (ServiceBase service in servicesToRun)
            {
                method.Invoke(service, new object[] { args });
            }

            Console.WriteLine(&quot;Press any key to exit&quot;);
            Console.Read();

            foreach (ServiceBase service in servicesToRun)
            {
                service.Stop();
            }

        }
        else
        {
            ServiceBase.Run(servicesToRun);
        }
    }
}
</pre>
<p>While using this trick has worked perfectly well for my services, which have all been pretty simple, it might not work for everyone. I&#8217;m sure there are a few things different between Windows Services and console programs that I don&#8217;t know about, and might make the console version of the program behave strangely in some cases. Imes also pointed out that there was no way to test pause-and-resume in the console version. The best way I could think of to mimic service events like these would be to read in keypresses in the main thread and do the appropriate action depending on the key pressed. For instance, instead of the &#8220;Press any key to exit&#8221; message, we might have &#8220;Press p to pause, r to resume, s to stop&#8221; and then the result of Console.Read() could be used to determine which action to take and which methods to call on the Service objects. However, I don&#8217;t have the time and interest to do it right now, so implementing it is left as an exercise for the reader <img src='http://einaregilsson.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  .</p>
]]></content:encoded>
			<wfw:commentRss>http://einaregilsson.com/run-windows-service-as-a-console-program/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>

