A Blog by Scott Isaacs

Tag: Code

NuGet and Lowering .NET Framework Version

Background

Today I was about to deploy an app to the server when I realized I had been building against .NET 4.5 while the server only had .NET 4.0 on it.  

Not a big deal, right?  I just needed to change the “Target Framework” in the project properties.  So I did, and it wouldn’t build.  Turns out I had some NuGet references that were targetting .NET 4.5.

Again, no big deal.  Remove EF 5.0 and add EF 4.4 (which is conspicuously named EF 5).  My build succeeds and gets deployed to the development server.

But it won’t run.  I dig around and find other NuGet references (specifically, Newtonsoft.Json) that also are targetting the wrong framework version.  I uninstall and re-install it (and Web API Client and HttpClient).

The Error

But at this point, my solution doesn’t even build.  I kept getting an error message:

‘System.Net.Http.HttpContent’ does not contain a definition for ‘ReadAsAsync’ and no extension method ‘ReadAsAsync’ accepting a first argument of type ‘System.Net.Http.HttpContent’ could be found (are you missing a using directive or an assembly reference?)

But I have a reference to System.Net.Http.Formatting where that extension method is defined.  In fact, when I use Visual Studio’s Go To Definition function, it takes me right to the definition.  At this point I’m perplexed.  So I do what any other developer would do.  Repeatedly remove and re-add the NuGet references, trying to figure out why it won’t build.

The Fix

All in all, I probably wasted almost three hours on this today before I found the fix.  I had to comment out the line of code that had errors and build the solution.  Once I had a solution that successfully built, I uncommented the line and the solution built successfully.

I don’t know why this fixed the issue.  I could spend more time trying to figure out why, but frankly, at this point, I don’t care.  I’m just glad I got my app deployed to the dev and test servers today and that I finally get to work on the project I planned to start last week.

Beginning Windows 8 Development (book)

You may or may not know that I am currently writing a book.  If you didn’t, now you do.  That’s a picture of the cover.

It’s a book about writing software for Windows 8 using HTML and JavaScript.  The first few chapters are heavily based on a similar book which introduces building Windows 8 applications with C#.  I’m really excited about it, and it should be available by the end of summer.  I’m not done yet, and I have a fair amount more work to go, but it’s getting there.  

A few weeks ago, the publisher, Apress, made an alpha version of the book available on their site.  Buying the alpha version gives you access to draft versions of the chapters I am writing as I submit them, even before the editing process has been completed.  Then I found out earlier this week that you can also pre-order the book on Amazon.  I thought that was pretty cool, and it’s kind of a boost to the ego to see that, not to mention adding motivation to complete the process.

Imagine my surprise today, though, to come across this advertisement “in the wild” when checking the weekend weather.

Visual Studio Command Prompt Here

I’m rebuilding my computer after a hard drive failure, and I keep running into issues where things I’ve taken for granted are missing – things such as the "VS Command Prompt Here" Windows Explorer context menu items.

Years ago, Scott Hanselman posted INF files to add VS 2005 to the context menu. Using that as inspiration, I’ve created a version of the file for VS 2008 and another for VS 2010. For what it’s worth, I tweaked his VS 2005 file because my copy of VS is installed in "Program Files (x86)" and on older versions of Windows, such as when he posted, it wasn’t.

So enough with the intro; here are the download links:

Right-click to save your file of choice, then right-click on the saved file and choose Install.

Disclaimer-ish stuff: These modify your registry. You should open the files in Notepad first to understand what is happening. Use at your own risk. I’m sure you’re not reading this part anyway.

Good luck.

Curly Brace Placement

I’ve been writing code with curly braces for a dozen years or so, starting with C++ in college, then JavaScript for many, many years, and for the last few years, C#.  In all that time, I have been a fan of putting the opening curly brace on the same line as the declaration or control statement.  For example:

private void DoSomething() {    // ...    if (somecondition) {        myval = true;    }    // ...}

At work, though, I have been working on someone else’s code, and I always try as much as possible change my coding style to match the existing code.  In this case, that means opening curly braces get their own line, like so:

private void DoSomething() {    // ...    if (somecondition)     {        myval = true;    }    // ...}

I find that I’m actually starting to prefer the "new line" option.  But I don’t want to prefer it!  I guess maybe I’m stubborn.

I agree that it is easier to visually match opening and closing braces this way, but VS2008 does that for me anyway with clever highlighting.  Also, I don’t like how much extra room is taken in my editor window.  I like whitespace, but this seems to put it in the wrong place for me.

Sigh.  What’s a poor developer to do?

What’s your preference and why?

Testing PreCode

I’m using this post to test PreCode — a code snippet formatting plugin for Windows Live Writer.  It uses SyntaxHighlighter on the client side and this plugin just sends the needed code to my blog engine.

As a test I thought I’d just add a couple short snippets, using some of the different available options.

//C#, using PREbool b = true;

' VB, using PRE, Starting at line #20Dim i as Int32i = 1i += 3MessageBox.Show(i)

Note that I may update this post several times while trying different options, or I may delete it altogether if I decide I don’t want to bother with the plugin.  (I think if I do delete it, it will be the only post I ever deleted.)

I would appreciate it if you would take a look at this post both in your RSS reader as well as on my website in your browser of choice and let me know what you think, my dear guinea pigs.

Powered by WordPress & Theme by Anders Norén