Tuesday, 1 December 2009

Expression Web SuperPreview

Recently I've discovered a relatively new product from Microsoft for web development (Asp.Net or PHP) called Expression Web. The product itself looks more or less like Visual Studio with less options available ;)

The functionality that caught my eye was SuperPreview option. It allows developers to render the pages your working on using chosen browser engine within your development area. Available are all popular versions of IE and any other browser installed on your machine. It actually uses the real IE6 engine instead of any emulators. It also allows you to display the same page on multiple layers, each rendered using different engine. This allows you to find the differences much quicker.

You can read about this cool option in this article.

There is also a separate version of SuperPreview which doesn't require Expression Web but it only supports IE. You can download it from here. Here is how my blog looks like when IE6 & IE8 overlay:



PS. I just wish that SuperPreview functionality was available in VS2010. The Expression Web seems as a new product only from marketing perspective. Its designer/developer area offers exactly the same options as in VS (code/design/split views), except the SuperPreview of course. Since VS already supports ASP.NET development, it would be logical to simply add PHP support to it instead of releasing a new product. When I asked tech guys from Microsoft they couldn't really explain this, which I think proves they think the same :)

Monday, 30 November 2009

PHP on Windows

According to Microsoft 70% of PHP developers are working on Windows, although the majority of PHP services are hosted on Linux (in my case this was actually true so maybe they are right about that). This leads to situation where most of the apps are being developed in an environment different than they are deployed to. This is obviously not the best way to that.

Microsoft claims there are 2 main reasons for that:

  1. Installation process

  2. Although there are many packages available for one-click installation of all necessary PHP components on Windows (e.g. WAMP) they are all configured to work with Apache server. Although IIS is recommended server to be used for PHP on Windows (to get most of what the system offers), developers are afraid of configuring/using it with their apps.

    Microsoft decided to change it by introducing Web Platform Installer. This free tool will download, install and configure all components required to run web applications on your machine for you (both Asp.net and PHP). It also offers installation of popular OpenSource scripts/engines (e.g. for blogs, galleries, etc.) so you can start development faster.

    This seems to be a step in a good direction and makes configuration of PHP and IIS easy like never before. However, there are still some some weak points in my opinion:

    • Developer is limited to one version of PHP (what if you need to install older version?)

    • The installer supports mainly Microsoft technologies e.g. drivers/extensions for MsSql. I haven't seen any option for installation of MySql extensions.

    • Some very popular php apps are missing e.g. phpBB (probably MS promotes an equivalent supported by them)


  3. Performance

  4. This one is actually more important than installation. Microsoft admits that PHP did not performed well on older versions of IIS. However, together with introduction of IIS7 most of the performance issues should have been resolved. The key aspects for improving performance are:

    • Microsoft contributed to creation process of PHP build for Windows e.g. rewritten parts of the code to make better use of the system

    • Implementation of FastCGI for Windows (can by installed with Web Platform Installer)

    • Windows Cache Extensions for accelerating PHP

    • Ability to work in a cloud (thanks to Windows Azure)


    I asked the speaker about any reliable benchmarks comparing performance of PHP in Windows vs. Linux environment that would prove that it is worth considering deployment of my PHP apps to Windows. Unfortunately this strongly depends on the app itself so there is no simple answer to that question.

Soo, who's convinced?

BTW. When I asked the Microsoft guy about examples of large, commercial PHP applications deployed on Windows and performing well he answered that there are some but he's not allowed to name them ;)

Tuesday, 17 November 2009

Code comments generators

Writing comments for your code is not the most interesting part of development. I admit that sometimes it's hard to follow company/project guidelines for code comments in a restrictive way, mostly because the lack of time (I know that's a pure excuse).

Some time ago somebody recommended me to use GhostDoc, which would take care of that for me. Tried it today - simple Shift+Ctrl+D combination and your code is fully commented. Some of the generated comments were even quite descriptive. After quick investigation I found out that all the descriptive comments were copied from base classes (e.g. when method overrides the one in the base class, implements interface member etc). For the new members the comments were generated basing on their names.

In my opinion this could be a handy tool if used correctly i.e. only for generating draft versions that would be corrected/extended manually. However, there is a great danger that most of generated comments would be left unchanged (especially if time is limited). Although it would cause all possible StyleCop rules to pass it would still affect the code quality. I will uninstall it right now because I don't want to be tempted when a deadline is close ;) We don't really need comments that don't introduce any new knowledge.

I'm wondering if there are any positive aspects of using them that I didn't think of? Are there any developers who admit that they're using them and can give some examples proving that this can be very useful?

Friday, 30 October 2009

Asp.Net: Validating data type using CompareValidator

CompareValidator, as the name says, is mainly used to compare value of specified control against value of another control, constant, etc. You can specify the operator that can be used in comparison so you are not limited to checking equality. The following example demonstrates simple comparison of int values (max > min):

Min: <asp:TextBox ID="TextBox1" runat="server"/><br />
Max: <asp:TextBox ID="TextBox2" runat="server"/>
<asp:CompareValidator
ID="CompareValidator1"
runat="server"
ControlToCompare="TextBox1"
ControlToValidate="TextBox2"
ErrorMessage="Max must be greater than Min"
Operator="GreaterThan"
Type="Integer">
</asp:CompareValidator><br />
<asp:Button ID="Button1" runat="server" Text="Submit" />

Which results in:
Results of comparison against different control

Validating data type
Except the basic functionality CompareValidator supports validation of data types. Except the regular operators (Equal, GreaterThan, GreaterThanEqual, LessThan & LessThanEqual) CompareValidator supports an additional one called "DataTypeCheck". It can be used to validate the type of provided value against the type specified in validator. The supported types are: String, Integer, Double, Currency & Date. The validator mainly checks the format of the provided value. In case of Date type it also checks the range (e.g. to ensure that 31st of April is not valid). I like this feature, especially that I've already seen a quite complex custom validator written to do exact this task. Example:

Date: <asp:TextBox ID="TextBox1" runat="server" />
<asp:CompareValidator
ID="CompareValidator1"
runat="server"
ControlToValidate="TextBox1"
ErrorMessage="Invalid date"
Type="Date"
Operator="DataTypeCheck"/>
<asp:Button runat="server" Text="Submit"/>

Which results in:Invalid date provided

Using right format
When validating the type Asp.Net uses the current Page Culture. If you'd like only to accept inputs in specified format you can set the Page Culture property manually. The same date "11.30.2009" will be validated differently for UK and US Cultures (in UK "DD.MM.YYYY" is used, in US it's "MM.DD.YYYY"):

UK:
<%@ Page ... Culture="en-GB"%>

Date invalid for Culture en-GB

US:
<%@ Page ... Culture="en-US"%>

Date valid for Culture en-US

You can also set the page Culture from your code by overriding InitializeCulture() method:

protected override void InitializeCulture()
{
Culture = "en-US";
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en-US");

base.InitializeCulture();
}

Monday, 19 October 2009

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM

I've deployed a WCF service to IIS with security mode set to "Message":





When I tried to called it from my client app I got the following error:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'.

I googled out that this is caused by security settings of the service virtual directory. I configured it to use Integrated Windows Authentication rather than allowing Anonymous access. I did so because I wanted to restrict the access to my service.

Solution:
It came out that using Anonymous Access in this particular case (WCF service in Message security mode) doesn't mean that anyone is allowed to use the service. The authentication is performed by WCF rather than IIS, but it still takes place.

Answer found here.

Tuesday, 28 July 2009

ASP.NET: How to trigger the client-side validation manually

By default the client-side validation is triggered when submitting forms using buttons. However, sometimes you may want to trigger client-side validation on your ASP page manually from custom Javascript. You can achieve that by calling Javascript validation functions provided by the ASP.Net framework directly from your custom code.

The following page source example displays a TextBox and its validation controls (RequiredFieldValidator & ValidationSummary). The validation controls have the same ValidationGroup defined, which allows us to validate different page elements independently. The page displays also a DIV element that will cause the Validation action when clicked:
<!-- Validation Summary -->
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="Validation errors:" ValidationGroup="Group1"/>

<!-- TextBox and its validator -->
Name: <asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Name is required" Text="*"
ControlToValidate="TextBox1" ValidationGroup="Group1"> />

<!-- Div that causes client-side validation when clicked -->
<div onclick="Validate();" >Validate Form</div>

The code above should should produce smth like that when validation is triggered:

Validation form
Now let's take a look at the custom JS code that triggers the validation. There are couple ways to do that:
  • Easy way - works for all validators from the same ValidationGroup:
    function Validate()
    {
    // If no group name provided the whole page gets validated
    Page_ClientValidate('Group1');
    }
  • If you want to validate only specific validators:
    function Validate()
    {
    // Get the specific validator element
    var validator = document.getElementById('RequiredFieldValidator1');

    // Validate chosen validator
    ValidatorValidate(validator);

    // Update validation summary for chosen validation group
    ValidatorUpdateIsValid();
    ValidationSummaryOnSubmit(validationGroup);
    }

Useful links:

Friday, 3 July 2009

C# - Array to Dictionary

Recently I needed to convert an Array of DictionaryEntry<object,object> items into a Dictionary<string,string> object. Here is simple code using LINQ and ToDictionary method for doing that with 1 command:
DictionaryEntry<object,object>[] props = GetSomeProperties();
Dictionary<string,string> dict =
props.ToDictionary(de => (de.Key != null) ? de.Key.ToString() :
string.Empty);
de => (de.Value != null) ? de.Value.ToString() :
string.Empty);

Isn't that simple? :)