Category Archives: Core .NET

C# core development of all types (desktop, Silverlight, ASP.NET, etc.)

Writing Automated Performance Tests

I found a memory leak in my application, and wrote an automated test using GC.GetTotalMemory to tell if objects were leaking or not. They were (20MB of them in 100k instances). Continue reading

Posted in Core .NET, Silverlight, Wndows Forms, WPF | Tagged , | Leave a comment

DriveInfo: Total vs. Available Free Space

DriveInfo.AvailableFreeSpace gives you free space with quotas applied; on the other hand, TotalFreeSpace gives you the total, physical amount of empty space on the drive. Continue reading

Posted in Core .NET, Wndows Forms | Tagged | Leave a comment

Running Windows Services in Visual Studio

You can run a project that’s a Windows service with an infinite loop (yes, really) by invoking the service and waiting. Forever. You can do this conditionally (while not debugging), instead of running ServiceBase.Run. Continue reading

Posted in Core .NET, Wndows Forms | Tagged | Leave a comment

Using Enumerations as Bit Flags

You can use enumerations as bit flags. The FlagsAttribute is optional; more importantly, you need to assign powers of 2^n to the enum’s values. Depending on the .NET version, checking can be easy (.HasFlag) or hard (bitwise AND operation) Continue reading

Posted in Core .NET, Silverlight, Web, Wndows Forms, WPF | Tagged , | Leave a comment

Creating an Instance Without Using Constructors

You can create an instance of a class without calling any of its contructors, via a call to System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(A)). Scary, but useful in certain cases (like creating instances of highly-coupled or hard-to-instantiate classes in tests). Continue reading

Posted in Core .NET, Web, Wndows Forms | Tagged | Leave a comment

Storing a String List in App.Config or Web.Config

How can you store a list of strings (or ints, say) into a single appSetting key/value pair? Surprisingly, it’s as easy as using a delimited list (like comma-delimited or semicolon-delimited) and splitting it at runtime. Continue reading

Posted in Core .NET, Silverlight, Web, Wndows Forms, WPF | Tagged , | Leave a comment

Procedure Expects Parameter Which Was Not Supplied

SqlCommand’s Parameters prevent SQL injection attacks. Surprisingly, when passed in a null value, you get the exception: Procedure or function “…” expects parameter ‘@foo’, which was not supplied. Continue reading

Posted in Core .NET, Web, Wndows Forms, WPF | Tagged , , | Leave a comment

Rails-Style Test Database Creation in ASP.NET MVC

How can you get ASP.NET MVC to create your database on the fly for tests? Well, you can, if you use migrations for your application to build the tables, drop and recreate all the tables prior to running any tests. Continue reading

Posted in Core .NET, Web | Tagged , , | Leave a comment

Fetching Connection Strings from App.Config in a Library via ConfigurationManager

System.Configuration.ConfigurationManager doesn’t (easily) allow you to read connection strings from an App.Config file inside a library. You need to add a few extra steps to pull this off, like using KeyValueConfigurationElement. Continue reading

Posted in Core .NET, Web, Wndows Forms | Tagged , , | Leave a comment

Using NInject For Compile-Time AOP

An update to my aspect-oriented design pattern for compile-time checking: you can implement aspects as interfaces with singular implementations, and use Ninject to inject the interface implementations. Smooth, easy, and it works! Continue reading

Posted in Core .NET, Silverlight, Web, Wndows Forms, WPF | Tagged , , | Leave a comment