Thursday, August 8, 2013

Web Essentials Goodness - Paste XML/JSON As Classes

I recently have been working on project where I have been evaluating some vendor APIs. Across the multiple vendors the responses have been in either XML or JSON. Since I am most familiar with .NET development, I have been creating my testing clients in C# after verifying the responses from the API with Fiddler. So in many cases once I have seen the XML/JSON output, I want to be able to download that data into my application as a class. Thankfully, the Web Essentials Visual Studio extension by Mads Kristensen, includes two excellent options. Paste XML As Classes and Paste JSON As Classes. With these two options ( from the File->Paste Special menu) when you have a class open you can paste any JSON or XML object and the extension will generate a class that corresponds to the document for you. In some cases (especially with XML) you may need to perform some additional clean up of local variable names, etc. But in the long run this has saved me lots of keystrokes. Below are a couple of examples.

JSON Object
Corresponding Class created with Paste JSON as Classes

XML Object

Corresponding Class created with Paste XML as Classes

As I stated earlier, the variable names in the classes created for this XML object could use a little clean up, but with a good refactoring tool, this should not be a difficult task and something that I find much more enjoyable than creating the entire file by hand.

Hope that you find this useful and that it can save you some time.

Tuesday, October 9, 2012

Web Api - Ninject & IHttpControllerActivator

I was reading through the following blog posts today by Mike Seemann:
  • Dependency Injection and Lifetime Management with ASP.NET Web Api
  • Dependency Injection in ASP.NET Web Api with Castle Windsor
In his posts, Mike shows how to use the IHttpControllerActivator to wire up Dependency Injection (DI) without using the built in IDependencyResolver in the Web Api bite. After reading through both of these, I was inspired to create the Ninject based equivalent DI leveraging the IHttpControllerActivator.

Here is the class that I created for this:

A complete sample application is available from https://github.com/paigecook/WebApiNinjectIHttpControllerActivator

Wednesday, September 19, 2012

ELMAH & ASP.NET MVC

If you are planning to use ELMAH - Error Logging Modules and Handlers with your MVC application , I would like to recommend the following resources to assist you.

  • Logging Errors with ELMAH and MVC 3 - This is a great 5 part walk through of how to setup Elmah within your MVC 3 (and MVC 4) application and even includes a great way to log JavaScript errors as well. It covers the following:
    • Setup
    • Notifications
    • Filtering
    • HandleErrorAttribute
    • JavaScript
  • Elmah.MVC NuGet Package - "for painless integration of ELMAH into ASP.NET MVC". Provides the following:
    • Custom route and Handler (uses /elmah by default instead of /elmah.axd), but you can specify your own  as well.
    • Prebuilt HandleErrorAttribute
    • Supports both C# & VB.NET applications
    • Configurable authorization rules
So, if you are using ELMAH with ASP.NET MVC, please do yourself and favor and checkout these resources to make using ELMAH easier and better. And if you are not currently using ELMAH, I would urge to consider using it, as it provides easy and simple error logging for your MVC or WebForms ASP.NET applications. 

Wednesday, September 12, 2012

VS 2012 Color Theme Editor Extension & ReSharper 7.0

I just installed the excellent Visual Studio Extension - VS 2012 Color Theme Editor. This is the newly updated version that is compatible with VS 2012. I was running this on VS 2010 and loved being able to use some of the built in or create my own custom color theme. One of my favorite improvements with the VS 2012 version is the ability to mix the editor/IDE experience of the built in Light & Dark themes in VS 2012. Two of the available options in the Color Theme Editor are "Light with Dark Editor" and "Dark with Light Editor". I have chosen the "Light with Dark Editor" option and have been loving it so far.

One thing to be careful with right now however, is that there is an issue with the Theme menu that is added to the VS menu bar with this extension if you also have ReSharper 7.0 installed. The menu should show you all of the available built-in Color Themes and allow you select one, however, that list is not displaying properly when ReSharper 7.0 is installed. However, there is a workaround -- You can still access and select the themes by selecting Tools->Options->Environment->General and these new color themes are listed in the drop down menu there.Also the Editor behavior is a little off as well and it cannot properly show the available themes to allow you copy one of those as your starting point. Hopefully the author will work with JetBrains to figure out the issues and get this fixed before too long.

Update: This issue has been corrected with the release of version 2.1 of the extension. If you are running ReSharper, please make sure to update to this version or a later release.

Wednesday, August 29, 2012

WordPress on IIS 7

Today I installed WordPress on IIS 7.X (Windows 2008 R2 Server). The initial setup was very easy thanks to the use of the Web Platform Installer (WebPI) I selected the following options:

Products

  • PHP 5.3.10
  • MySQL Windows 5.1
Applications
  • WordPress
It was very simple to setup and within about 10-15 minutes (including) download times, I had a WordPress site running with IIS 7.X on my Windows 2008 R2 server.

One thing of  note was that WordPress wanted to update to a later release and when I clicked the Update button, it kept failing with an error of being unable to unzip the update. After some searching I found this article - WordPress and Plugin Updates on IIS7 Fail a Lot that walked me through the steps to get the update working by modifying permissions for the IUSR and Network Service accounts on my server.

Wednesday, June 13, 2012

RegEx: Remove Line Numbers

Found this Regex to replace any line starting with one or more numbers followed by colon -
\b[0-9]*:
I used this today for removing line numbers from code a snippet I copied off of a web page.

Thursday, May 31, 2012

NuGet Package Restore Tips

I recently have started using the NuGet Package Restore option in all of my .NET projects. This is a great new feature that was enabled with NuGet version 1.6. I followed this blog post Cleaning Up Your Git Repository for NuGet 1.6 and saw my repository size shrink drastically, a very good thing.

However, I noticed that when I enabled Package Restore that Git was excluding my NuGet.exe file in the solution .nuget folder. This was based on my having a *.exe entry in my .gitignore file. I was able to manually add the file by issuing the command git add -F NuGet.exe from the git command line. This worked but I wanted to find a better way. I tried a few different entries in the .gitignore file unsuccessfully. Then today, I had this issue again and did some more research and found this Answer on StackOverflow about putting an additional .gitignore file in the .nuget folder and explicitly including all executables via a !*.exe line. This worked great.

Friday, May 18, 2012

How to get Application Version

Ever need to the the version of your application? For example showing the version of a executable (a console application, for example). Then you can do that with a single line of code

Assembly.GetExecutingAssembly().GetName().Version

Please note that Version is of the type System.Version that contains properties for the major and minor revisions and others if needed.

Thursday, May 10, 2012

IIS Application Pool Identity access to SQL Server

I recently had a need to connect to a SQL Server Express 2008 R2 instance using the 'Integrated Security=SSPI" option in my connectionString setting. Of course when I ran the site the first time, I got an error stating that IIS APPPOOL\{MyApplicationPoolName} could not access the database. So after a little searching, I found this post ApplicationPoolIdentity IIS 7.5 to SQL Server 2008 R2 not working. on the IIS Forums that provided the proper steps to grant the Application Pool Identity access to the database.


The necessary steps from the post are below for reference.


In SQL Server Management Studio ...
    • connect to your SQL Server and navigate to Security - Logins in Object Explorer.
    • Add a new login:
      • Create a Windows login with the name of your application pool. Use the virtual domain name "IIS AppPool", like: "IIS AppPool\{YourApplicationPoolName}"

        (Dont' forget to use the real application pool name after the backslash, e.g.: "IIS AppPool\DefaultAppPool")
      • Assign the new login to your database(s) in the User Mapping page of the New Login dialog.
        Give it any user name you find appropriate.
      • Save and close the New Login dialog.
    • In Object Explorer ...
      • Navigate to your database, then navigate to Security - Users
      • Navigate to your new user and set permissions according to your needs.

Monday, April 30, 2012

Solr 3.6

I am excited to see the latest improvements that are now available in Solr 3.6. Please see the following links for more details about the latest release:


For me the I am most looking forward to the improved HtmlStripCharFilterFactory and improvements to the FST based Suggesters.

Monday, January 23, 2012

TFS Compare Tool

The very first thing you should do before starting to use the TFS explorer, is to replace the Diff/Compare Tool. The one that comes with Visual Studio is pretty bad. Here is a good blog post - Replacing the TFS Diff Tool that shows how to quickly setup a new tool.

I prefer to use SourceGear DiffMerge, but you can use whatever you like. Just, please do yourself a favor and change it to something other than the default.

Wednesday, August 17, 2011

Following Open Source via RSS

I have recently been working on an ASP.NET MVC project, where I have included many Open Source projects. Most of them were included using NuGet (a great tool by the way, you should get it if you don't already have it!). So, for a lot of these open source projects that I an now using, I like to keep track of what changes are happening in the projects. A great way that I have found to do this, is to track the commits to the projects via RSS. I have subscribed to the RSS feed of the commits for the projects in Google Reader and then grouped them into an OpenSource folder that I can monitor. For me, this is a really easy way to see what is happening on a project and to watch out for interesting updates.

Tuesday, June 14, 2011

My Git Ignore File

I am using Git more and more these days and I really love how easy it is create branches and switch between them. If you have not tried out Git before, I really would encourage you to do so. Here is my current .gitignore file. This file basically tells Git what files to exclude when looking at a repository for changes, etc.

Tuesday, May 31, 2011

Entity Framework Serialization - Circular Reference

I have been working with KnockoutJS recently and needed to serialize an Entity Framework model to the client as JavaScript so that I could convert it to my Knockout ViewModel. Here is the entity that I needed to serialize.



However, when I attempted to serialize this object, I was getting an error "A circular reference was detected while serializing and object of type Person". I did a little research on this issue and found a good blog post by Rick Strahl on LINQ to SQL and Serialization, that discusses this issue.

The problem is the public Tags property. The first suggestion is to change the access modifier from public to internal. I did this and it worked great. I have not fully explored the ramifications of making this change to the rest of my application, but will post an update if I find anything.

Monday, April 25, 2011

Synergy 1.4.2 with Mac as Server and Windows 7 Client

I was setting up Synergy today so that I could control my Windows 7 laptop from my Mac. So I went out and installed the latest version of Synergy - 1.4.2 and I followed the great installation guide to get everything setup. Note that I also am using QSynergy to get the GUI on the Mac. I have my Synergy setup with my Mac as the server and Windows 7 as a client. Once I had everything set, I was able to move my mouse over to the Windows 7 machine, but the keyboard was not working. So I did some googling and came across issue #57 on the Synergy Support forums that describes this issue. According to this, it looks like the official fix for this will not be ready until version 1.4.4 of Synergy is released. However, Matthew Toso posted the 1.4.2 version of synergys with a patch applied for this issue. See note #82 in this thread. Once I installed the patched synergys on my Mac, the keyboard was working properly when I was over on my Windows 7 machine.

Tuesday, March 29, 2011

Entity Framework 4.1 Entry Extensions

I have been working with the Microsoft Entity Framework version 4.1 aka "Code First" for about the last 3 weeks and have been really enjoying the ease and flexibility of setting things up and getting my CRUD operation working so smoothly. However, I had the need to create modify some entities in my database and was starting down the path of passing in the new entity, then querying the context for the old entity and finally setting the values of the old entity to those of the new entity. This worked for a little while, when I only had simple entities. However, my entities started getting more complex and I found myself adding more properties and always forgetting to update the modify operation.





So I started doing some research and found that I could use the Attach method on the DbSet to add the modified entity. However, this left the entity in the UnChanged state and that did not really do me any good. So with a little more research I found this Blog Post by Julie Lerman about Round Tripping a Timestamp Field in EF 4.1 with MVC 3 that discusses the DbContext.Entry method and how I can set the State as I attach the entity.



I had also in my secondary research found this post about creating extensions for the ObjectContext in the regular Entity Framework and this led to the creation of my own Extensions for the DbContext object as seen below...



I now use these extension methods as shown below.

Monday, March 21, 2011

Good Drupal Books

Had the following Drupal books recommended today...















I have read Pro Drupal Development and it was an excellent book that helped me get up to speed quickly on developing modules for Drupal. I would highly recommend this book. I have not read the others, but plan on checking them out soon.

Drupal, MAMP & Drush

I am attending a Lullabot.com Drupal API &Module Development course at work this week. Learning a lot of great thoings so far. I worked on a Drupal project last year and created a custom module, but what I have learned so far in one day, could have probably saved me a close to a week as I was trying to figure it out on my own.

At the end of class today, the instructors were going over some tips for Drupal developers and showed off the awesome Drush, Drupal Shell. It is amazing what this tool can do, and being able to have the power of the command line to very nice. However, since I am running Drupal on MAMP, I was getting an error about not having the correct amount of RAM configured. However, I found this "getting drush to work on MAMP setup" article that had the key that worked for me. Create a .bash_profile file and adding an alias:


alias drush='/Applications/MAMP/bin/php5/bin/php /usr/bin/drush/drush.php'
export COLUMNS

Friday, March 18, 2011

ASP.NET MVC & MvcBuildViews set to true causes Build Error

I recently set the property in my ASP.NET MVC web application for the Release mode as recommended by K. Scott Allen in Notes on Building Razor Views. This was working fine for a few days. Then yesterday, I started getting this error every time I would try a Release build.

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

I went back to the comments from the blog post and found this one, where someone else was having the same issue and they solved the issue by deleting the obj\ folder, as recommended by the StackOverflow MVC BuildViews not working correctly question. I tried this and unfortunately it did not resolve my issue. Another answer further down suggested changing the PhysicalPath value for the AspNetCompiler value in the BuildViews task. I tried the recommended path:



That also did not work for me, so I followed a link to the use of Web Deployment Projects with ASP.NET MVC and noticed that they were using the following PhysicalPath setting.



I changed my setting to this value, $(ProjectDir)\..\Web created this directory and now things are working properly again.

Monday, March 7, 2011

Flush DNS on a Mac

So there were some network changes over the weekend at work and I needed to flush my DNS entries, so I did a quick Google search and found this great reference for flushing DNS on almost any system.

Specifically for  Mac OSX Leopard you issue the following command:

dscacheutil -flushcache