Archive

Archive for the ‘.NET’ Category

VS2010/.NET4 Coming March 22nd

October 19, 2009 Leave a comment

In case you haven’t heard, VS 2010 and .NET 4.0 are officially scheduled for release on March 22nd. If you’re into playing with Betas (I know I am) and have an MSDN subscription you can go download it now. Those of you without subscriptions will need to wait about a week or so.

What I like most about this version of VS2010 is that someone at Microsoft finally wised up and realized that selling a gazillion different flavors wasn’t working and just confused/angered everyone. So now they’ve got just three:

  • Microsoft Visual Studio 2010 Ultimate with MSDN. The comprehensive suite of application life-cycle management tools for software teams to help ensure quality results from design to deployment
  • Microsoft Visual Studio 2010 Premium with MSDN. A complete toolset to help developers deliver scalable, high-quality applications
  • Microsoft Visual Studio 2010 Professional with MSDN. The essential tool for basic development tasks to assist developers in implementing their ideas easily
Categories: .NET, Team System

Full Expression Tree Support Coming in .NET/C# 4.0

August 24, 2009 3 comments

Yesssssssss! I’ve been waiting and hoping that this was coming in 4.0 and now it’s official: Full Expression Tree Support. This seems like a HUGE step forward to me. With the advent of this feature, one can finally implement support for converting an expression written in C# to something that runs on the GPU. For example, this would enable a scenario in the future where we can actually write WPF shader code using C#/VB and they can be considered safe because a “GLINQ” interpreter could inspect the statements before it converts them to shader code directly on the fly at runtime.

I haven’t blogged in forever (something I want to get back into), but this news warranted a post immediately. Really excited, hope I can find some time to tinker with starting a expression tree based GPU library.

UPDATE/CORRECTION:

Ah, darn it. Looks like I read the post wrong. While full support is coming to the expression library, it looks lilke C# 4.0 will still not implement compiling of full method bodies into expression trees. Do they really think that having the ability to hand write expression trees by hand with the expression library is of any use? ‘Cause I don’t really see it happening… gotta have the language support for it to take off. :\ 

Guess it’ll be another four years of not being able to implement something like rich GPU support inside of .NET languages. :(

Categories: .NET

Create your own ObjectContext Factory To Avoid performance penalties

April 6, 2009 1 comment

Here’s a quick performance tip for those of you using ADO.NET Entity Framework. If you’re like me, you’re using EF in web services as your data access layer. Also like me, I’m sure you want your web services to perform their absolute best. Well, if you use the default constructor when instantiating EF ObjectContext classes, you’re incurring a penalty of metadata lookup every single time which includes the cost of three internally handled exceptions. As we all know, throwing exceptions, handled or not, is one of the most costly things there is runtime in .NET.

For starters, here’s the pattern you’ll see a lot in samples and documentation when, if used in production code, will cost you:

using(MyEntities entities = new MyEntities())
{
    … perform LINQ queries and whatnot here …
}

In this sample MyEntities is the EDMX code-generated ObjectContext subclass that is specific to our model. The code-generated constructor will look like this:

public MyEntities() : base(“name=MyEntities”, “MyEntities”)
{
    this.OnContextCreated();
}

As you can see, the base class constructor that is invoked is the one that uses the connectionString+defaultContainerName overload. Now this causes the following chain of events to occur:

  1. The ObjectContext needs to internally resolve an EntityConnection instance which it will use to do its communication with the DB. So it instantitates the EntityConnection by passing in the name=”MyEntities”.
  2. The EntityConnection checks to see if the connection string is just a named connection string or the full blown real deal. If it’s just a name, now the EntityConnection needs to take a detour through configuration land to resolve the full connection string from ConfigurationManager.ConnectionStrings.
  3. Now that the ObjectContext has an EntityConnection to work with, it needs a MetadataWorkspace to be able to know how to map between the conceptual model and the storage model. So, it’s going to go ask the EntityConnection for it by calling its GetMetadataWorkspace method.
  4. Well, with the path we took, the EntityConnection was created for us without a MetadataWorkspace instance, so it’s gonna have to go and resolve it.
  5. Assuming you’re using assembly embedded EDMXs (the default) this involves going through the assembly manifest to resolve the metadata.
Categories: .NET, Entity Framework

JIT’d JavaScript is all the rage and Microsoft dropped the ball again

January 11, 2009 5 comments

There’s a lot of buzz lately about browsers finally getting JIT’d JavaScript. First it was SquirrelFish in WebKit, then FireFox let the cat out of the bag about their implementation called TraceMonkey  and then Google came out with V8 when they unleashed Chrome on the world. Kudos to all of those teams for pushing performance forward since DHTML/AJAX apps these days are really starting to show the signs of weakness in current JavaScript engine implementations. Now’s when I turn this into a rant on how Microsoft dropped the ball again…

Let’s be honest, Microsoft held the crown for quite some time with their ActiveScripting engine. It was a pretty damn good implementation considering it was created in 1996 and only recently did they actually start to care about its performance again. Microsoft has had a .NET based JavaScript engine since .NET 1.0 (announced in 2000, released in Feb. 2002) which runs on the CLR and, as a result, has spanked the crap out of the ActiveScripting engine since day one. Now, granted, IE6 was released in 2001, so there’s pretty much no way it could have been in there, but… why the heck didn’t they switch to it in IE7? They could have been ahead of the curve and probably still hold a performance crown against these other implementations (or at least come close). Better yet, why didn’t they at least switch to that  in IE8?!? In fact, let’s take it one step further! More recently, as work for the Silverlight 2.1 platform, Microsoft has moved ahead with the Dynamic Language Runtime (DLR) initiative and they’ve provided an entirely new, cross platform implementation of the CLR and a DLR flavor of JavaScript which is probably even more efficient. So, even better, why didn’t IE8 build on that? Why didn’t IE8 just take the dependency on Silverlight 2.0 which is a freakin’ ~2meg add-on which at least 1/3 of which is probably made up for by removing ActiveScript. Not only that, but it would help increase the surface area of Silverlight. Can you say “win, win”? I knew you could.

I’ve worked with Microsoft technology my entire career and I’ve seen the various departments blow integration opportunities so many times I’m really starting to get sick of it.  At least the newer projects within Microsoft seem to be doing a better job, so perhaps they’ve finally got some architects in there that are actually taking in the bigger picture and doing a lot more cross pollination, but man to see IE8 blow it again just makes me shake my head. Between keeping the antique ActiveScripting engine and writing yet another version of a rendering engine that is pretty much redoing everything WPF does with no where near the extensibility or features, I just gotta wonder what that team is thinking. They must have a serious case of “not invented here” syndrome and like writing/maintaining a bunch of plumbing code rather than being able to focus on higher level HTML/CSS specific stuff or spending more time building better browser features. *sigh*

Categories: .NET, Web Development

Velocity Cache API needs TryGetValue

January 11, 2009 1 comment

Ok, I’ve just started working with Microsoft’s Distributed Caching API (aka “Velocity”) and while I’m very happy with the features thus far (can’t wait for notifications!), I really think the API needs a TryGetValue method. Right now you have the Get, GetAndLock and GetIfNewer methods and all of those return type Object. My suggestion is two-fold:

  1. Add the TryGetValue method with similar overloads to Get. Return a bool which, if true, indicates the item was found.
  2. Take it a step further and make the method generic. This will help when working with simple value types like DateTime, Int32, Guid, etc.

Imagine you’re caching a DateTime and want to look it up… here’s an example of how you need to do that with the API today:

object cacheValue = cache.Get(“MyCachedDateTime”);
DateTime myDateTime; if(cacheValue != null)
{
   myDateTime = (DateTime)cacheValue;
}
else
{
   myDateTime = CalculateSomeComplicatedDateTime();

   cache.Add(“MyCachedDateTime”, myDateTime);
}

// … use myDateTime here …

Notice the annoying need to have a temporary object variable (“cacheValue” in the sample). You need that because you can’t cast straight to a value type like DateTime. Now let’s look at what it might look like with the TryGetValue implementation I’m suggestion:

DateTime myDateTime;

if(!cache.TryGetValue<DateTime>(“MyCachedDateTime”, out myDateTime))
{
   myDateTime = CalculateSomeComplicatedDateTime();

   cache.Add(“MyCachedDateTime”, myDateTime);
}

// … use myDateTime here …

There’s no denying the second version results in less code and, IMHO, this pattern is far more legible.

ScriptReferences to ScriptResource.axd for GAC’d assemblies are problematic in server farms

July 29, 2008 Leave a comment

Ok, I just discovered a nasty little problem with ScriptReferences to script files that are embedded in assemblies that are installed in the GAC… starting with System.Web.Extensions itself.

First, in case you’re not already familiar with this subject, the way scripts are referenced when they are embedded into assemblies is by building a URL to the ScriptResource.axd handler that is provided by the ASP.NET AJAX server side runtime. The URL that is build includes two query string parameters:

  1. “d” – this is a encoded/hashed copy of the assembly identity that includes it’s typical .NET assembly identity info (name, version info, etc.)
  2. “t” – this is a timestamp parameter that is taken from the assembly’s last modified date in terms of the file system.

Second it’s important to realize that when assemblies are placed into the GAC they actually receive a new last modified time on the copy of the assembly that is put into %SYSTEMROOT%\Assembly. This is news to me personally, but then again it never mattered before because the times on those files never mattered in the traditional .NET application. So, if you’re installing a GAC based assembly on multiple servers in a web farm, there’s virtually no way that the last modified time on the assembly on one server will end up matching the time on the other servers… unless you were using a virtual server image of course.

So, with those two details out there, I’m sure you already see the problem: the “t” parameter will be emitted differently from each server in your farm resulting in different URLs.

So why is this a problem? Well your end users hit www.foobar.com and ServerA fields the first request. ServerA’s ScriptReference to Microsoft.Ajax.js serves up a ScriptResource.axd URL with a t=1234 (just an example). Now the user does something that requires a new page to be served up and that request ends up going to ServerB. Well ServerB’s ScriptReference to Microsoft.Ajax.js ends up serving up a ScriptResource.axd URL of t=5678 and guess what? Yup, the user has to download that rather large script file all over again because as far was their browser knows the URL is different so the content must be different.

That’s the typical problem most people using ASP.NET AJAX will have. Non-runtime, silent, not deadly, but killing performance none-the-less. Another problem you can have if you’re doing more advanced work is if you dynamically load scripts (using the ScriptLoader) the URLs will not match and you’ll potentially end up trying to load the same script twice and you’ll end up with a runtime error.

This may not seem all that significant, but consider that it will happen for every script that is served out of a GAC based assembly you reference in your web project. Depending on how large those files are and how many servers you have in your farm you may be causing your end users quite a bit of network load/startup time.

<rant>
IMHO, the inclusion of the “t” query string parameter flies in the face of the power of .NET assembly identity. They’ve taken a well defined, working system which was already being leveraged for the “d” parameter and completely broke it by including another piece of information to identify the assembly. The only plausible explanation for the “t” parameter is that they want to support the “lesser” developers out there who aren’t incrementing their assembly version numbers correctly or maybe has to do with the “pure” web project style projects (which I never use personally, always use web application projects), but I’d be willing to bet they don’t even have any version information by default.
</rant>

So how can you solve this problem? Well there’s several ways that I can think of, none of them great:

  1. You can move to host your GAC’d scripts using the file system, but this completely circumvents the entire benefit of having them in the assembly in the first place.
  2. Handle the ScriptManager.ResolveScriptReference event and write the script paths yourself without the “t” parameter. The problem with this approach is that if you wanted to build links to ScriptResource.axd you’re up the creek without a paddle because all of the methods associated with building these URLs are marked as “internal” within the System.Web.Extensions assembly.
  3. You can choose to “Copy Local” the GAC based assemblies in your build, but this really defeats the purpose of assemblies being installed in the GAC in the first place and has farther reaching implications for your applications.
  4. You can manually go and set the last modified time on those assembly files in the GAC across all your servers using a script of some kind.

We’ve opted for #4 here at Mimeo. We’re using a PowerShell script to set the FileInfo::LastWriteTime on the GAC based assemblies to a fixed date for all the servers. It’s a hack, sure, but at least it’s a server configuration hack and not a runtime hack.

Finally, I have submitted a bug to the connect.microsoft.com site about this problem. Please go and vote on it if you feel it’s as important as I do.

Categories: .NET, Web Development

ADO.NET Entity Framework's CompiledQuery when using anonymous projections shows why C# needs "mumble" types

May 15, 2008 1 comment

Ok, that’s a really long title for a post, so what the heck do I mean by all that? Well, I’ve started working with the CompiledQuery class and I’ve run into a language limitation problem that almost makes any kind of performance gain I might get from CompiledQuery not worthwhile when using projections due to the fact that they cannot be anonymous so you’re forced to define a new class yourself to represent the projection.

First off, if you’re not familiar with CompiledQuery, it’s basically an optimization provided by the ADO.NET Entity Framework that enables it to take your LINQ expression once, generate a cached execution plan (not in the SQL sense, but in the entity sense) for it and return you a Func delegate on the fly that takes parameters to allow you to plug in any dynamic values you might need for the query (e.g. for a where clause).

Secondly, if you’re not familiar with the C# “mumble” type problem, jump on over to this excellent post from Ian Griffiths where he details the issues quite well and even talks about the language changes that the C# team was thinking about making to support the concept.

Go ahead… I’ll wait. Done yet? K. :) So here’s why CompiledQuery suffers from this problem annoying problem. The whole point of CompiledQuery is that you can call Compile once for the LINQ expression and store the resulting Func delegate instance so you can reuse it over and over. So ideally you’d want to store this Func delegate in a static variable or, at bare minimum, a member variable right? Here’s how you could create a compiled query that allows you to find all orders for a customer in a specific status. Note that for my examples, I’m assuming we’ve created an EF model over the AdventureWorksLT DB.

public static Func<AdventureWorksLTEntities, DateTime, IQueryable<SalesOrderHeader>> GetCustomersOrdersByStatus =
    CompiledQuery.Compile((AdventureWorksLTEntities entities, int customerId, int status) =>
        from order in entities.SalesOrderHeader
        where order.CustomerID == customerId
            &&
        order.Status == status 
        select order);

I can then call this function and consume it’s results like so:

using(AdventureWorksLTEntities entities = new AdventureWorksLTEntities())
{
    foreach(var order in MyCompiledQueries.GetCustomersOrdersByStatus(entities, someCustomerId, someStatusValue))
    {
       /* ... do something with the order ... */
    }
}

Now, the above example actually works out just fine because we’re selecting the entire SalesOrderHeader entity type so I know the type parameter for my IQueryable<T> is going to be SalesOrderHeader. The problem comes in when you want to do a projection of fields, either from multiple entities or just reducing the number of fields you bring back from a single entity because that results in an anonymous type being generated. For example, assume I want to bring back some data from the customer and just some data from the order for presentation in a list view:

public static Func<AdventureWorksLTEntities, int, int, IQueryable<???>> GetCustomersOrdersByStatus =
    CompiledQuery.Compile((AdventureWorksLTEntities entities, int customerId, int status) =>
        from order in entities.SalesOrderHeader
        join customer in entities.Customer on order.CustomerId = customer.CustomerId
        where customer.CustomerID == customerId
            &&
        order.Status == status 
        select new
        {
            CustomerId = customer.CustomerID,
            CustomerFirstName = customer.FirstName,
            CustomerLastName =
customer.LastName,
            CustomerCompanyName = customer.CompanyName,
            OrderSalesOrderId = order.SalesOrderId,
            OrderDate = order.OrderDate,
            OrderTotalDue = order.TotalDue

        });

So, as you can see, I’m now trying to project an anonymous type that is composed of values from two separate entity types. The problem is not the LINQ query because it works just fine by itself. The problem is that I can’t determine the type parameter type for the IQueryable<T> for the result parameter of my Func variable because that type is anonymous. This is where having the ability to use a “mumble” type and say IQueryable<var> would help out tremendously because the compiler does know the anonymous type.

The only solution to this problem today AFAICT is that you actually need to declare a class to represent the projection and use it to type the Func signature and select it instead of an anonymous type in the LINQ query:

public sealed class CustomerOrder
{
    public int CustomerId
    {
        get;
        set;
    }

    /* ... declare other properties here ... */
}

public static Func<AdventureWorksLTEntities, int, int, IQueryable<CustomerOrder>> GetCustomersOrdersByStatus =
    CompiledQuery.Compile((AdventureWorksLTEntities entities, int customerId, int status) =>
        from order in entities.SalesOrderHeader
        join customer in entities.Customer on order.CustomerId = customer.CustomerId
        where order.CustomerID == customerId
            &&
        order.OrderDate == orderDate
        select new CustomerOrder
        {
            CustomerId = customer.CustomerID,
            CustomerFirstName = customer.FirstName,
            CustomerLastName =
customer.LastName,
            CustomerCompanyName = customer.CompanyName,
            OrderSalesOrderId = order.SalesOrderId,
            OrderDate = order.OrderDate,
            OrderTotalDue = order.TotalDue

        });

And so depending on how large your projection is this forces you to do a hell of a lot of code generation which as of today is very manual. It would be great to have a tool that could generate a strong type based on the anonymous type syntax. Maybe there’s one out there today that I don’t know about?

Anyway, it’s still all stuff the compiler should be smart enough to do for you. The annoying thing is it already does this work, but it’s limited to local scope only. Perhaps another solution would be that, instead of using the var keyword
to “mumble”, they actually gave us a way to give the anonymous type a name inline which would basically name the anonymous type instead of the compiler just generating a random type name.

Categories: .NET

David Teitlebaum on Channel 9 reviewing WPF 3.5 SP1 features

May 13, 2008 Leave a comment

It’s WPF week over on Channel 9 and a new episode went up yesterday which has David Teitlebaum, a PM on the WPF team, giving an overview and demos of the new lower level features that SP1 brings to the table. You don’t want to miss it, so hop on over and check that out.

I think we all know the Effects stuff is the most sought after feature, but I know a lot of people were looking for the WriteableBitmap feature since WPF 3.0 and now they have it and, judging by the demo, the performance is amazing.

Categories: .NET, WinFX

A post on Silverlight 2's layout and rendering

April 14, 2008 Leave a comment

I just wanted to link to this great post on Silverlight 2′s layout and rendering features. Both features borrow heavily from WPF, but there are also important differences. For one, unlike WPF, there is only one tree… no Logical vs. Visual. Also very cool mention of Silverlight 2′s rendering internals being many-core friendly so it scales well on the CPU. Big difference from WPF where rendering is offloaded to the GPU.

Categories: .NET, WinFX

How to do SSL pass-through with WCF (a link for the MVP crowd)

April 14, 2008 1 comment

I sat in on a very informative roundtable discussion about WCF tips, tricks and pitfalls this afternoon here at the MVP conference. In that discussion one of the topics that came up was load balancing. After some brief discussion, I chimed in with a pitfall that people would need to watch out for which is that, if they’re using something like a BIG-IP box from F5, which proxies SSL communication for them, they will have problems getting their message credentials to flow through to the actual web server since communication between the F5 and the web server is done over just plain HTTP. After I finished talking, people were asking for more detail and whether or not I had written something up on the topic. I thought maybe I never ended up writing anything, but it turns out I did! So for those MVPs who are probably here looking for that info now, or for anyone else who might have missed it, here’s a link that write up.

Also, I mentioned it in the original article, but I again wanted to give credit to Pedro Felix because he was a big help in guiding me down the right path to get this all implemented originally. I finally met Pedro today after he recognized the topic and my URL in the roundtable discussion. Thanks again Pedro, nice to finally put a face to the name!

Categories: .NET, Web Services
Follow

Get every new post delivered to your Inbox.