Tag Archives: c sharp

Performance testing LINQ to Objects Except Method

When .NET introduced LINQ and lambdas, I was blown away by the efficiency improvements in writing code. But, after reading countless articles online about how horribly slow and inefficient LINQ (even to objects) was compared with just plain for-loops, I’ve always felt guilty about using it.

The other day, however, I wanted to see how much slower linq would be in comparing 2 lists of objects to find all of the items from list1 that were not in list2. LINQ has an extension method called “Except” just for this.

I coded the solution in LINQ, and then coded a few “solutions” using pre-LINQ techniques…spoiler alert: LINQ was the best for large sets of objects.
Continue reading

Entity Framework Mocking template with IDisposable implementation

I’m working on a C#/.NET StackOverflow clone and wanted to create mock data contexts for unit testing with Entity Framework.

I found a great T4 template that does this from Rab Hallett’s blog, but it did not include the IDisposable interfaces used by the .NET ObjectContext.

I’ve modified the template to include these additions to the generated Mock context so that I can now use the interface in a using block.

            using (var db = DataContextFactory.CreateContentContext(_isMock))
            { //actual code goes here

The entire T4 template (ContentModelGenerator.Context.tt) is on my bitbucket if you want to use it.

Target SQL Server and SQL CE with the same Entity Framework objects while using Sync Framework

When developing a sometimes-disconnected application in .NET, you have the ability to create a “local data cache” (Sql CE file) and then synchronize this local database with a server database periodically (when connected) to keep the data relevant.

Of course to actually interact with the data from your .NET application, you will want to create a data model with Entity Framework. There are a few tricks that can make the whole process much simpler.
Continue reading

AccessViolationException, EntryPointNotFoundException, and Change Tracking Error on SyncAgent.Synchronize()

Recently I was working on a data-driven application which needed to continue operating in sometimes-disconnected environments. In theory, this is a fairly simple problem to solve with .NET Sync Framework using a “Local Data Cache” implementation of SQL Server CE file and SQL Server database–a solution that I have implemented several times in the past.

Never have I had to jump through so many crazy hoops.
Continue reading

IO Permissions Exception during XML Serialization

Today I fired up an application that I hadn’t touched in quite some time–I ran it, and got a very unusual exception error message while attempting to serialize an object from some XML:

Unable to generate a temporary class (result=1).

At first I suspected it was some silly Windows 7 user permissions problem, but I was running everything as Administrator! What could it be?
Continue reading

Minimize Asynchronous Socket Callbacks With Dynamic Buffer Sizes

I’ve been doing a lot of work with .NET Sockets lately, and as I was thinking about the specific needs of my application I got an idea about a more optimal way to use sockets. In this article, I want to talk about the concept of reading a Socket asynchronously, raising events to notify your application when bytes are received, and minimizing the number of times the callback method is processed.

Continue reading

System.Threading.Timer Stops Firing in Windows Server 2003 SP1

Do you have an application that starts misbehaving randomly? You aren’t quite sure what is going on, but it seems like for some reason your Timer just stops firing it’s event handler–and once it stops it never starts back up.

This drove me nuts! But, it is definitely a problem in the Windows Server 2003 SP1. This bug is particularly devious since even if your application uses extensive logging it maybe be nearly impossible to find proof of a timer not firing in your logs.

Short of implementing a Homer Simpson type of “everything is okay alarm” is there anything you can do to determine whether or not your application might be suffering from dying timers?

Continue reading