Galin Iliev's blog

Software Architecture & Development

Run scheduled tasks in managed code

Easy thing, isn't it? Just have to create a database that will have all tasks with relevant information about them and then create a service which will pull them and execute the tasks when needed. Not so hard! As making a wheel :) Just bend a thin wood!

Alternatively you might consider using Windows Task Scheduler.

Bart De Smet wrote an article "Calling the Task Scheduler in Windows Vista (and Windows Server 2008) from managed code"

Hope this helps :)

Cool commands for Visual Studio

Do you know the great Visual Studio 2003/2005/2008 add-in called CoolCommands and it written by Gaston Milano? It has some very nice features and it is very easy to get used to it. There is a little hassle to run it on VS 2008 but keep reading - on the bottom of this post are the required steps.

Here is what makes me use CoolCommands:

Locate in Solution Explorer

Expands solution explorer's tree in order to select opened file - very helpful with huge projects while 'Go to definition' command is used to jump from one project to another.

image

Demo Font

Quickly toggles between an 18-point font size and your preferred (Tools | Options | Environment | Fonts & Colors) font size. This is very handy shortcut for while you're giving a presentation.

image

Add As String Resource

Context menu item displayed in response to a right-click in a text editor window - Pops up a dialog to allow you to easily specify a key and value (defaults to the selected text), and a dropdown list of resource files to which you can add the new string resource.

 

These ones impressed me much but read about all the features on Brian Schoroer blog post.

Here is how to install:

Luckily there is version 4 and it supports Visual Studio 2008 but with little changes.

  1. Download v4.
  2. Extract it to a folder you won't delete later
  3. modify setup.bat to include this line
    regpkg CoolCommands.dll /root:Software\Microsoft\VisualStudio\9.0 /codebase
  4. Run setup bat and it is done!

How (not) to bloat SQL Server execution plan cache?!

Short answer: don't use SqlParameterCollection.AddWithValue() method!

Why?! Because if you have query like this:

   1: string lastName = "Galcho";
   2:  
   3: SqlCommand selectCommand = new SqlCommand(
   4:         "SELECT ContactID, FirstName, LastName
   5:         FROM AdventureWorks.Person.Contact
   6:         WHERE LastName = @LastName",
   7:         connection);
   8:  
   9:  
  10: selectCommand.Parameters.AddWithValue("@LastName", lastName);

the execution plan cache entry is like this

(@LastName nvarchar(6))SELECT ContactID, FirstName, LastName FROM AdventureWorks.Person.Contact WHERE LastName = @LastName

So if you execute the code with different value in lastName variable you'll get different first part of the cached executed plan.

Using selectCommand.Parameters.Add("@LastName", SqlDbType.NVarChar, 50).Value = lastName; instead is a way better.

Read full story here

So, what's new in .NET 2.0?

That's right! There is new stuff in .NET 2.0 SP1 libraries and you should know about changes in red bits because there will be MethodMissingException exceptions if you deploy solution developed with VS 2008 with .NET 2.0 as target framework and deploy it on machine with .NET 2.0 (without SP1).

Scott Hanselman created a nice compilation with changes.

Note: this do not affect existing solutions at all because of .NET framework compatibility policy unless you've taken an advantage of fixed bug. In this case you should test affected components (UPDATED in order to include valueable comments from Daniel Moth)

blog posts on the subject:

LINQ to SQL vs LINQ to Entities

Elisa Flasko from Microsoft Data Platform Team wrote a nice article "Introducing LINQ to Relational Data" highlighting main features or two main ORMs coming out form Microsoft with Visual Studio 2008 (ADO.NET EF is not released so far but it will be released as additional package to VS2008)

In short:

LINQ to SQL is applicable when:

  • Use an ORM solution and my database is 1:1 with my object model
  • Use an ORM solution with inheritance hierarchies that are stored in a single table
  • Use my own plain CLR classes instead of using generated classes or deriving from a base class or implementing an interface
  • Leverage LINQ as the way I write queries
  • Use an ORM but I want something that is very performant and where I can optimize performance through stored procedures and compiled queries

LINQ to Entities is applicable when:

Write applications that can target different database engines in addition to Microsoft SQL Server

Define domain models for my application and use these as the basis for my persistence layer.

Use an ORM solution where my classes may be 1:1 with the database or may have a very different structure from the database schema

Use an ORM solution with inheritance hierarchies that may have alternative storage schemes (single table for the hierarchy, single table for each class, single table for all data related to specific type)

Leverage LINQ as the way I write queries and have the query work in a database vendor agnostic manner.

Use an ORM but I want something that is very performant and where I can optimize performance through stored procedures and compiled queries

 

Read article on MSDN or download Word 2007 version.

Building a Volta Control : A Flickr Widget

Tanzim Saqib wrote a nice article about Microsoft Volta in which he gives step by step instructions on how to write a Flickr control that you can reuse  in your pages.

Microsoft Live Labs Volta is Microsoft's emerging toolset that enables developers to build multi-tier web applications by applying familiar .NET techniques and patterns. The intended use of this toolset is for Developers to build web application as .NET client application, and then specify the portions of the codes that will run on the client and server side. The compiler then creates cross browser JavaScript, Web Services, communication, serialization, security etc. to tie the tiers together. This article is based on the first CTP of Volta considering its current limitations. We will see how we can create a Volta control that the compiler can convert into an AJAX Widget without requiring us writing a single line of JavaScript code. We will write code in our very familiar C# language.

Interesting reading! Full Article.

.NET 2.0 SP1 improves performance in ADO.NET

ADO.NET team made a blog post in which describes the how much the performance is improved by SP1.

For a complex real world web application, like Petshop, the improvement is not so big, since the application does many other things in addition to data access, but still you should get part of the benefits.

A summary of our lab numbers, just as a reference of how much improvement you can get:

Scenario .NET FW 2.0 .NET FW 2.0 SP1 Improvement
SqlReader 14855 18100 27.3%
DataSet insert 9637 12890 40.8%
Pet Shop 4.0(Browse the store) 22.44 24.40 8.72%
Pet Shop 4.0 (Buy some pets) 21.54 23.04 6.99%

* Average throughput, over a set of runs, on 4 proc dual core servers with W2K3 SP1, 16 Gb. of RAM, with a sample average load (not by far peak capacity). Hardware differences, network conditions and the way your scenario is written affect performance, among many other things, so your mileage may vary.

If you haven't installed it yet - go for it. You can only benefit from it!

Internal .NET Framework Data Provider error 6.

For all these year I've been using .NET framework this is first encrypted exception that I get. Here is what I get from quick search

If you are getting the error “Internal .Net Framework Data Provider error 6″ and you are using SQL Server 2005 with database mirroring know that its a bug in the SQL managed client code.

Check out KB article 944099 for more info.

At the time of writing this post the only solution for this problem is a private hot fix that you need to obtain from Microsoft.

The links around the web for the Hotfix Request Web Submission form seems to be broken. I’ve contacted Microsoft and got this replacement link.

Use this link to contact Microsoft, state that you require the hotfix for KB article 944099 and submit the form. You will be contacted shortly by a Microsoft representative that will give you the download details or request further information to better understand your needs.

Source dotnetdebug.net

InternalDotNetException

Call MethodInfo.Invoke with an out parameter

Here is a trick inspired by Visual C# Forums and C# MVP Mattias Sjögren. In this code snippet int.TryParse() method is called using reflection and get result from out parameter.

int parNum = 0;
string parText = "99";
object[] methodParms = new object[] { parText, parNum };

MethodInfo methInfo = parNum.GetType().GetMethod("TryParse", new Type[] { typeof(string), typeof(int).MakeByRefType() });
methInfo.Invoke(null, methodParms);
parNum = (int)methodParms[1];
Console.WriteLine("Parsed number:{0}", parNum);