Skip to content

{ Tag Archives } C#

How to Fix MSBuild Error MSB4006

You may encounter an error which looks like this:
MSB4006: There is a circular dependency in the target dependency graph involving target "ResolveProjectReferences" [MyProjectName\MyProjectName.csproj]
…when running MSBuild from the command line.
This error happens when:

You run MSBuild on a machine with .NET 4.5 installed and
You build a project referencing a solution where the SLN file contains dependencies in [...]

Also tagged ,

Ad-hoc SQL/POCO Queries in Entity Framework 4.0

Since version 4.0, the Entity Framework has had the ability to query un-mapped data and project it onto POCOs using ad-hoc SQL.
Here, for example, is how we check the current SQL Server version:

internal class SqlVersionInfo

Also tagged ,

Testing Cross Cutting Concerns

So imagine you’ve been asked to implement the following requirement:
When a to-do list item is marked as complete, the CompletedOn date time property shall be set to the current time.
That doesn’t sound so hard to implement, but how can I test it? I don’t know precisely what the value of the CompletedOn property "should be" [...]

Also tagged , ,

Why Won’t Visual Studio Step Into This Code?

I helped another developer debug an interesting problem this morning. Let’s see if you can spot the problem. The code in question looked something like this simplified version containing only enough code to show the problem:

public void Execute()
DoStuff(); // breakpoint 1

public IEnumerable<Coordinate> DoStuff()

Also tagged ,

How to Avoid Cut and Paste Code with ASP.NET MVC 2 Model Validation

In this post, I will demonstrate how to make your own model validation attributes in order to share common validations throughout an ASP.NET MVC application, and which support MVC 2’s client-side validation feature.
Validating a ZIP Code
As an example, consider a model for an address.

public class EditModel
public Guid Id { get; set; [...]

Also tagged , ,

Unit Testing Html Helpers for ASP.NET MVC

Html helpers for ASP.NET MVC are static extension methods, which frequently reference the ViewContext and  HttpContext. Combined, this can make unit testing a bit tricky. Let’s write a new Html helper using a test-first methodology. Let’s start with a prototype function:

        public static MvcHtmlString MyTable(this HtmlHelper helper, MyModel model, IDictionary<string, object> htmlAttributes)
            return MvcHtmlString.Empty;

I’ve added just enough code here to get the prototype to compile. Now let’s write [...]

Also tagged , , , ,

Troubleshooting Entity Framework Connection Strings

In an application which uses the Entity Framework, you may see the following error at runtime:
MetadataException: Unable to load the specified metadata resource
The cause of this error is a missing or malformed Entity Framework connection string. In particular, the cause of this error is the metadata parameter of the connection string. Let’s examine this more closely.
Which Config File?
Before [...]

Also tagged ,

Replacing Controller.Session in ASP.NET MVC: Is This Wrong?

Here’s some code I wrote:

public class MyBaseController : Controller
// …
public new ISession Session { get; private set; }
// ….

ISession is an interface type I wrote which exposes everything I store in the session at runtime. I use constructor injection to assign this to [...]

Also tagged ,

Comparing C#, C++, and Delphi (Win32) Generics

C#, C++, and Delphi all have a generic type and method language feature. Although all three languages are statically typed, they implement generics in very different ways. I’m going to give a brief overview of the differences, both in terms of language features and implementation. I presume that Delphi Prism generics work essentially the same [...]

Also tagged , , , ,

NDepend Review and Tips

On and off for the past few months, I’ve been experimenting with the .NET static analysis tool NDepend. NDepend works somewhat differently than most other static analysis tools I’ve tried. Notably,

Most tools tend to work on either compiled assemblies or source code. NDepend uses both.
Most tools implement their tests in compiled, executable code. NDepend, on [...]

Also tagged , , ,

Bad Behavior has blocked 713 access attempts in the last 7 days.