Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Archive for the ‘ASP.NET 4.0’ Category

Massive: 400 Lines of Data Access Happiness

Posted by Ramani Sandeep on February 23, 2011

Rob Conery blogs about an interesting lightweight data access he’s writing for WebMatrix that’s focused on simplicity and driving .NET 4 dynamic support for all it’s worth.

Massive is a “wrapper” for your DB tables and uses System.Dynamic extensively. If you try to use this with C# 3.5 or below, it will explode and you will be sad. Me too honestly – I like how this doesn’t require any DLLs other than what’s in the GAC. Yippee.

  • Get a Database. Northwind will work nicely. Add a connection to your database in your web.config (or app.config). Don’t forget the providerName! If you don’t know what that is – just add providerName = ‘System.Data.SqlClient’ right after the whole connectionString stuff.
  • Create a class that wraps a table. You can call it whatever you like, but if you want to be cool just name it the same as your table.
  • Query away and have fun

Read more…

Hope this will helps !!!

Jay Ganesh

About these ads

Posted in ASP.NET 4.0, Web Matrix | Tagged: , , | Leave a Comment »

Parallelism in .NET – PLINQ

Posted by Ramani Sandeep on February 10, 2011

Introduction

Most .NET developers today are familiar with LINQ, the technology that brought functional programming ideas into the object-oriented environment. Parallel LINQ, or ‘PLINQ’, takes LINQ to the next level by adding intuitive parallel capabilities onto an already powerful framework.

PLINQ is a query execution engine that accepts any LINQ-to-Objects or LINQ-to-XML query and automatically utilizes multiple processors or cores for execution when they are available. The change in programming model is tiny, meaning you don’t need to be a concurrency guru to use it.

Using PLINQ is almost exactly like using LINQ-to-Objects and LINQ-to-XML. You can use any of the operators available through C# 3.0 syntax or the System.Linq.Enumerable class, including OrderBy, Join, Select, Where, and so on.

LINQ-to-SQL and LINQ-to-Entities queries will still be executed by the respective databases and query providers, so PLINQ does not offer a way to parallelize those queries. If you wish to process the results of those queries in memory, including joining the output of many heterogeneous queries, then PLINQ can be quite useful.

Using AsParallel method :

The AsParallel method is the doorway to PLINQ. It converts data sequence into a ParallelQuery. The LINQ engine detects the use of a ParallelQuery as the source in a query and switches to PLINQ execution automatically. You are likely to use the AsParallel method every time you use PLINQ.

Sample code 1 : Sequential LINQ execution


	var customers = new[] {
		new Customer { ID = 1,  FirstName = "Sandeep"  , LastName = "Ramani" },
		new Customer { ID = 2,  FirstName = "Dharmik"  , LastName = "Chotaliya" },
		new Customer { ID = 3,  FirstName = "Nisar"    ,  LastName = "Kalia" } ,
		new Customer { ID = 4,  FirstName = "Ravi"     , LastName = "Mapara" } ,
		new Customer { ID = 5,  FirstName = "Hardik"   , LastName = "Mistry" }
		new Customer { ID = 6,  FirstName = "Sandy"    , LastName = "Ramani" },
		new Customer { ID = 7,  FirstName = "Jigar"    , LastName = "Shah" },
		new Customer { ID = 8,  FirstName = "Kaushal"  , LastName = "Parik" } ,
		new Customer { ID = 9,  FirstName = "Abhishek" , LastName = "Swarnker" } ,
		new Customer { ID = 10, FirstName = "Sanket"   , LastName = "Patel" }
		new Customer { ID = 11, FirstName = "Dinesh"   , LastName = "Prajapati" },
		new Customer { ID = 12, FirstName = "Jayesh"   , LastName = "Patel" },
		new Customer { ID = 13, FirstName = "Nimesh"   , LastName = "Mishra" } ,
		new Customer { ID = 14, FirstName = "Shiva"    , LastName = "Reddy" } ,
		new Customer { ID = 15, FirstName = "Jasmin"   , LastName = "Malviya" }
		new Customer { ID = 16, FirstName = "Haresh"   , LastName = "Bhanderi" },
		new Customer { ID = 17, FirstName = "Ankit"    , LastName = "Ramani" },
		new Customer { ID = 18, FirstName = "Sanket"   , LastName = "Shah" } ,
		new Customer { ID = 19, FirstName = "Amit"     , LastName = "Shah" } ,
		new Customer { ID = 20, FirstName = "Nilesh"   , LastName = "Soni" }       };

	var results = from c in customers
		      where c.FirstName.StartsWith("San")
		      select c;

Sample code 2 : Parallel LINQ execution


	var customers = new[] {
		new Customer { ID = 1,  FirstName = "Sandeep"  , LastName = "Ramani" },
		new Customer { ID = 2,  FirstName = "Dharmik"  , LastName = "Chotaliya" },
		new Customer { ID = 3,  FirstName = "Nisar"    ,  LastName = "Kalia" } ,
		new Customer { ID = 4,  FirstName = "Ravi"     , LastName = "Mapara" } ,
		new Customer { ID = 5,  FirstName = "Hardik"   , LastName = "Mistry" }
		new Customer { ID = 6,  FirstName = "Sandy"    , LastName = "Ramani" },
		new Customer { ID = 7,  FirstName = "Jigar"    , LastName = "Shah" },
		new Customer { ID = 8,  FirstName = "Kaushal"  , LastName = "Parik" } ,
		new Customer { ID = 9,  FirstName = "Abhishek" , LastName = "Swarnker" } ,
		new Customer { ID = 10, FirstName = "Sanket"   , LastName = "Patel" }
		new Customer { ID = 11, FirstName = "Dinesh"   , LastName = "Prajapati" },
		new Customer { ID = 12, FirstName = "Jayesh"   , LastName = "Patel" },
		new Customer { ID = 13, FirstName = "Nimesh"   , LastName = "Mishra" } ,
		new Customer { ID = 14, FirstName = "Shiva"    , LastName = "Reddy" } ,
		new Customer { ID = 15, FirstName = "Jasmin"   , LastName = "Malviya" }
		new Customer { ID = 16, FirstName = "Haresh"   , LastName = "Bhanderi" },
		new Customer { ID = 17, FirstName = "Ankit"    , LastName = "Ramani" },
		new Customer { ID = 18, FirstName = "Sanket"   , LastName = "Shah" } ,
		new Customer { ID = 19, FirstName = "Amit"     , LastName = "Shah" } ,
		new Customer { ID = 20, FirstName = "Nilesh"   , LastName = "Soni" }       };

	var results = from c in customers.AsParallel()
		      where c.FirstName.StartsWith("San")
		      select c;

With the simple addition of the AsParallel() extension method, the .NET runtime will automatically parallelize the operation across multiple cores. In fact, PLINQ will take full responsibility for partitioning your data into multiple chunks that can be processed in parallel.

PLINQ partitioning is out of the scope for this article, but if you’re curious about the inner workings of it, this blog post from Microsoft’s own Parallel Programming team does a great job of explaining the details.

When you will run the above sample queries , you might get same output but possibly in different order. Here Sample code 1 is an example of Sequential LINQ execution, while Sample code 2 is an example of Parallel LINQ execution.

Limitations

  1. PLINQ only works against local collections. This means that if you’re using LINQ providers over remote data, such as LINQ to SQL or ADO.NET Entity Framework, then you’re out of luck for this version.
  2. Since PLINQ chunks the collection into multiple partitions and executes them in parallel, the results that you would get from a PLINQ query may not be in the same order as the results that you would get from a serially executed LINQ query.

However, you can work around this by introducing the AsOrdered() method into your query, which will force a specific ordering into your results. Keep in mind, however, that the AsOrdered() method does incur a performance hit for large collections, which can erase many of the performance gains of parallelizing your query in the first place.

Sample code 3 : Preserving the Order of PLINQ Query Results Using the AsOrdered Method


	var results = from c in customers.AsParallel().AsOrdered()
		      where c.FirstName.StartsWith("San")
		      select c;

Controlling Parallelism

1) Forcing Parallel Execution :

In some cases, PLINQ may decide that your query is better dealt with sequentially. You can control this by using the WithExecutionMode extension method, which is applied to the ParallelQuery type. The WithExecutionMode method takes a value from the ParallelExecutionMode enumeration. There are two such values: the default (let PLINQ decide what to do) and ForceParallelism (use PLINQ even if the overhead of parallel execution is likely to outweigh the benefits).

Here is sample code which demonstrates the use of this method :


	var results = from c in customers.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism)
		      where c.FirstName.StartsWith("San")
		      select c;

2) Limiting the Degree of Parallelism :

You can request that PLINQ limit the number of partitions that are processed simultaneously using the WithDegreeofParallelism extension method, which operates on the ParallelQuery type. This method takes an int argument that states the maximum number of partitions that should be processed at once; this is known as the degree of parallelism. Setting the degree of parallelism doesn’t force PLINQ to use that many. It just sets an upper limit. PLINQ may decide to use fewer than you have specified or, if you have not used the WithExecutionMode method, may decide to execute the query sequentially.

Here is sample code which demonstrates the use of this method :


	var results = from c in customers.AsParallel().WithDegreeOfParallelism(2)
		      where c.FirstName.StartsWith("San")
		      select c;

3) Generating and Using a Parallel Sequence :


	IEnumerable<int> evens
		= ((ParallelQuery<int>) ParallelEnumerable.Range(0, 50000))
			.Where(i => i % 2 == 0)
			.Select(i => i);

Above code uses the Range method to create a sequence of 50,000 integers starting with the zero. The first argument to the method is the start index; the second is the number of values you require. Notice that we have cast the result from the Range method to a ParallelQuery. If we don’t do this, LINQ doesn’t recognize the sequence as supporting parallel execution and will execute the query sequentially.

4) Generating and Using a Repeating Sequence :


	int sum = ParallelEnumerable.Repeat(1, 50000)
			.Select(i => i)
			.Sum();

The static Repeat method takes an object and a count and creates a sequence where the object is repeated the specified number of times.

That’s all

Thats all for this article, I have tried to cover all important topic related to PLINQ. I hope this article will help you to start with PLINQ and gain some knowledge from it.

Hope this will helps !!!

Jay Ganesh

Reference links :

  • MSDN – Parallel LINQ – link
  • Difference between plinq and linq - link

Shout it

Posted in ASP.NET 4.0, CodeProject, Linq | Tagged: , , , , , , , , | 4 Comments »

Improving ASP.NET Security with Visual Studio 2010 Code Analysis

Posted by Ramani Sandeep on January 5, 2011

Anyone doing ASP.NET development probably admits, openly or not, to introducing or stumbling upon a security issue at some point during their career. Developers are often pressured to deliver code as quickly as possible, and the complexity of the platform and vast number of configuration options often leaves the application in a less than desirable security state. In addition, the configuration requirements for debugging and production are different, which can often introduce debugging settings in production, causing a variety of issues.

Over the years, the ASP.NET platform has matured and better documentation has been made available through MSDN and community blogs, but knowing which feature or configuration setting to use is often troublesome. Even with good knowledge of the security functionality, mistakes can happen that could result in security vulnerabilities in your application.

Peer code review is a useful process and a good way to catch issues early. Still, not everyone has the time or budget—or knowledgeable peers at hand—for such review.

Since the introduction of code analysis in Visual Studio 2005, developers have been able to automatically analyze their code to see if it complies with a series of best practices ranging from design, maintainability, performance and security. So far, code analysis has been a great tool, but it hasn’t focused on providing best security practice guidance for ASP.NET—until now.

In this article I’ll introduce you to the new ASP.NET code analysis rules that can be used with Visual Studio code analysis as well as with the standalone FxCop application to improve the security of your ASP.NET applications.

Read more…

Hope this will helps !!!

Jay Ganesh

Posted in ASP.NET 3.5, ASP.NET 4.0, Visual Studio | Tagged: , , , | Leave a Comment »

Securing ASP.Net Web Services with Forms Authentication

Posted by Ramani Sandeep on June 10, 2010

In this article we show how Forms Authentication can be used to secure ASP.Net Web Services, using the built-in ASP.Net Membership Provider classes which utilize SQL Server to store usernames and passwords.

Why?

Adding a Web Service, (also called an Application Programming Interface, or API for short) to an existing web site or desktop application (of the client-server variety) is a great way to enable additional and innovative uses of the data it holds, and also extend its reach to different development platforms such as native Apple and Linux applications , or to native mobile device applications such as those on Apple’s iPhone.

Read more

Hope this will helps !!!

Jay Ganesh

Posted in ASP.NET, ASP.NET 3.5, ASP.NET 4.0, Web Services | Tagged: , , , , | Leave a Comment »

ASP.NET 4.0 Features

Posted by Ramani Sandeep on April 27, 2010

The focus of Microsoft’s latest ASP.NET 4has mainly been on improving the performance and Search-engine Optimization (SEO). In this article, I’ll be taking a look at what I think are the most important new features in ASP.NET 4.

    * Output cache extensibility
    * Session state compression
    * View state mode for individual control
    * Page.MetaKeyword and Page.MetaDescription properties
    * Response.RedirectPermanent method
    * Routing in ASP.NET
    * Increase the URL character length
    * New syntax for Html Encode
    * Predictable Client IDs
    * Web.config file refactoring
    * Auto-Start ASP.NET applications
    * Improvements on Microsoft Ajax Library

read more

Posted in ASP.NET 4.0 | Tagged: , | 1 Comment »

Master-Detail Views with the ASP.NET Ajax Library by Dino Esposito

Posted by Ramani Sandeep on February 22, 2010

When you think of data-driven Web pages, most of the time what you really have in mind is a master-detail view of some cross-related data. Master-detail views are ideal for rendering one-to-many relationships, and such relationships are so common in the real world that a Web platform that doesn’t provide an effective set of tools for that functionality is inadequate.

ASP.NET Web Forms has always provided strong support for data binding and a powerful set of data-source and data-bound server controls. In Web Forms, server controls do a great job of rendering hierarchies of data using nearly any possible combination of grids, lists, and drop-down boxes and supporting multiple levels of nesting.

The drawback of the views you get out of Web Forms server controls is not the effectiveness of the rendering, but the static condition.

Users who navigate within a master-detail view typically switch among master records and drill down into the details of the records that are of interest. This interaction is the essence of a master-detail view.

In a classic Web Forms scenario, each drill-down operation may trigger a postback. Many postbacks—and subsequent page reloads— are not what makes users happy these days.

An alternative exists, but it’s not free of issues either. It basically consists of preloading any possible data the user might want to see. The data is then downloaded with the standard page and kept hidden using CSS styles. At the same time, any handler of user actions is rewritten to unveil hidden content rather than triggering a postback. As you can see, this is not an easy way to go.

The ASP.NET Ajax Library, in collaboration with jQuery, offers a much more powerful toolset and makes it possible to write smooth and effective master-detail views that post back asynchronously and only when strictly needed.

Read more

Jay Ganesh

Posted in ASP.NET 4.0, JQuery | Tagged: , | 2 Comments »

Simple task Scheduling using Global.asax

Posted by Ramani Sandeep on February 1, 2010

A frequent requirement for ASP.NET developers is to schedule tasks at regular intervals. This can include site maintenance tasks, like cleaning up old files, emailing newsletters on a schedule etc. This article examines one easy option for managing tasks like these without having to configure external tools, and discusses a couple of alternatives.

Read more

Simulate a Windows Service using ASP.NET to run scheduled jobs By Omar Al Zabir

How to run scheduled jobs from ASP.NET without requiring a Windows Service to be installed on the server? Very often we need to run some maintenance tasks or scheduled tasks like sending reminder emails to users from our websites. This can only be achieved using a Windows service. ASP.NET being stateless provides no support to run some code continuously or to run code at a scheduled time. As a result, we have to make our own Windows Services in order to run scheduled jobs or cron jobs. But in a shared hosted environment, we do not always have the luxury to deploy our own Windows service to our hosting provider�s web server. We either have to buy a dedicated server which is very costly, or sacrifice such features in our web solution. However, running a scheduled task is a very handy feature especially for sending reminder emails to users, maintenance reports to administrators, or run cleanup operations etc. So, I will show you a tricky way to run scheduled jobs using pure ASP.NET without requiring any Windows service. This solution runs on any hosting service providing just ASP.NET hosting. As a result, you can have the scheduled job feature in your ASP.NET web projects without buying dedicated servers.

Read more

Hope this helps

Jay Ganesh

Shout it

Posted in ASP.NET, ASP.NET 3.5, ASP.NET 4.0 | Tagged: , | Leave a Comment »

Best Practices for Speeding Up Your Web Site

Posted by Ramani Sandeep on January 30, 2010

The Exceptional Performance team has identified a number of best practices for making web pages fast. The list includes 34 best practices divided into 7 categories.

1) Content

  • Minimize HTTP Requests
  • Reduce DNS Lookups
  • Avoid Redirects
  • Make Ajax Cacheable
  • Post-load Components
  • Preload Components
  • Reduce the Number of DOM Elements
  • Split Components Across Domains
  • Minimize the Number of iframes

2) Server

  • Use a Content Delivery Network
  • Add an Expires or a Cache-Control Header
  • Gzip Components
  • Configure ETags
  • Flush the Buffer Early
  • Use GET for AJAX Requests

3) CSS

  • Put Stylesheets at the Top
  • Avoid CSS Expressions
  • Choose <link> over @import
  • Avoid Filters

4) Javascript

  • Put Scripts at the Bottom
  • Make JavaScript and CSS External
  • Minify JavaScript and CSS
  • Remove Duplicate Scripts
  • Minimize DOM Access
  • Develop Smart Event Handlers

5) Cookie

  • Reduce Cookie Size
  • Use Cookie-free Domains for Components

6) Images

  • Optimize Images
  • Optimize CSS Sprites
  • Don’t Scale Images in HTML
  • Make favicon.ico Small and Cacheable

7) Mobile

  • Keep Components under 25K
  • Pack Components into a Multipart Document

           Read more

Posted in ASP.NET, ASP.NET 3.5, ASP.NET 4.0, ASP.NET Ajax, Css, JavaScript, Performance | Tagged: , | Leave a Comment »

How to deploy ASP.NET Web Application on server

Posted by Ramani Sandeep on January 20, 2010

Hello, in this article i have tried to explain, how a web application is deployed on to web server.

Deployment refers to the process of copying an asp.net web application from the development system to the server on which the application will be run. There are several way we can deploy our web application

We can deploy ASP.NET Application in  3 different ways

  • xCopy Deployment
  • Precompiled Deployment
  • Web Setup Project  

The choice of best deployment alternative depends upon particular need of each application. Xcopy deployment is the most easiest, and it is often used during development to create copies of an application n different servers for testing purpose. For small application xcopy deployment may be the best choice.

Precompiled deployment has several advantages over XCopy deployment. Eg. Precompiled deployment is always gives better performance for the first users of the site at the same time it is more secure as we don’t need to copy our source code files on to server. If our application deployed on one or few servers then precompiled deployment is usually best choice.

When we are going to deploy our application on number of servers then creating a setup program is a very handy tool. Although creating this setup program is much tedious and involves considerable working, the deployment from this setup program becomes very easier.

read more : http://www.codeproject.com/KB/aspnet/Deploy_web_application.aspx

Hope this will help

Jay Ganesh

Posted in ASP.NET, ASP.NET 3.5, ASP.NET 4.0 | Tagged: , , , , , | 4 Comments »

ASP.NET Dynamic Data Support

Posted by Ramani Sandeep on January 8, 2010

ASP.NET Dynamic Data brings major usability and RAD development changes to the existing ASP.NET data controls. RAD development is significantly increased by the use of a rich scaffolding framework. After you add a LINQ to SQL or Entity Framework data model to a project, you can simply register it with Dynamic Data. The result is a fully functional Web site. Full CRUD (create, read, update, and delete) operations are supported. The site includes filtering by foreign keys and Boolean fields; foreign keys are automatically converted to their friendly names. Smart validation is automatically available, which provides validation based on database constraints for nullable fields, data type, and field length.

The DetailsView and GridView controls have been extended to display fields by using templates instead of by using hard-coded rules that are programmed in the controls. These templates are part of the project, and you can customize them to change their appearance or to specify which controls they use for rendering. This makes it very easy to make a change in one place in your site that specifies how to present dates for editing, as one example. FormView and ListView controls can implement similar behavior by using a DynamicControl object in their templates and by specifying which field in the row to display. Dynamic Data will then automatically build the UI for these controls based on the templates that you specify.

Validation is significantly improved in the controls as well. The controls read metadata for a LINQ to SQL or Entity Framework data model and provide automatic validation based on the model. For example, if a column in the database is limited to 50 characters, and if a column is marked as not nullable, a RequiredFieldValidator control is automatically enabled for the column. (The controls also automatically support data-model-level validation.) You can apply other metadata to take further control over display and validation.

Important Links

Hope this will Helps

Jay Ganesh

Shout it kick it on DotNetKicks.com

Posted in ASP.NET, ASP.NET 3.5, ASP.NET 4.0 | Tagged: , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 124 other followers