About Script#

Have you ever wondered how hard is still writing your client side scripts in JavaScript. With Script# you can write your codes in C# with all its power and compile it into JavaScript for your browsers.

By this magic  you will instantly get all the power of compiled world, IntelliSense support in Visual Studio, Code comments, Refactoring.

Source C# code is C# 2.0 with some limitations. Other limitations is in range of supported JavaScript frameworks.

Supported Not supported
MS Ajax Framework jQuery (in future release)
extjs
Silverlight API
Virtual Earth API
ScriptFX framework (own)

Table: Frameworks supported and unsupported in Script#

Among the others planned features is also Unit Testing, which sounds quite cool.

Links:

Tip: Did you know that ASP.NET MVC, Windows Live Messenger and Mesh uses Script# internally?

Message box from Wsh on one command line

I was looking for some really simple solution how to display MessageBox like this:

From one command line in Windows Scripting host (wscript). The solution is a bit tricky, but works nice:

echo WScript.Echo “Hi” > aa.vbs && start /wait wscript /nologo aa.vbs && del aa.vbs

Advantage is that I don’t need to have prepared script to display the message and I don’t want to left anything behind.

Still, I suggest to think about using some real “not existing” file name when using this in your system.

Making ASP.NET MVC application working on IIS 6

I was always trying MVC applications on IIS 6 and I was stopped while deploying of my new AtomSite engine with 404 errors on all requests that uses routing (Login, any post, .. simply just homepage was working).

After some time when I even had no records of my attempts in log, I have found following blogpost:

http://blogs.msdn.com/b/tonyshao/archive/2010/07/08/enable-asp-net-mvc-on-iis-6-by-wildcard-mapping.aspx

To make it simple, MVC on IIS 6 requires enabled Wildcard mapping. That means that all requests goes throught ASP.NET on IIS and then routing can take its chance to form correct request.

Excel tip – How to convert .NET DateTime to SQL DateTime

I needed to do some data transformation today and part of that was to convert datetime from form:

5/31/2011 11:22:18 AM (.NET US-culture DateTime)

Into

2011-05-31 11:22:18.000 (SQL Server DateTime format)

I wanted to do this quick and for some reason Excel was unable to convert the datetime using its functions. So, I needed to prepare following transformation function:

=CONCATENATE(MID(C90874;FIND("/";C90874;FIND("/";C90874;1)+1)+1;FIND(" ";C90874;
FIND("/";C90874;FIND("/";C90874;1)+1)+1)-FIND("/";C90874;FIND("/";C90874;1)+1)+1-2);"-";
CONCATENATE(IF(LEN(MID(C90874;1;FIND("/";C90874;1)-1))=1;"0";"");MID(C90874;1;
FIND("/";C90874;1)-1));"-";CONCATENATE(IF(LEN(MID(C90874;FIND("/";
C90874;1)+1;FIND("/";C90874;FIND("/";C90874;1)+1)-FIND("/";C90874;1)-1))=1;"0";"");
MID(C90874;FIND("/";C90874;1)+1;FIND("/";C90874;FIND("/";C90874;1)+1)-
FIND("/";C90874;1)-1));" ";IF(MID(C90874;FIND(" ";C90874;FIND(" ";C90874;1)+1)+1;
LEN(C90874)-FIND(" ";C90874;FIND(" ";C90874;1)+1))="PM";VALUE(MID(C90874;
FIND(" ";C90874;1)+1;FIND(":";C90874;1)-FIND(" ";C90874;1)-1))+12;
IF(LEN(MID(C90874;FIND(" ";C90874;1)+1;FIND(":";C90874;1)-FIND(" ";C90874;1)-1))=1;
CONCATENATE("0";MID(C90874;FIND(" ";C90874;1)+1;FIND(":";C90874;1)-
FIND(" ";C90874;1)-1));MID(C90874;FIND(" ";C90874;1)+1;FIND(":";C90874;1)-
FIND(" ";C90874;1)-1)));":";IF(LEN(MID(C90874;FIND(":";C90874;1)+1;
FIND(":";C90874;FIND(":";C90874;1)+1)-FIND(":";C90874;1)-1))=1;CONCATENATE("0";
MID(C90874;FIND(":";C90874;1)+1;FIND(":";C90874;FIND(":";C90874;1)+1)-
FIND(":";C90874;1)-1));MID(C90874;FIND(":";C90874;1)+1;FIND(":";C90874;
FIND(":";C90874;1)+1)-FIND(":";C90874;1)-1));":";IF(LEN(MID(C90874;
FIND(":";C90874;FIND(":";C90874;1)+1)+1;FIND(" ";C90874;FIND(" ";C90874;1)+1)-
FIND(":";C90874;FIND(":";C90874;1)+1)-1))=1;CONCATENATE("0";MID(C90874;FIND(":";
C90874;FIND(":";C90874;1)+1)+1;FIND(" ";C90874;FIND(" ";C90874;1)+1)-FIND(":";
C90874;FIND(":";C90874;1)+1)-1));MID(C90874;FIND(":";C90874;FIND(":";
C90874;1)+1)+1;FIND(" ";C90874;FIND(" ";C90874;1)+1)-FIND(":";C90874;
FIND(":";C90874;1)+1)-1));".000")

Summary of function features:

  • C90874 is the source Cell, just replace with your cell
  • It automatically converts AM/PM into 24hour format
  • It should do left Zero padding into two number format

ILSpy – A free alternative to .NET Reflector

After all the years we get used to use .NET Reflector for free and I personally realy on that as last line of defence to see what is going on in the code. On Feb 2011, RedGate did an annoucement that the tool cannot be available free anymore. Thats a unfortunate.

But luckily, SharpDevelop team took the challenge and build something similar. They calls the tool ILSpy and it really does a good work for me.

Find it out on: http://wiki.sharpdevelop.net/ILSpy.ashx

And check the screenshots for compare with original Reflector.

have a good fun with the new free tool.

HttpModule does not work in IIS Classic mode

IIS 7 introduces two operating modes

  • New Integrated mode, when all served requests are processed by ASP.NET pipeline
  • Classic mode is legacy mode used for backward compatibility. All requests are served by IIS based on its extension handlers configuration. This is how IIS worked before IIS 7. This could make some problems running HttpModules that works correctly with Integrated mode

There is a difference in configuring HttpModule in web.config for Integrated and Classic Mode

Integrated mode

When you see Http Error 500, its probably misconfigured web.config. There are two ways how to do this

Migrating web.config automatically

You can migrate web.config automatically with appcmd (IIS command) typing:

%windir%\system32\inetsrv\Appcmd migrate config “<ApplicationPath>”

or, what I prefer more is to move manually.

Migrating web.config manually

You need to move following HttpHandlers entries:

<system.web>/<httpModules>

<system.web>/<httpHandlers>

into new IIS 7 sections (sytem.webServer)

<system.webServer>/<modules>

and then either remove old sections <httpHandlers> and <httpModules> or add an element into <system.webserver>:

<validation validateIntegratedModeConfiguration=”false” />

Disabling Integrated Mode Configuration validation is better when you need your application working in Classic and Integrated mode, because you then need to have both sections in the web.config.

Classic mode

Classic mode is some kind of backward compatibility mode in IIS. Until IIS 7, ASP.NET was hosted inside IIS as so called ISAPI module. IIS process was unmanaged application (did not need .Net Framework) and ASP.NET application was hosted in managed environment as a plugin. That means, that all requests started processing by IIS process itself and based on some configured rules, processing could be passed to ASP.NET runtime.

Problems with HttpModules in Classic mode

When IIS 7 processes requests in Classic mode, IIS operates the same way and it does in IIS 6. For each request, IIS decides what handler to use, based in handlers configuration for a site. This decision is made based on extension of requested file. When we want to write some redirect plugin, your application will usually redirect from some non-existing content, or at last from some content without specified extension.

What this mean is when you look at the Handler Mapping configuration table in IIS.

This mapping will process all requests with .aspx extension with isapi module. Which one is that can be found by double click to see configuration details on that selected handler mapping.

And we can see, that .aspx pages are processed in a ISAPI module called aspnet_isapi.dll. This is basically unmanaged dll that creates managed environment to run ASP.NET content.

Our URI request is not processed by ASP.NET without .aspx extension

As you probably realized, when your request looks like:

http://www.library.com/book/Pro-ASP.NET-4-in-Csharp-2010

Where is no extension, it does not reach ASP.NET ISAPI module in classic mode processing and the request will finish with HTTP Error 404 – Not Found.

To get over that problem, we need to configure wildcard (*) or any extension to be processed by ASP.NET ISAPI handler.

We need to go to handler mappings again, click to Add Wildcard Script map and configure it to process with aspnet_isapi.dll. Whole path to that dll can be copied from .aspx handler mapping. In the end the dialog should look like:

Where the Name of Handler mapping could be anything meaningful.

On the dialog asking to to allow this extension, you should choose Yes.

Next step is to switch Handler Mappings to Ordered view (to see in what order of processing goes our handler) by clicking to View Ordered List.

In Ordered view, you need to move the new mapping almost at the bottom, right above Static File mapping.

Before we finish, you need to find out, whether your computer runs 64bit Windows. When yes, you probably have .NET also in 64 bit variant. For the case, you have your code compiled for x64bit platform and IIS will process it using .NET 64 (by default if not specified), you also need to specify Wildcard handler mapping for aspnet_isapi.dll. You should proceed the same steps as above except the path will point to 64bit aspnet_isapi.dll.

For finding the path of aspnet_isapi.dll, you can look into configuration of “PageHandlerFactory-ISAPI-2.0-64”, but in most cases it is in “%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll”

Again, you need to enter some meaningful name like “ASP.NET-ISAPI-2.0-64-Wildcard” and switch to ordered view and move new Handler Mapping right above Static File.

When finished, you should have no problem running HttpHandler in classic mode.

Formatted XML in Microsoft Server Management Studio

There is a less known possibility in the SQL Management studio to format XML datatype. The format is fine for reading and you don’t need to format the XML anywhere else.

The whole trick is to use cast to tell SMSS to format the column in the nice form.

This is working with any valid XML, I have tested with varchar(max) and ntext column types.

Just use command similar to this:

SELECT cast(Settings as xml), *
  FROM [BlogEngine].[martinkunccom].[be_DataStoreSettings]

The result you will see is like on following image:

Now, you can simply click on the blue text and it will show inside the SQL Management Studio viewer in a nice form:

Nice, isn’t it ?