Demo from my MVC/EF/jQuery Talk

by ely August 26, 2010 16:45

Sorry this is a few days late, but I have finally uploaded my demo from my talk at last Monday’s Denver Visual Studio.Net User Group

If you were there, thanks for coming out and seeing our community launch event for Visual Studio 2010.  We had a packed house of 100+ people, and had a great time.

In order to run this demo, you will need the Entity Framework Code First CTP4 binaries.  See Scott Guthrie’s post about EF4 code first and info on how to download it

Tags:

.Net | jQuery | VS2010 | mvc | entity framework

Using custom dynamic object with C# 4.0 for even more flexible parameter passing

by ely August 10, 2010 09:10

A few days back, Rob Conery wrote a post comparing C# and Ruby, and how using the dynamic keyword in C# lets you pass in arguments into a C# method in a way similar that it is done in Ruby land.  I’m not a Rubyist, but the same ideas are in JavaScript as well.  I have worked on .Net applications that take in a dictionary parameter of named value pairs, and sometimes it has been a bit of a bear to maintain, but I agree with Rob that it can be very flexible, especially if you don’t have your API nailed down quite yet and you are constantly changing your method signature.

Rob gives an example of using the dynamic keyword in C# to pass in arguments like so:

  1: DoStuff(new { Message = "Hello Monkey" });
  2: 
  3: static void DoStuff(dynamic args) {
  4:     Console.WriteLine(args.Message);
  5: }

However, I quickly saw a problem with this method, and that is that all the arguments used inside of the DoStuff method are now required to be defined in the dynamic object being passed in.  So if I had a DoStuff method like so:

  1: static void DoStuff(dynamic args) { 
  2:     Console.WriteLine(args.Message); 
  3:     Console.WriteLine(args.Name);
  4: }

This code would crash at runtime unless you passed in the Name as well.  In JavaScript, we can check to see if a value has been defined on an object like so:

  1: if(obj.Name){
  2:     //do something with obj.Name
  3: }
  4: else {
  5:     //Ignore because obj.Name is not defined.
  6: }

It would be really helpful if we could do the same thing in C#, so I set out on a mission to try to see if this was possible.  After much digging on the web (and many thanks to this excellent articlefrom Anoop Madhusudanan) I found a method that gets close, but not quite there.  Instead of passing in the argument to the if statement, we pass in a Has(Member Name) property.  For instance, if we wanted to check to see if a Name property was on our dynamic object, we call if(obj.HasName), and if we were looking for age if(obj.HasAge), etc..

We accomplish this by creating a new class called DynamicParam that derives from DynamicObject, and overload TryGetMember, TrySetMember, and TryInvokeMember.  A dictionary member variable keeps track of all the dynamic members we have defined, as well as stores the values for these members.  Elsewhere, I have read that internally, this is how the ExpandoObject works.

I’m not saying this is necessarily a good practice, but it is interesting use of using the dynamic features of C#, and you might find it useful in your application.  The full source for the sample console app is below, and you can find it on gist here http://gist.github.com/517890.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Dynamic;
  6: 
  7: namespace ConsoleApplication2
  8: {
  9:     class Program
 10:     {
 11:         static void Main(string[] args)
 12:         {
 13:             dynamic param = new DynamicParam();
 14:             param.Message = "butt";
 15:             param.SayHi = new Action<string> (s => Console.WriteLine(s));
 16:             
 17:             DoStuff(param);
 18:             Console.ReadLine();
 19:         }
 20: 
 21:         static void DoStuff(dynamic args)
 22:         {
 23:             Console.WriteLine(args.Message);  //This param is required since we don't check if exists or not
 24:             if(args.HasTitle)  //Check to see if args Has a Title param, since we don't this statement doesn't execute
 25:                 Console.WriteLine(args.Title);
 26:             if(args.HasSayHi) //Check to see if args Has a SayHi method
 27:                 args.SayHi("h22i");
 28:         }
 29:     }
 30: 
 31:     class DynamicParam : DynamicObject
 32:     {
 33:         private Dictionary<string, object> _members = new Dictionary<string, object>();
 34: 
 35:         public override bool TryGetMember(GetMemberBinder binder, out object result)
 36:         {
 37:             if (binder.Name.StartsWith("Has"))
 38:             {
 39:                 var name = binder.Name.Substring(3);
 40:                 if (_members.ContainsKey(name))
 41:                     result = true;
 42:                 else
 43:                     result = false;
 44:                 return true;
 45:             }
 46:             else
 47:             {
 48:                 if (_members.ContainsKey(binder.Name))
 49:                 {
 50:                     result = _members[binder.Name];
 51:                     return true;
 52:                 }
 53:                 else
 54:                 {
 55:                     result = false;
 56:                     return true;
 57:                 }
 58:             }
 59:         }
 60: 
 61:         public override bool TrySetMember(SetMemberBinder binder, object value)
 62:         {
 63:             if (!_members.ContainsKey(binder.Name))
 64:                 _members.Add(binder.Name, value);
 65:             else
 66:                 _members[binder.Name] = value;
 67:             return true;  
 68:         }
 69:     }
 70: }
 71: 

Tags:

.Net | JavaScript | c#

Need to Reinstall VS2010 MVC Tools?

by ely March 19, 2010 18:36

Last week, MVC 2 was released, and like many ecstatic developers, I grabbed the bits right away and downloaded them.  First, however, I uninstalled all the MVC2 RC bits I had, including the tools for VS2008 and VS2010.  Then I installed RTM, and… came to realize that this release was for VS2008 only.  Ok, no biggie, I can wait till VS2010 RTMs to use the final MVC2 bits, so I uninstalled RTM and reinstalled RC.  However, the bits that includes all the templates and tooling for VS2010 is not included in the RC release, only the tools.  Phil Haack had a post about it here

So I needed to get the tools reinstalled.  I knew I could probably uninstall VS2010 and reinstall, but I wanted to avoid that if I could.  Fortunately, in the comment section from the above blog post, there was a quick solution that worked.  Simply run <dvd>\WCU\ASPNETMVC\VS2010ToolsMVC2.msi, and the tools will be reinstalled.  Yay for saving time.

Tags:

.Net

Visual Studio 2010 Not Starting with an “The Application Cannot Start” error?

by ely December 08, 2009 12:19

I started getting this error out of the blue trying to startup VS 2010 tonight.  A quick bingle search lead me to the Microsoft Connect site where somebody has already filed it as a bug, and a quick workaround to get VS to start again was to run “devenv /resetuserdata” in the C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE directory.  The problem seems to stem from having a corrupted profile (at least it did for me), and running that command fixed the problem. 

If you are having this issue, give this a try, and head over to the Connect site and vote up the bug to get fixed.

Tags:

.Net | VS2010

Demos for jQuery For ASP.Net Developers Talk

by ely October 23, 2009 12:03

Here is the demos that I will be using for my Talk tonight on jQuery.

Download

Tags:

.Net | jQuery

Installing Visual Studio 2008 RTM - Office 2007 Beta Software

by ely November 29, 2007 05:11

So Visual Studio 2008 RTM was released last week.  I'm a bit behind the game and just downloaded it from MSDN.  After the long download, I burnt the ISO to disk and began the install, and what happened?  Installation failure.  I went and did some googling and it seems there are a lot of people having installation problems with RTM.  I followed some of the suggestions I found and nothing seemed to work for me.  So, I had to go and do the research myself in order to get this thing installed.

My installation was failing on the Microsoft Visual Studio Web Authoring Component, which is the third item installed if you do the standard installation (after the .Net framework 3.5 and Microsoft Document Explorer 2008).  I went into the D:\WCU\WebDesignerCore folder and used winzip to extract WebDesignerCore.EXE into another folder, then I attempted to do the installation of the Web Authoring Components manually to see what happened.  I received the following error:

"Setup is unable to proceed due to the following error(s): The 2007 Microsoft Office system does not support upgrading from a prerelease version of the 2007 Microsoft Office system. You must first uninstall any prerelease versions of the 2007 Microsoft Office system products and associates technologies."

Which was a bit strange to me since I am still on Office 2003 and haven't installed any Office 2007 betas to my knowledge.  I dove back into my Add Remove Programs to make sure I didn't have any betas hanging around.  What I did find was that I had the Compatablity Pack for the 2007 Office System (Beta) installed.  I got this a few months back so that I could open up the new Word docx file format using Word 2003.  Seems that when Microsoft first released this patch, it was using Office 2007 beta software.  I uninstalled this package and tried the Web Component install again.  Worked like a charm this time.

I cancelled the installation and began fresh installing VS2008 once again.  This time it successfully went through the Web Components and continued with the rest of the installation with no issues.

Uninstalling the Compatability Pack worked for my issue.  Microsoft has since released a new version of the Compatablity Pack that does not use Office 2007 beta software.  You can find that here.

 

Tags:

.Net

ASP Buffering Limit

by ely January 03, 2007 07:05

I'm sure this isn't new to any blogs anywhere, but I wanted a quick place to find it in case I need it again in the future.  We had a problem this morning with an asp page giving the following error:

 Response object error 'ASP 0251 : 80004005'

Response Buffer Limit Exceeded

/page.asp, line 0

Execution of the ASP page caused the Response Buffer to exceed its configured limit.

Turns out that IIS6 sets a limit on how much data can be stored in the response buffer when you are creating a page.  On the particular page that was giving us problems this morning, the total output was over 12MB, and the default for the ASPBufferLimit is only 4MB.  Sure, the 12MB of html being sent back to the client is a problem that will have to be fixed through redesigning the page, but I needed to get the size of the buffer increased so this page would work now.  So, to do so we increase the ASPBufferLimit through the adsutil utility:

adsutil set w3svc/aspbufferinglimit "4194304"

This will set the ASPBufferingLimit on the global web service that all the virtual directories inherit from, meaning that all the sites configured on this box now have this limit.  The number is the amount of bytes for the buffer limit, 4194304 (4MB) being the default.

Tags:

.Net

Powered by BlogEngine.NET 2.0.0.36
Theme by Mads Kristensen | Modified by Mooglegiant

About me

@ElyLucas

I am a Denver based software developer, focusing mainly on web technologies in the Microsoft stack.  I dig Asp.Net, MVC, jQuery, C#, JavaScript, and HTML5.  I am starting to dabble in mobile technologies like iOS, Android, and wp7, and have particular interests in mobile web.  I am a Microsoft MVP for Asp.Net/IIS.  I live in Westminster, CO with my wife, son, and dog.  During the day, I sling code for @aspenware creating awesome software.


Month List

Page List

Site hosted by: