Things you should know when debugging JQuery and Javascript inside your partial Views in ASP.NET MVC

Well, I’m heavily using JQuery now on a web application and I must say I’m very fond of this library.

It’s very easy to use, encapsulates quite a few cross browser compatibility issues so the developer doesn’t have the pain to check what browser the page will be displayed on. The ajax calls ($.get, $.ajax and $.post) work like a charm and the pages can have painless and cool animations and effects.

But, no matter how robust and sturdy a library is, we always need to debug. I must say Visual Studio 2008 is heaven to debug JavaScript code. I just have to put the debugger; keyword in front of the line to create a break point. The IDE shows the iexplorer.exe process and thread ID (you can also chose to debug on FF or your favorite browser). You can review your locals, add to the Watch Windows or just take a sneak peek with Quick Watch.

All the power of strongly type language debugging is now available for the type agnostic JavaScript and believe me, this is priceless when you debug JSON.

Happy JQuery debugging!

Oops, this post was about debugging JavaScript on the partial views in MVC…

Now I remember what I was suppose to comment on:

If you use your Model properties inside your JavaScript code, you won’t be able to put this code on a separate .js file and include it on your main view header or even include it on your partial view. It won’t work.

Unfortunately the javascript code should be embedded along with your MVC markup on your partial view. You see, the rendering of these HTML helpers happen first on the server side before the page is served, while the js include happens after. Maybe MVC will be smart enough to detect these js includes in the future and render any helper content inside the js includes, as of MVC 2.0 RTM, this is not possible.

Done!

IIS 6.0 does not serve aspx pages out of the box

I have used ASP.NET for quite some time. I have probably always been lucky that the infrastructure or deployment person always enabled aspx on IIS for me. I use a Windows XP with IIS 5 sometimes or Vista with IIS 7. They do serve aspx pages by default, on XP once the .NET framework is downloaded and installed, there is nothing else to tweak.

On Friday afternoon, I had to deploy to an IIS 6 box on windows 2003, to my surprise, it didn’t serve any of the aspx pages. Even a small Hello World project on an simple label. I missed the happy hour with my colleagues and went home completely puzzle. Why? ASP.NET was already available when Windows 2003 saw the light. To my surprise there was nothing on the event log.

The answer came a few hours later and after few Google queries:

IIS 6.0: ASP.NET Is Not Automatically Installed on Windows Server 2003

1. Open IIS Manager, expand the master server node (that is, the Servername node), and then select the Web service extensions node.
2. In the right pane of IIS Manager, right-click the extension that you want to enable. In this example, this is Active Server Pages.
3. Click to select the Allow check box.
Add a New Web Service Extension to IIS 6.0

To permit IIS to serve content that requires a specific ISAPI or CGI extension that is not already listed in the Web service extensions list, follow these steps:
1.Open IIS Manager, expand the master server node, and then select the Web service extensions node.
2.In the right pane of the IIS Manager, click Add a new Web service extension under Tasks.
3.In the Extension name box, type a friendly name for the extension that you want to add (for example, FrontPage Server Extensions).
4.In the Required files box, click Add, and then select the path and the name of the file that will handle requests for the specific extension. After you select the path and the file name, click OK.
5. If the extension must be enabled immediately, click to select the Set extension status to allowed check box.
6. Click OK to save your changes.

Hopefully Mono will run seamless  on Apache one of these days…

Oh Fiddler, how did I live without you?

This is one of the tools that once you get a good grab of it, you can’t let go…

Fiddler link here

For me it’s invaluable the information it gives me to debug my custom soap headers or my JSON streams.

One trick though, if you are hosting your services on a webserver on your own development box, use the machine name instead of localhost on your HTTP requests or Fiddler won’t intercept them.

Other than that the TextView/XML View is the perfect tool to see what you’re sending on your SOAP request:

Happy SOAP envelop debugging 😉

Client proxy stub generation nightmare. SVCUtil generates colliding types. Beware of the porttype name and your data types…

This is one of the things that drives us insane from time to time. You may call this a bug, a lazy developer encapsulating bad code on a code generation tool, or just bad luck, but I quite some hours on this issue.

We have Java services that need to be consumed from C# client proxies. We do have a contract first approach and have the types defined on schema files and the service contracts are defined on their corresponding wsdl 1.2 files.

We validated the wsdl and xsd files with Altova XmlSpy and did the code generation with the svcutil tool several times, as well as the Visual Studio 2008 Add a Service Reference Tool.

The code generation  goes on fine, but the project that contains the proxy classes does not build. There are conflicting type names under the same namespace.

This does not happen with the Java client proxy generation tools, the Java code builds fine.

Here’s part of the problematic wsdl:

<?xml version=”1.0″ encoding=”utf-8″?>
<wsdl:definitions xmlns:tns=”http://service.some.com/Driver/” xmlns:soap12=”http://schemas.xmlsoap.org/wsdl/soap12/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:comm=”schema” name=”Driver” targetNamespace=”http://service.some.com/Driver/” xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/”>
<wsdl:types>
<xsd:schema targetNamespace=”http://service.some.com/Driver/”>
<xsd:import schemaLocation=”Schema.xsd” namespace=”schema” />
<xsd:element name=”UpdateDriver”>
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”driverId” type=”xsd:int” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”driver” type=”comm:Driver” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”driverRelation” type=”xsd:int” />
<xsd:element minOccurs=”0″ maxOccurs=”unbounded” name=”claims” type=”comm:Claim” />
<xsd:element minOccurs=”0″ maxOccurs=”unbounded” name=”convictions” type=”comm:Conviction” />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name=”AddDriverConviction”>
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”driverId” type=”xsd:int” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”convictionDate” type=”xsd:date” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”convictionType” type=”xsd:int” />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name=”UpdateDriverConviction”>
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”driverId” type=”xsd:int” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”convictionId” type=”xsd:int” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”convictionDate” type=”xsd:date” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”convictionType” type=”xsd:int” />
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<wsdl:message name=”CreateDriverRequest”>
<wsdl:part name=”parameters” element=”tns:CreateDriver” />
</wsdl:message>
<wsdl:message name=”CreateDriverResponse”>
<wsdl:part name=”returnValue” element=”tns:CreateDriverResponse” />
</wsdl:message>
<wsdl:message name=”UpdateDriverRequest”>
<wsdl:part name=”parameters” element=”tns:UpdateDriver” />
</wsdl:message>
<wsdl:message name=”UpdateDriverResponse”>
<wsdl:part name=”returnValue” element=”tns:UpdateDriverResponse” />
</wsdl:message>

<wsdl:portType name=”Driver”>
<wsdl:operation name=”CreateDriver”>
<wsdl:input message=”tns:CreateDriverRequest” />
<wsdl:output message=”tns:CreateDriverResponse” />
</wsdl:operation>


</wsdl:portType>

<wsdl:binding name=”DriverSoapBinding” type=”tns:Driver“>
<soap12:binding transport=”http://schemas.xmlsoap.org/soap/http” />
<wsdl:operation name=”CreateDriver”>
<soap12:operation soapAction=”http://service.some.com/Driver/CreateDriver” />
<wsdl:input>
<soap12:body use=”literal” />
</wsdl:input>
<wsdl:output>
<soap12:body use=”literal” />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name=”UpdateDriver”>
<soap12:operation soapAction=”http://service.some.com/Driver/UpdateDriver” />
<wsdl:input>
<soap12:body use=”literal” />
</wsdl:input>
<wsdl:output>
<soap12:body use=”literal” />
</wsdl:output>
</wsdl:operation>

</wsdl:binding>
<wsdl:service name=”DriverService”>
<wsdl:port name=”DriverPort” binding=”tns:DriverSoapBinding”>
<soap12:address location=”http://www.example.org/” />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

and the schema file:

<?xml version=”1.0″ encoding=”utf-8″?>
<xsd:schema xmlns:this=”schema” targetNamespace=”schema” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>

<xsd:complexType name=”Driver”>
<xsd:sequence>
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”person” type=”this:Person” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”driveInNASince” type=”xsd:date” />
<xsd:element name=”driverLicence” type=”xsd:string” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”provinceOfLicence” type=”xsd:int” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”graduateType” type=”xsd:int” />
<xsd:element name=”hasTrainingCertificate” type=”xsd:boolean” />
<xsd:element minOccurs=”1″ maxOccurs=”1″ name=”hasAutoInsuranceBefore” type=”xsd:boolean” />
<xsd:element name=”previousInsurer” type=”xsd:int” />
<xsd:element name=”previousPolicy” type=”xsd:string” />
</xsd:sequence>
</xsd:complexType>

</xsd:schema>

The generated code had a Driver Interface defined and a driver partial class defined.

After quite a few hours trying to figure out what was wrong (we changed the namespace names, the type names etc), a colleague pointed out the name of the port on the wsdl file.

Voilá!!!

On the service definition tag in the wsdl we put:

<wsdl:portType name=”DriverService“>
<wsdl:operation name=”CreateDriver”>
<wsdl:input message=”tns:CreateDriverRequest” />
<wsdl:output message=”tns:CreateDriverResponse” />
</wsdl:operation>

<wsdl:binding name=”DriverSoapBinding” type=”tns:DriverService“>


Now the interface is generated with the name DriverService and not Driver, so there is no collision with the data contract types, whew…

Happy development!!!

Blogger to stop FTP publishing…

Whatever was free once, is no longer free, after all, Google has to get money out of something else, other than ads…
I had used Blogger for a while, probably since their beginning when it was acquired by Google.
I never liked to host my blog on their servers though, and I still won’t. Hosting on my own account gives me the freedom of owning anything I post, whether it is a rant, an image or just random IT knowledge.
Blogger has cut down features little by little. First the labels on the blog, then the amount of available HTML/CSS themes, now the FTP publishing.
Bottom line, you either host with them or you’ll be eventually unable to use them.

I guess the conversion to WordPress is mandatory now. No more procrastination for me.

L.

PS. On the bright side, I’ll be able to close my gmail account, I never use it and it is full of spam. I won’t force the kind people who leave comments to have a Gmail account and I’ll have full control on my ramblings…and the infrastructure that supports it.

To nvarchar or to varchar in SQL Server to accept French text

I found one single character from the French language that its binary representation is not the same for Unicode and Windows-1252. The oe ligature. Let me rant about it…

create table test_table
(name1 varchar (1) ,
name2 nvarchar (1) )

insert test_table
values (‘œ’, ‘œ’)

select * from test_table

select ASCII(name1), ASCII(name2), Unicode(name1), unicode(name2) from test_table

select COL_LENGTH(‘test_table’,’name1′) as Length1, COL_LENGTH(‘test_table’,’name2′) as Length2 from test_table

select char(156), char(339)

drop table test_table

This character is used in the word “eggs” in French
‘des œufs’ means some eggs…

These first two sets of characters from the Latin alphabet have identical Unicode and Windows-1252 (ASCII) character code.
(Basic Latin)
http://en.wikipedia.org/wiki/Basic_Latin_Unicode_block
and
Latin-1:
http://en.wikipedia.org/wiki/Latin-1_Supplement_Unicode_block

Their binary representation is the same as they all fit in one single byte or octet.

However, the extended Latin characters do not have the same Windows-1252 (ASCII) code and Unicode code.
http://en.wikipedia.org/wiki/Latin_Extended-A_Unicode_block

From this character set, French only uses the oe ligature though.

French accents and ligatures and how to type them with the number pad:
a with grave accent
à ALT + 133 À ALT + 0192

a with circumflex
â ALT + 131 Â ALT + 0194

a with tréma
ä ALT + 132 Ä ALT + 142

a e ligature
æ ALT + 145 Æ ALT + 146

c with cedilla
ç ALT + 135 Ç ALT + 128

e with acute accent
é ALT + 130 É ALT + 144

e with grave accent
è ALT + 138 È ALT + 0200

e with circumflex
ê ALT + 136 Ê ALT + 0202

e with tréma
ë ALT + 137 Ë ALT + 0203

i with circumflex
î ALT + 140 Î ALT + 0206

i with tréma
ï ALT + 139 Ï ALT + 0207

o with circumflex
ô ALT + 147 Ô ALT + 0212

o e ligature
œ ALT + 0156 Œ ALT + 0140

u with grave accent
ù ALT + 151 Ù ALT + 0217

u with circumflex
û ALT + 150 Û ALT + 0219

u with tréma
ü ALT + 129 Ü ALT + 154

French quotation marks
« ALT + 174 » ALT + 175

Euro symbol
€ ALT + 0128

The Windows-1252 encoding can be seen here:
http://www.visibone.com/htmlref/char/webascii.htm

For more, see Joel on Software rant :-p

As most questions in technology, the answer is: it depends.

http://stackoverflow.com/questions/35366/varchar-vs-nvarchar-performance

Performance wise, varchar is more efficient, less memory space, 20% to 30% smaller indexes.
Most database drivers will interpret the incoming stream and convert to Windows-1252 encoding, if the server code page is Windows 1252.
If you use way too many characters in the extended Latin group, you have no choice but using nvarchar…if you deal with other languages that are not Romance Languages, you have no choice but nvarchar.

Cheers!

Java Initialization blocks…why should we ever use them?

Coming from a C# world I’m trying to get a grasp on the Java language, mostly for comparison purposes and to see if really the grass is greener on the other side of the fence.

What I like so far of the Java 1.6 that I see C# might benefit from:
1. The main one is cross platform portability. I wish Mono would have more support.
2. Enums are more complex, but I’m wondering if we ever really use that complexity…
3. Static imports, but do they really add to maintainability or make it worse?

What I find confusing in Java vs C#:

1. Checked exceptions: In Java, the exceptions that a method throws must be declared and are part of a method’s public interface. They were left out from C# on purpose. Here’s an interview made by Bruce Eckel (Thinking in C++) to Anders Hejlsberg (lead C# architect and Turbo Pascal creator, I should point out I wrote my first program in Turbo Pascal :-p)…
2. Initialization blocks, not the static ones that are similar to c# static constructors but instance initialization blocks…why would you ever use them? I’m still puzzled…

HTTP 1.1 caching directives for dummies and caching problems for old versions of Internet Explorer.

The reason why I published this link is because a coworker had a problem with viewing a PDF on IE6 when the cache was set to no-store over https.

The problem does not happen with Internet Explorer 7 or above or with FireFox, but the old IEs fail to retrieve the files when the cache directive is no-store or no-cache.

More of this on:

Internet Explorer file downloads over SSL do not work with the cache control headers

Unable to Open or Run Files if “Cache-Control: No Store” Header Set

and

Prevent caching when you download active documents over SSL

And another good reading on Caches on the web:

Caching Tutorial for Web Authors and Webmasters