European ASP.NET 4.5 Hosting BLOG

BLOG about ASP.NET 4, ASP.NET 4.5 Hosting and Its Technology - Dedicated to European Windows Hosting Customer

European ASP.NET 4.5 Hosting - Amsterdam :: WebSockets in ASP.NET 4.5

clock March 23, 2012 05:49 by author Scott

This post discuss about using WebSockets in ASP.NET 4.5. You can read this post to get an understanding about WebSockets. This post shows piece of code which uses WebSockets in ASP.NET 4.5 and the code is related to simple chat application.

The HTML of Web Form chat application looks as below




It contains a text box where you can type your text message and button where it sends a message to the server. You can notice there is <ul> element which shows the list of other messages which are arriving from the server.


Instantiate a W3C WebSocket JavaScript object and pointed to url to establish a connection to WebSockets Server.




The url starts with ws which runs on HTTP and pointing to .ashx handler which runs on ASP.NET server.


The Chat Handler code looks as below, The handler code checks whether sender is looking for WebSockets connection.






AcceptWebSocketRequest is a new method on Context object in ASP.NET 4.5.


ChatHandler is a wrapper API which implements lower level of WebSockets Handler.


The ChatHandler code looks as below




Override the OnOpen method in ChatHandler Class as below




We are modifying the OnOpen method code by adding the set WebSockets collection variable which contains the set of connection objects.






chatapp exposes a method BroadCast which accepts string or binary payload and send the message to all the objects in the collection. The payload in this piece of code is a JSON object.




Now To handle the messages that coming from the server, we are adding below piece of code in JavaScript by implementing a handler for OnMessage.


<image>


In above code we are actually parsing the JSON object that coming from server using JSON.parse method.


To send a message from client to server you can use the below code




Once this message comes to the server, you can check the type of message and can write the code as below




There is a OnClose method which you can override to close your WebSockets connection. For example when user closes the browser this method will fire and closes the connection.




When you run the application then output is as below


 

 



European ASP.NET 4.5 Hosting - Amsterdam :: WebSockets in ASP.NET 4.5

clock March 23, 2012 05:49 by author Scott

This post discuss about using WebSockets in ASP.NET 4.5. You can read this post to get an understanding about WebSockets. This post shows piece of code which uses WebSockets in ASP.NET 4.5 and the code is related to simple chat application.

The HTML of Web Form chat application looks as below




It contains a text box where you can type your text message and button where it sends a message to the server. You can notice there is <ul> element which shows the list of other messages which are arriving from the server.


Instantiate a W3C WebSocket JavaScript object and pointed to url to establish a connection to WebSockets Server.




The url starts with ws which runs on HTTP and pointing to .ashx handler which runs on ASP.NET server.


The Chat Handler code looks as below, The handler code checks whether sender is looking for WebSockets connection.






AcceptWebSocketRequest is a new method on Context object in ASP.NET 4.5.


ChatHandler is a wrapper API which implements lower level of WebSockets Handler.


The ChatHandler code looks as below




Override the OnOpen method in ChatHandler Class as below




We are modifying the OnOpen method code by adding the set WebSockets collection variable which contains the set of connection objects.






chatapp exposes a method BroadCast which accepts string or binary payload and send the message to all the objects in the collection. The payload in this piece of code is a JSON object.




Now To handle the messages that coming from the server, we are adding below piece of code in JavaScript by implementing a handler for OnMessage.


<image>


In above code we are actually parsing the JSON object that coming from server using JSON.parse method.


To send a message from client to server you can use the below code




Once this message comes to the server, you can check the type of message and can write the code as below




There is a OnClose method which you can override to close your WebSockets connection. For example when user closes the browser this method will fire and closes the connection.




When you run the application then output is as below


 

 



European ASP.NET 4.5 Hosting - Amsterdam :: What is WebSockets and Why Use WebSockets?

clock March 22, 2012 07:39 by author Scott

WebSockets are required to develop the rich and real time web applications. WebSockets can securely enable the real-time web. Before explaining the need of WebSockets we see what current Web is and it’s limitations.

What is Current Web?



We have rich web applications in current web which does the bi-directional communication. HTTP is a request-reply protocol and it is hard to PUSH on top of this protocol. Current programming model use different techniques to get connect with server, one of them is Periodic Polling.




example: Browser uses XmlHttpRequest to periodically query the server.




In Long Polling Server holds the HTTP request until there is a data to return. Client Sends the data as soon as previous request completes.


The limitations with current programming model

-
Periodic polling technique uses high-latency.
-
Long polling programming model is unintuitive.

Many scale out and Bandwidth issues with current programming model and limited cross domain support too.

What All WebSockets about is?

WebSockets is a new interoperable technology still undergoing standardization.

-
It is full duplex bidirectional socket.
-
You can program against using a browser and JavaScript API.
-
WebSockets runs over SSL
-
Designed for high performance – it is a light weight messaging protocol, it requires low bandwidth and latency.
-
Important thing is it is designed to broad reach.
-
WebSockets also has the built-in capability to make cross-domain calls.

What Applications can get benefit from WebSockets?

It supports

-
Rich web applications
-
Real-time applications and services(stock tickers and monitoring systems)
-
Beyond browser – Windows 8 and mobile
-
Online games

WebSockets are in

-
Windows 8
-
Internet Explorer 10
-
.NET 4.5

How it works?

The secret source for WebSockets protocol is HTTP handshake which enables the socket connection. When a client sends an HTTP request to WebSockets server, it contains the some information in header which tells the server to establish connection over WebSockets. If Server is happy to accepts the request and it then sends the HTTP response back to client and switches the protocol and starts the communication.

Once the handshake is complete client and server communicates over WebSockets using HTTP connection. The Message communication is in either binary or UTF8 format.

 



European ASP.NET 4.5 Hosting - Amsterdam :: Request Validation in ASP.Net 4.5 Beta

clock March 21, 2012 12:25 by author Scott

The next version of Microsoft’s ASP.Net framework is currently in Beta and there are some pretty cool changes to how Request Validation works in version 4.5. Up until now, there were two ways to enable or disable request validation:

1. Globally – This controls request validation for the entire application.

2. Per Page – This controls request validation on a per-page basis.

In 4.5, the idea is to allow disabling request validation at the field level. This is a huge improvement, because it allows request validation to be enabled on a much larger scale and only be disabled for specific functionality. The first step in taking advantage of this is to make sure that the request validation mode is set to 4.5 in the web.config (shown below).

<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime requestValidationMode="4.5" targetFramework="4.5"
               encoderType="System.Web.Security.AntiXss.AntiXssEncoder,
               System.Web, Version=4.0.0.0, Culture=neutral,
               PublicKeyToken=b03f5f7f11d50a3a" />

Now that the application is set up for the new validation mode, we can start taking advantage of this.

There are two ways to disable request validation on a specific control, a textbox for example. The first way, and probably the easiest, is to set the validation request mode to “disabled” in the html markup. The code below shows how this would look.

<asp:TextBox ID="txtASPNet" ValidateRequestMode="Disabled" runat="server" />

The second way is to set the validation request mode programmatically. This must be done in one of the earlier events for it to be effective. During my testing, it worked in the Page_Init event, but not in the Page_Load event. The code below shows how to do this in the Page_Init event.

protected
void Page_Init(object sender, EventArgs e)
{

    txtASPNet.ValidateRequestMode = System.Web.UI.ValidateRequestMode.Disabled;
}


Web controls are not the only way to have such granular control over retrieving data without having it run the request validation. A new collection that was added to the request object is called “Unvalidated”. This collection allows accessing specific parameters, form variables for example, without checking the value against request validation. This is possible because Request validation has been modified to run when a variable is used, not when the request is made. It is important to note that web controls are always used, because the framework populates the controls automatically. Lets take a look at accessing an html input field without triggering request validation.


protected
void cmdTest_Click(object sender, EventArgs e)
{

    // Access directly from the Unvalidated collection.
    Response.Write(Request.Unvalidated["txtHtml"].ToString());

    // Specify which Unvalidated collection to access.
    Response.Write(Request.Unvalidated.Form["txtHtml"].ToString());
}


As you can see, there are two ways to access the Unvalidated collections. You can pass the index to the collection directly, or specify exactly which collection you want to retrieve this data from. In this case, it was the forms collection.


Through my testing, I have not yet been able to access Unvalidated.Querystring values because the URL gets run against Request Validation so an exception is thrown before I get a chance to access the unvalidated version. I have not tried the other collections yet.


It is important to remember that manual validation should be performed in addition to using Request Validation. This is especially true for the unvalidated fields, but should also be practiced for fields that are validated. Request Validation is a very limited input validation and does not perform enough validation to be appropriate on its own.



Premier European HostForLIFE.eu Launches ASP.NET 4.5 Hosting Beta

clock March 7, 2012 07:22 by author Scott

HostForLIFE.eu was established to cater to an under served market in the hosting industry; web hosting for customers who want excellent service. HostForLIFE.eu – a cheap, constant uptime, excellent customer service, quality, and also reliable hosting provider in advanced Windows and ASP.NET technology. We proudly announces new FREE ASP.NET 4.5 beta hosting in our entire servers environment.

You can start hosting your ASP.NET 4.5 beta site on our environment for FREE. For more information about our new product, please visit our site at http://www.hostforlife.eu/ASPNET-45-Beta-European-Hosting.aspx

"This is limited program offered as an open beta for developers on a first come first serve basis. Our .NET 4.5 beta hosting account comes with 50 MB disk space and 50 MB SQL 2008 database space. With this .NET 4.5 beta release, we prove our existence in this hosting world and also we want the developers to give it try with the newest .net 4.5 in our hosting environment." said John Curtis, VP Marketing and Business Development at HostForLIFE.eu

So, why wait longer? Please visit http://www.hostforlife.eu/ASPNET-45-Beta-European-Hosting.aspx to register new account.

About us

We are European Windows Hosting Provider which FOCUS in Windows Platform ONLY. We support Microsoft technology, such as the latest ASP.NET 4, ASP.NET MVC 3, SQL 2008/2008 R2, and much more.

Our number one goal is constant uptime. Our data center uses cutting edge technology, processes, and equipment. We have one of the best up time reputations in the industry.

Our second goal is providing excellent customer service. Our technical management structure is headed by professionals who have been in the industry since it's inception. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.



European ASP.NET 4.5 Hosting :: How to Optimize Your Site Using ASP.NET 4.5

clock January 20, 2012 05:55 by author Scott

In this article, I will show you how to optimize your website performance. Yes, there are many ways to optimize your website performance. Ok, here we go, hope you enjoy this article.

Typical website may have the following architecture. We can do optimization in each layer but this post specifically talks about ASP.NET 4.5/IIS which is a presentation layer.

Page Request Tree  when a page load in browser you will get page request tree as shown below. You can get this tree if you use Page Speed(web developer tool). This can be downloaded from
here. You can also use the toll YSLOW to analyze your webpage, can be downloaded from here.



If you look at the Request tree of a test web page above, the top box which is actually the html. The more requests you get to your site then longer the tree and in-turn slows the site.

Typical web site contains CSS files, Images and Javascript files along with you HTML elements. CSS files, Images and JS files will take some time to load into the browser though the loading time is in milliseconds but matters.



The HTML is not taking much time but other elements are taking time to load in to the browser.

The Typical ASP.NET web site might look like as below in Visual Studio. It may contain Scripts  folder, Images Folder, Styles Folder and a Default aspx page.



Usually you refer them in your page as shown below



If you run the Page Speed tool on above page then you might get the below score



it also gives you the suggestion where else you can improve the site performance. If you look at the same page in Yslow then you might get the below statistics



There are 46 HTTP Requests, 5 Java Script files, 6 Style Sheet files and 8 images. Total weight of the page is 11.5K. Some of the browsers actually cache these images, we do not have a control on their logic.

The first problem that we can see from the above report is too many HTTP requests which are going to images, CSS files and to JavaScript files.

We can use Bundling and Minifying the files to reduce those requests. In ASP.NET 4.5 you have the built-in features to these. Write one line of code in Global.asax to bring these HTTP Requests down. You can read more about bundling in ASP.NET 4.5
here



The above line enables the minification for CSS and Javascript files, only these two. Minifying means removing whitespaces, comments and everything that browser does not need to understand. We can really compress these files using this technique.

Basically this bundling technique looks at the folder and takes all the files inside and bundles them into one file, no matter how many are in the folder. This all happens at runtime. It only happens at once.

The order of bundling of your files goes as first it takes all Jquery scripts first and then it takes custom scripts alphabetically from your solution explorer.

Instead of doing the references to individual files, You can do this



Styles/CSS is the convention. Folder name / CSS bundles all the css files on that folder. We can do the same foe JavaScript as shown below



Results of writing above lines of code are shown below which makes HTTP requests down to 37



Suppose if you want to bundle the files by taking from different directories in bundle into single file by using the below code



In above code we are registering our own bundle named mycss and then we are adding file styles.css and a directory styles.

Compress components with gZip. we can enables this on IIS. You tell the server everything that respond to client that text based zip it. You can do this by changing the couple of attribute values in web.config file



In IIS 7.5 it enables for you by default, if you running on windows server 2008 then you need to set the attribute values to true.

Encoding the Images to Base64 Images



Above code shows before and after encoding the image.

You may not want to encode all images in your project but if you want the images that you want to embed along with style sheets then you can write some regular expressions as shown below.



After doing the above steps then we are ending with 19 page requests



We can even transform your response further using coffee script as shown below





You can optimize the images in your folder by using Visual Studio extension tool named Optimize Images.



You can see the before and after percentage of optimization of images in output window



You can read more about this concept
here



European ASP.NET 4.5 Hosting :: New Features in ASP.NET 4.5

clock January 20, 2012 05:08 by author Scott

Another major release in .NET Framework, .NET 4.5 which allows the developers to use Windows 8 technologies and windows runtime directly from .NET 4.5. It makes easy and natural to write Metro style applications using C# and VB. .NET 4.5 makes your applications run faster eg: Faster ASP.NET startup. .NET 4.5 gives  you easy access to your data with entity framework code first approach and recent SQL Server features. This post discuss these features in detail.

You can download the .NET FW  4.5 Developer preview from
here.

 



European ASP.NET 4.5 Hosting :: Visual Studio 2011 Preview - Server Side Event Handler Generation from ASP.NET Markup

clock November 11, 2011 06:08 by author Scott

Yes, Visual Studio 2011 will coming soon. This is really a great addition in Visual Studio 2011 Developer Preview for ASP.NET developers. If you are familiar with  WPF and XMAL development in Visual Studio, it should be well known for you, because  preview version of Visual Studio supports  generation of  event handler code from XMAL markup itself.  With Visual Studio 2011 developer preview, creating event handlers for ASP.NET controls have become significantly easy. You really don’t not need to write the event handler manually or  generating the even handler from design view.

In VS 2011 Developer preview, markup intellisense for all ASP.NET server-side events will show you list of exiting handlers and a value called “<Create New Event>” . Which means you can attach the event with some exiting event handler or  select “create New event” to generate an new  handler with the right signature in the code-behind file.



If there is no control Id provided, Visual Studio will consider the control id as “Unnamed” and will generate the event handler as shown in below.





If you are wondering  about what will happen if there are multiple controls  without ids, does Visual studio going to attach with same event handler? No, It will  append an incremental number with event handler name.



Visual Studio will generate Event handler name as <ControlName>_Click if control id is provided.





About HostForLIFE

HostForLIFE is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2019 Hosting, ASP.NET 5 Hosting, ASP.NET MVC 6 Hosting and SQL 2019 Hosting.


Month List

Tag cloud

Sign in