Tuesday, January 31, 2012

How can I use keyboard in Windows Phone 7 simulator ?

When I entered into the WP 7 development using the WP7 emulator, the first annoying behaviour noticed was the unavailability of machine keyboard support in the emulator. I had to use the visual keyboard popped up by the simulator. Our Windows Phone 7 application has integration with Azure and ACS which requires authentication every time I run the app. In other words that was the only part which really needs keyboard input. I don't know why I didn't google to get the keyboard support .May be I was in bad impression that the simulator always simulates in the real way.

Days passed and the schedule became tight .Accidently I press on a key and I could see that it is accepting the physical keyboard entries. This made the development easier and didn't get much time to bother about the key which made that magic. Later the week I had to restart my machine and could see that the emulator is not at all accepting the keyboard. This made me to google for that magic key and it was "Page Up". 

Yes really there are some keyboard short cuts. Have a look at the below page for details

Tuesday, January 24, 2012

Ultimate method to disable the viewstate of asp.net web page

Recently I was working on a ASP.Net project template which can accommodate both asp.net web forms and MVC 3 screens together. I started with a MVC 3 application and added a web page .I could see that MVC3 template is missing some configurations in the web.config which makes it little difficult to work. Finally I could see the web forms running along with the MVC views after some changes. But when I put a button inside the webpage and wired its click event the things started breaking. The error I was getting tells about the view state.

The state information is invalid for this page and might be corrupted.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The state information is invalid for this page and might be corrupted.

With the stack trace as

[FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. ]
   System.Convert.FromBase64String(String s) +0
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +77
   System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
   System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
   System.Web.UI.HiddenFieldPageStatePersister.Load() +147

[ViewStateException: Invalid viewstate. 
 Client IP: ::1
 Referer: http://localhost:49849/WebForm1.aspx
 Path: /WebForm1.aspx
 User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)

[HttpException (0x80004005): The state information is invalid for this page and might be corrupted.]
   System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +235
   System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState) +14
   System.Web.UI.HiddenFieldPageStatePersister.Load() +251
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +106
   Mvc3TestApp.WebForm1.LoadPageStateFromPersistenceMedium() in E:\Joy\Code\DotNet\ASPDOTNET\MVC\Mvc3TestApp\Mvc3TestApp\WebForm1.aspx.cs:27
   System.Web.UI.Page.LoadAllState() +43
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +8431
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +253
   System.Web.UI.Page.ProcessRequest() +78
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
   System.Web.UI.Page.ProcessRequest(HttpContext context) +49
   ASP.webform1_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\09b72d60\350d6099\App_Web_uhhehyfz.0.cs:0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Disabling ASP.Net page viewstate

I have compared the code and config with a newly created normal asp.net web forms application.But not able to find any difference. Then I decided to go with disabling the view state.

As everybody does I disabled the view state at the page directive as follows

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm1.aspx.cs" Inherits="Mvc3TestApp.WebForm1" 
Debug="true" EnableViewState="false" 
EnableViewStateMac="false" EnableSessionState="True" 
ViewStateMode="Disabled" ViewStateEncryptionMode="Never" 
EnableEventValidation="false" %>

It didn't fix the issue.Then I started disabling the view state for the controls present in the web page.Still the same error. Then I started google and could see some ideas such as disabling in the web.config.But still the viewstate came and it was not able to deserialize properly.

This makes me to do something myself and started debugging. After some time I could see that I can disable by overriding the LoadPageStateFromPersistenceMedium method. It was very simple.

protected override object LoadPageStateFromPersistenceMedium()
            return null;
            //return base.LoadPageStateFromPersistenceMedium();

Tuesday, January 17, 2012

List of my favourite Visual Studio 2010 Extensions

There is no question that Visual  Studio is my favourite, the most used and best code editor. Now I am using Visual Studio 2010 waiting for the final release of VSTS 2011. When we use VSTS 2010 sometime we feel that, it would be nice if it has one more feature which may increase our productivity. If you start feeling like that don't wait to open the Visual Studio Extension Manager.It will look as below and it is the place where you can get the desired features as extensions which are not delivered with VS 2010 where most of them are developed by third parties.
Select the Online gallery and search for the words which match your interest.I am sure that you will find almost what you need. Just select and install from Extension Manager itself. When I used it I could see that it saved lot of time especially the spell check extension :-) .Below are some extensions I am using .Hope any .Net developer can use those to leverage his productivity.

  • Productivity Power Tools - This brings you a new Tool window called Solution Navigator which is a much improved version of Solution Explorer.Using the Solution Navigator you can drill down till the method name and open the method directly. Also it comes with a good search mechanism and much more.
  • Spell Checker - This works like the office spell check and will surely help you to write method names without spelling mistake.
  • Indent Guides - Draws a line vertically to connect your braces.
  • Regex Editor - A handy utility to test regex patterns before checking in your code.
  • TFS Power Tools - If you are using Visual Studio TFS as code repository.
NB: If you just google for visual studio 2010 extensions you will get tons of VS extension lists like this. But use it, only if you think your system is capable enough to handle the extensions. Otherwise it will decrease your productivity.

Tuesday, January 10, 2012

Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel' after WCF non-http activation in IIS 7.5 with .Net 4.0

"Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'

This is an error you may encounter when you configure your machine ie IIS 7.5 for non-http activation to host WCF services.Initially I wonder what I did to reach this situation.But soon I realized that the relation between IIS and .Net 4.0 has broken and I need to register .Net 4.0 to IIS. Just used the famous aspnet_regiis.exe utility which is located at <%WindowsDir%>\Microsoft.NET\Framework\v4.0.30319.

Mainly this happens when we install .Net framework 4.0 after IIS installation or activating non http wcf service support after IIS and .Net 4.0 installations.

Monday, January 2, 2012

Creating simple ASP.Net MVC 3 RAZOR application and adding a view to list users

I am really new to Razor the new view engine of ASP.Net MVC and decided to have a look at it during last weekend. First I started with what are the specialties of RAZOR and could see that it is more simple than the traditional ASP.Net view engine. Then I searched for the differences between the asp.net view engine and the Razor in terms of usage which will help me to write Razor view.Also I got the list which says how the Razor view elements are converted to HTML elements.

Seems we are ready to taste RAZOR through a sample application. Again searched for creating simple razor application and got the link which explains how to create a user list with option such as edit,list...

But it looks little complicated to me as we need to create model and all. So decided to just list down the user list from Membership provider.Steps goes as follows.
  1. Create a new ASP.Net MVC 3 project with default templates which will setup the membership provider and some pages such as the login, create new user etc...
  2. Create a new action in HomeController.cs named "Users". Add the below code to return the MembershipUserCollection.
    public ActionResult Users()
          return View(Membership.GetAllUsers());
  3. Right click on the "Users" Action method definition and select the "Add view".Use the same master page of other views(~/Views/Shared/_Layout.cshtml)
  4. Go to the added view and add the below lines in it.
        ViewBag.Title = "Users";
        Layout = "~/Views/Shared/_Layout.cshtml";
    @foreach (var user in Model)

  5. Go to master page and add action along with Home and About.
    <li>@Html.ActionLink("Users", "Users", "Home")</li>
Hope the steps are self explanatory. From the Action method in the Controller we are passing the MembershipUserCollection as model and that model is being enumerated in the RAZOR View to display the UserName.

More on ASP.Net MVC 3