posts - 76, comments - 26, trackbacks - 0

Thursday, July 22, 2010

Great Michigan .NET User Group Meeting Last Night

I’ve been to a lot of user group meetings over the years in various cities, but I think the best one I’ve ever been to was last night’s migang.org’s gathering at the Microsoft office in Southfield, MI.  Len Smith presented Javascript is Real Code:  SOLID and TDD in the Browser.  I really dug the topic and Len’s presentation, but I was impressed by the people and the group - Lots of very friendly folks with a great sense of community.  Dave Giard does a great job organizing the group and fostering a friendly, communal, ego-less vibe.

I really enjoyed being there and planned to go back often.

posted @ Thursday, July 22, 2010 7:56 AM | Feedback (1) |

Friday, July 09, 2010

Backing up with T-SQL

I never remember this syntax, so here it is for future reference:

To back with a SQL Server database with T-SQL, this is the command:

BACKUP DATABASE [YourDb] TO
DISK = N'C:\YourDb.bak' WITH STATS = 1

 

Be careful with using WITH STATS = 1 on very large databases

posted @ Friday, July 09, 2010 9:03 AM | Feedback (0) | Filed Under [ cheatsheet ]

Tuesday, June 08, 2010

VS2010 Performance

My team immediately started using Visual Studio 2010 after it’s release in April.  We were waiting early for Silverlight 4 which was a component of the release, so we switched IDE’s right away.  I like the feature set very much, but the performance is subpar and plain awful in some areas.  I am running Resharper 5.1 EAP (nightly builds).  5.0 had a number of issues and several performance problems.  I upgraded to 5.1 after reading this thread on JetBrain’s forum.

It’s hard to tell whether the performance issues are caused by the core IDE or Resharper, but there seems to be reports of performance issues in both.  Here some interesting posts on issues related to the core IDE:

It’s going to be awhile for these wrinkles to get ironed out.  My team is all-in, and patiently awaiting fixes and work around’s.  On promising lead on was posted on the previously mentioned JetBrains forum thread:

It basically states that clearing out the following VS options greatly improves scrolling performance:

image

I’ll give this a shot for a while and see if it improves things

posted @ Tuesday, June 08, 2010 6:04 AM | Feedback (0) | Filed Under [ visual studio ]

Wednesday, March 31, 2010

My take on the ViewModelLocator pattern for MVVM

John Papa and Glen Block have recently posted on a topic that touched home for me.  They put together a solution to find view models for a given view in a view-first fashion to support Blendability and a clean MVVM presentation pattern.  Funny, I put this very pattern together last week after being motivated by what I saw from Rob Eisenberg and Laurent Bugnion at MIX the week before.  I too came to the conclusion that it makes a lot of sense to go with a View-first approach if you’re going to make Blend a first class citizen in your development process (as I believe you should).

What John and Glen put together is MEF based (obviously), but what I put together is IoC based.  In my case I’m using Unity, but it could be any IoC container.  With a very simple convention, I can greatly simplify this entire pattern.  That convention is this:  The View and the ViewModel will always reside in the same namespaces within the same assembly and the View will always be suffixed with xxxxxView and the ViewModel will always be suffixed with xxxxxViewModel.  That’s it.  With that convention in place, one line of markup can be used in all views to auto-locate, instantiate, and bind to it’s corresponding ViewModel.  No need to Export anything with MEF and no need to have string names on your exports to find by.  I like my approach a lot better and my team has been very happy with it so far.  It’s very simple, straightforward and requires very little ceremony code.

Below is what the code looks like…

All views have this markup at the root element:

DataContext="{Binding RelativeSource={RelativeSource Self},  Converter={StaticResource LocatorConverter} }"

This is simply binding the View’s DataContext to itself and passing itself to the LocatorConverter.  The LocatorConverter looks like this:

    public class ViewModelLocator
    {
        public static object Locate(object view)
        {
            try
            {
                Check.ForNullArg(view, "view");

                Type viewType = view.GetType();
                string viewTypeName = viewType.Name;

                Logger.WriteInfo("Attempting to auto-resolve the view model from the view (type: {0})", viewTypeName);

                if (string.IsNullOrEmpty(viewTypeName) || !viewTypeName.EndsWith("View"))
                {
                    throw new PresentationModelException("Attempting to locate a view model instance for an invalid {0} implementation - expected the type name to be suffixed with 'View'", typeof(IView).Name);
                }

                string viewModelTypeName = string.Concat(viewTypeName, "Model");

                var viewModelTypeFullName = viewType.AssemblyQualifiedName.Replace(viewTypeName, viewModelTypeName);

                var viewModelType = Type.GetType(viewModelTypeFullName);

                if (viewModelType == null)
                {
                    throw new PresentationModelException("Could not find view model type '{0}' in namespace '{1}' within assembly '{2}'.  Auto resolution of the view model requires both the view and the view model to reside within the same namespace.", viewModelTypeName, viewType.Namespace, viewType.Assembly.FullName);
                }

                object resolved = IoC.Resolve(viewModelType);

                if (resolved == null)
                {
                    throw new PresentationModelException("Failed to resolve the view model type '{0}'", viewModelTypeFullName);
                }

                Logger.WriteInfo("Successfully auto-resolved view model (type {0})", viewModelTypeFullName);

                return resolved;
            }
            catch (Exception ex)
            {
                string viewType = view != null ? view.GetType().FullName : "NULL";

                throw new PresentationModelException(ex, "The attempt to auto-instantiate the view model for the view '{0}' threw an exception: {1}.  See the inner exception for details.", viewType, ex.Message);
            }
        }



IoC is a very think shim around Unity that’s setup by our application’s Bootstrapping process:

        public static object Resolve(Type type)
        {
            return _container.Resolve(type);
        }

I love MEF, but this doesn’t feel like a good fit for MEF to me – at least not in it’s current form.

posted @ Wednesday, March 31, 2010 9:51 PM | Feedback (0) | Filed Under [ silverlight ]

Tuesday, March 23, 2010

Handy Serialization Extension Method, Clean code with Action<T>

Here’s a handy Extension Method in C# that attaches a “ToXml()” method to any and all object instances.  I added the capability to dictate whether or not to use the classic XmlSerializer or the DataContractSerializer that came to us with WCF.  I made the XmlSerializer the default because typically, when I’m doing this programmatically in my code, it’s intended for human eyes and the XmlSerializer is far more readable, but the DataContractSerializer is what’s actually used at runtime by WCF.  So, call the overload the matters to you.

        public static string ToXml<T>(this T obj) where T : class
        {
            return ToXml(obj, false);
        }

        public static string ToXml<T>(this T obj, bool useDataContractSerializer) where T : class
        {
            if (obj != null)
            {
                Type type = typeof (T);

                Action<XmlWriter, object> action;
                
                if (useDataContractSerializer)
                {
                    action = new DataContractSerializer(type).WriteObject;
                }
                else
                {
                    action = new XmlSerializer(type).Serialize;
                }


                using (MemoryStream stream = new MemoryStream())
                {
                    XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };
                    XmlWriter writer = XmlWriter.Create(stream, settings);

                    if (writer != null)
                    {
                        byte[] bytes;

                        using (writer)
                        {
                            action(writer, obj);
                            writer.Flush();
                            stream.Flush();

                            bytes = stream.ToArray();
                        }

                        return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    }
                }
            }

            return null;
        }

 

This is a good example of C# generics using Action<T>.  Notice I’m assigning the variable “action” to either the XmlSerializer’s Serialize() method, or the DataContractSerializer’s WriteObject() – which both have a method overload of void (XmlWriter writer, object obj).  I really like this syntax as it provides yet another way to remove duplication from your code without requiring the types to share a base class or interface, and without having to create a full blown Template Method pattern implementation.

And it works in Silverlight too :)

posted @ Tuesday, March 23, 2010 11:44 PM | Feedback (0) | Filed Under [ silverlight ]

Sunday, February 14, 2010

Cleaning up after a rough week with Oracle

I had a pretty rough week attempting to get the Oracle client installed on my Windows 7 – 64bit machine.  I’m going resist the temptation to throw stones, but let’s just say it was a frustrating week.

Today I went to the Server Explorer in Visual Studio 2008 to do some work with the ADO.NET Entity Framework.  When attempting to connect the designer to my local SQL Server, I immediately received this

An unexpected error occurred in the Oracle Data Provider for .NET.  Please contact the provider vendor to resolve this problem.

image

Siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiigh

Even though I’ve uninstalled every trace of Oracle from my machine from the Add/Remove Programs dialog, Oracle seems to have failed to cleanup after itself and it’s still left some damaging residue in my registry.

It took a while and some googling to figure this out, but the solution is to find all occurrences of the following GUID in your Windows registry and delete each one.  I found about 25 of these.  After deleting them all and restarting Visual Studio, it looks like I’m back in business.

D601BB95-E404-4A8E-9F24-5C1A462426CE

posted @ Sunday, February 14, 2010 5:18 PM | Feedback (3) | Filed Under [ visual studio ]

Tuesday, February 09, 2010

Restoring Base Image of my Dell Latitude E6400 from my WHS and NIC driver pangs

My company issued me a new Dell Latitude E6400.  It’s a fine machine, and I like it very much.  Yesterday our system tech hands me the machine brand spanking new with our base company image on it (Windows 7 Enterprise – 64 bit).  My plan was take the image on this machine and copy it to the solid state hard drive that I have in my personal laptop.  I don’t need this Lenovo T61, but it’s got a blazing fast SSD in it and I want to use that – not this old school SATA thing that came w/ my dell.  So my plan was this:

  • Get the machine completely up to date in terms of Windows Updates
  • Install all of the common stuff that I require on all machine.  Basically, all of this stuff, minus the developer tools – this is what I call a “base image” for myself
  • Back up this “base image” to my Windows Home Server – this should give me a restore-able snapshot in time that essentially hardware agnostic.
  • Remove the solid state hard drive from my T61 and install it as my main hard drive on my E6400
  • Restore the base image onto this solid state drive and… presto – new machine running on my solid state hard drive and using my “base image”
  • Install all of my other software, take another backup at that point, and then happily go on about my life

For those of you who are not familiar with Windows Home Server, the above scenario is not magic.  This is a pretty common thing – I’ve done this many times before.

The problem I had today was I could not for the life of me getting the WHS Restore Disk to recognize my network adapter.  I’ve never had this issue before with other machines I’ve restored.  I simply booted up w/ the restore disk, it found the hardware it needed (essentially the NIC and your hard drive(s)) and it just worked.  This time, when I reached the “Detect Hardware” screen, the WHS Restore software couldn’t find my network adapter.  Without connectivity, there’s no way to restore from the WHS.  Luckily, the Restore software gives you the option to insert a USB Flash Drive or Floppy at the Detect Hardware screen.  It will let you provide drivers if you have them.  That’s nice, but you have to find the right drives that will work w/ the Restore software.  I spent several hours last night working on this with no success.  This morning I found the solution.  But first, the things I tried that didn’t work:

  • I put the original hard drive (issued from my company) back in, and cherry picked the drives from C:\Windows, copied them to a USB Flash Drive, rebooted into the Restore software, and let it scan the flash drive.  Result:  Still couldn’t find the NIC
  • With WHS you can “open” an image.  In other words, when I backed up my Dell in bullet 3 above, WHS will let me open this open and copy files from it.  This is a pretty wicked feature.  It essentially creates a mapped drive on the PC you’re using to talk to the WHS, and lets you copy from it.  In my case, it looked like this:
    image 
    So, following the advice in this forum, I tried copying the drivers from this image (because I know those drivers worked w/ this hardware before I got into all this restore stuff).  I did the same copy-to-flash-drive-then-reboot-then-scan-for-drivers dance, the result:  Still couldn’t find the NIC

I gave up for the night.  After a good night’s sleep and mentally shifted back to the basics.  I know the NIC works when running under Window 7 64bit Enterprise.  What’s different now?  Well, it’s not Window 7 64bit Enterprise that’s trying access the NIC, is it?  This is the WHS Restore software.  Ok, what is that?  It’s obviously some tiny little version of Windows.  Then it dawns on me, I’m barking up the wrong tree.  This probably has nothing to do with 64 bit.  I highly doubt this tiny little version of Windows is 64 bit.  It’s most likely a little version of Windows XP if I had to guess.  The Restore disk doesn’t have default drivers for my hardware.  That’s a shame, but it happens from time to time.  And, I’ve been trying to feed this this thing 64 bit drivers, which it doesn’t like.  This forum I referenced early eluded to this a bit by advising to copy both System32 (32 bit drivers) and SysWOW64 (64 bit drivers).  For whatever reason, it didn’t like those drives (32 bit or 64 bit).  I’m guessing it didn’t like them because these are the Windows 7 versions of the drivers.

Here’s what I did to fix this.  According to Dell, my NIC is a Intel 825xx Gigabit Platform LAN Network Device.  I googled around for drivers, and the normal shady sites came up. Then I realized that Dell.com had versions of these drivers for both Win 7 and Win XP/Vista and 32 and 64 bit versions of each.  This supported my theory and made good common sense.  So, I did the following:

I rebooted into the WHS Restore software, let is scan my Flash Drive, and it found one of these drivers!  After loading these drivers it was on my network and it could find my home server.  I’m not sure which driver it ended up loading, but it found one it like and I was able to continue with the restore wizard.  Within 13 minutes, my base image was restored to my solid state hard drive and my company laptop was ready for business.  The problem is solved, but now I’m late for work… gotta get in the shower!

I hope this helps somebody else out there

posted @ Tuesday, February 09, 2010 7:49 AM | Feedback (2) | Filed Under [ tools windows home server ]

Sunday, February 07, 2010

Using Fiddler - No connection could be made because the target machine actively refused it

Fiddler is a great tool for inspecting HTTP traffic.  If you’re building a web services based application or AJAX intensive web application, you’ll need to check this out.  Fiddler has been around for a long time and there’s a lot of good content out on the net about how to use this.  There are several gotchas with this tool – like how it behaves with local addresses (i.e. localhost, 127.0.0.1, etc.).  Be sure to read up on all of that if you’re new to Fiddler.

One lesson I learned today while playing with the latest CTP of ADO.NET Data Services, is disable IPv6 in the Fiddler options.  Having this enabled seems to result in “Connection to localhost failed. Exception Text:  No connection could be made because the target machine actively refused it” errors for local addresses.  Like so:

image

image

Disable IPv6 in the Fiddler options seems to solve this.

image

Looks good now:

image

Hope this helps someone else out there

posted @ Sunday, February 07, 2010 9:58 AM | Feedback (1) | Filed Under [ tools ]

Wednesday, February 03, 2010

Please Vote: ChannelFactory Support in Silverlight

http://silverlight.codeplex.com/WorkItem/View.aspx?WorkItemId=5008&ProjectName=Silverlight

posted @ Wednesday, February 03, 2010 2:05 PM | Feedback (0) |

Monday, February 01, 2010

Deploying ASP.NET MVC Apps to Shared Hosting Providers – Webhost4life.com

Are you getting 404’s on your deployed site when it works perfectly on your development machine?

There’s a number of hidden gotchas when deploying an ASP.NET MVC application to a shared hosting providers.  My provider of choice is Webhost4life.com.  Here’s a list of issues I ran into and the solutions for each.

IIS 6.0 vs IIS 7.0

There’s a number of issues you need to be aware of here.  If you using IIS 6 -- and if you’re on Webhost4life there’s a good chance you are – then you need to make sure you’re virtual directory is configured w/ the “Wildcard application map” ISAPI setup that Phil talks about in his ASP.NET MVC on IIS 6 Walkthrough post.  The short answer is – you have to contact Webhost4life to do this.  As of this writing, this is not available through their Control Panel.

MVC Binaries

As of this writing, Webhost4life.com does not have ASP.NET MVC installed on their servers and therefore none of the assemblies are installed in the GAC.  At a minimum, you need to bin deploy System.Web.Mvc.dll assembly.  Follow the instructions from Phil on how to do this in his post Bin Deploying ASP.NET MVC – the abbreviated version is:  mark the references to MVC assemblies in your project as CopyLocal=true.

Route Setup

Make sure that your routes our setup in a way that jives with your domain.  In other words, make sure that http://www.yourdomain.com/ will go to the proper route.  This is easy to overlook since your Startup Page in Visual Studio is set to something like /Site/Home while debugging. However, this isn’t how people are going to navigate to your site on your hosting provider.  In my case I setup routes with the code below.  This allows folks coming into my site via http://yourdomain.com and http://yourdomain.com/Site/Home to all end up in the same place

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("Default", "{controller}/{action}/{id}",  new { controller = "Site", action = "Home", id = "" });
            routes.MapRoute("Root", "", new { controller = "Site", action = "Home", id = "" } );
        }

        protected void Application_Start()
        {
            Global.RuntimeSettings = new RuntimeSettings {MachineName = base.Context.Server.MachineName};
            RegisterRoutes(RouteTable.Routes);
        }
    }

Link Round Up

Here’s a list of things I found helpful when debugging this stuff

posted @ Monday, February 01, 2010 6:18 AM | Feedback (0) | Filed Under [ .net ]

Powered by: