Monday, August 19, 2013

Pictures from Austin Code Camp 2013

Here's a few pics from the Austin Code Camp 2013.
The Camp was great.  The advanced planning and hard work of all those involved was evident.
It always surprises me that the camp is free, with two free meals thrown in.

Registration was fast and easy.
 My 8:30 class was "Web API Quick Start" by Ryan Vice.
My notes:
* Ryan stressed a resource-centric, guessable URL design like the apogee web site promoted I was not able to find the apogee web site) like Accounts/:id/Withdraw and not AccountWithdraw/:id
* Ryan used Postman for debugging URL, although Fiddler works as well
* HTTP 201 is the freturn code if you create a resource (Accounts/add) and in the headers you return "location: Accounts/12345"
* The MVC framework has HTTP codes so you don't need to put in a magic string, "201".
* They use a web api pipeline poster to discern where to insert their function into the sequence.  Perhaps he was talking about this one.
* To create a Web Api project start with a new "MVC4 Web" app in VS.
* Use the Json.Net framework
* For versioning use something like "uship.com/api/v2/Resource"
* JSON to C# utilities are out there that create a C# object from JSON text
* For documentation, instead of WDSL use HelpPage from NuGet



My next class was Database Optimization with Anil Desai
* Don't look for the longest running query, but the longest time*frequency
* Use the Activity Monitor in mssql by right clicking on the db server in Management Studio
* He showed a bunch of reports I'd never seen by right clicking on the database and selecting "Reports"
* Sometimes autogrowth is set to 1 Meg - too small a value for today.
* When tracing you can add a filter to only catch queries only above a certain threshhold
* It's good to add the name of the app in the connection string.
* He showed a statement with "BETWEEN" which I'd never seen.   BETWEEN 10 and 20
* When running a trace write to a file and then import to database, otherwise it soaks up more resources


John Crowe gave a good overview of Dependency Injection.
* The big problem is the "new" operator, it makes for ridged code that is hard to test.
* Three types of IOC: Factory Pattern, Service Location and Dependency Injection
DI has three types:  Constructor Injection, Setter/Property Injection, and Interface Injection

Anne hosted a "Lightening Talk" round where attendees gave informal 5 minute presentations.
Jeffrey Palermo gave in interesting talk on his new company, Clear Measure, started in January and quickly built the startup company by using Cloud based services.  They use GoogleApps for mail and video calls, GoToMeeting for conferencing, EchoSign for authentication, Ring Central for phones, Quickbooks for accounting, LegalZoom for incorporation and BaseCamp for management.
Latish Sehgal talked about tools for .Net Development, his list is at http://dotnetsurfers.com/tools.
* Use ReSharper to locate unused libraries
* Use Firebug console mode to test jQuery commands; YSlow to get recommendations on speed
* Glimpse (http://getglimpse.com/) does for the server side what Firebug does for the client.
* SMSS Toolpack which is a little like ReSharper for Sql Enterprise Management Studio. It remembers all your handwritten sql queries.
* Gray Wolf was the most interesting. This (hacker) tool allows you to insert code into applications.

Amir Rajan gave an interesting talk on "JavaScript MVC Framework Rundown"
* jQuery - spaghetti code
* Backbone - event driven (most accessible) may use Marionette plugin
* Knockout - MVVM with DuRandal
* Angular.js - redefines HTML
* Ember - all MVC in browser, steep learning curve

https://github.com/tastejs/todomvc has an excellent overview of all the browser-based MVC frameworks.


Friday, August 09, 2013

C# WebClient .DownloadStringAsync Blocking Main Thread

Photo by Max Ronnersjö
While needing to invoke a url in the background from a C# 5.0 program, I was overjoyed to stumble across "WebClient .DownloadStringAsync(url)". I thought I'd found a bird's nest on the ground. 
Here was a single call to solve my problem. 
Great job Microsoft framework group!

Well, not so fast.

My unit tests for bad and malformed URLs were actually blocking.
The problem is that WebClient.DownloadStringAsync() blocks until the connection is made.  If your application happens to call a bad url, the main thread will block until it gets an error code back.  This can be up to 3 seconds for normal malformed urls.  In  one case, our internet proxy had problems with the malformed url and consistently hung for 10 seconds while it was sorting out the problem and eventually returned a timeout error.

If you ever suspect the url you are calling is ever going to have issues (and we know all sites are up all the time!  Right.), don't use DownloadStringAsync, use DownloadString() wrapped in a Thread or Task.  This way if the connection has problems, it's another thread's problem, and your main thread can move forward.

...
new Thread(() => DownloadUrlSynchronously(url)).Start();
...

public void DownloadUrlSynchronously(string url)
{
            using (WebClient client = new WebClient())
            {
                try
                {
                    var x = client.DownloadString(new Uri(url));
                    _logFileWrapper.LogInfo("Url called successful - " + _url);
                }
                catch (Exception e) { ...}
                ...
}

Friday, July 12, 2013

Continous Testing with SpecWatchr

I was impressed with Amir Rajan's talk at Austin .Net this week, and was intrigued by his use of the Continuous Testing framework SpecWatchr, so I decided to give it a spin. You can download it at here. I found the installation instructions very clear and concise. You need to download Ruby 1.9.2 and Growl.

By default it assumes nspec, but it was easy enough to change it in dotnet.watchr.rb to be NUnit.

By default SpecWatchr assumes all the tests for MyClass.cs are in a file named "describe_MyClass.cs", which mine were not. Mine tests are in MyClassTest.cs. But since this is open source, I cracked open "watcher_dot_net.rb" and changed half a dozen lines of Ruby and got it to recognize my convention. It worked like a charm.

Amir encouged the .Net group to reduce the friction in development. One of these frictions is to manually save and run tests. With SpecWatchr all you have to do is save a file and it will automagically compile and run affected tests. When all is happy you get this in the corner of your screen for a few seconds.  :
If you have a compile time error you get this:
Things to like about SpecWatchr:
  • It's free!
  • It works.
  • It's well documented.
  • It's easy to install.
Things not to like: You have to annotate your tests with a category, which is the name of the class affected.   I don't see anyway around this without some serious introspection of the code.

Overall SpecWatchr is  a great tool.

Thursday, July 11, 2013

Pictures from Austin .Net Users Group July 8, 2013: Getting things done with dynamic ASP.NET

Amir Rajan (@amirrajan) gave an interesting talk on how to use C# 4.0's dynamic objects in MVC using the Oak framework to the 50 members present. Here's a few pics just in case you were wondering what a geekfest looks like.

My jumbled notes:
  • Hey, Amir is using Emacs - my people!
  • Recommends AngularJS and Knockout
  • Rake used to build and populate system
  • used conEmu for console
  • Oak has a DynamicDB and DymanicRepository
  • "ghost methods" are not present at compile time, but added dynamically
  • Use "System.Diagnostics.Debugger.Launch();" in code to bring up the debugger
  • Amir recommended http://www.infoq.com/presentations/Simple-Made-Easy
  • The Gemini extends the properties of System.Dynamic.DynamicObject to be more Rubyesque.
  • Amir used specWatchr for automatic compiling and testing
  • For GUI testing use "canopy F#", which is a layer above Selenium.
  • Three things: reduce friction, dynamic typing, increase feedback
  • dynamic is a thing. SignalR uses it.

Tuesday, July 02, 2013

How to disable Viewstate in .Net

Viewstate reminds me of a monster in a horror movie that just won't stay dead - it keeps coming back, and back.  No matter how many silver bullets you pump into its chest, it keeps coming.
You can turn off viewstate by adding the attribute "EnableViewState" to your page.

 <%@ Page language="c#" EnableViewState="false" Inherits="MyAwesomeApp.table" %>

But it didn't work in my page.  The value of EnableViewState is overridden by the MasterPage's value.
You can also change "EnableViewState" in web.configs with

<pages enableViewState=“false” />

But depending on which subdirectory the web.config is in, it may be overridden by something else.
The way I finally killed viewstate was by explicitly setting  the "EnableViewState" variable on the page:


private void Page_Load(object sender, EventArgs e)
  {
        this.EnableViewState = false;
        ...
   }

Then it stayed dead. 
Hmmm.... hey wait ... is that a heartbeat I'm hearing?

Monday, June 17, 2013

How to tell if you have .Net 4.5 installed

Since 4.5 is a drop in replacement for 4.0 you can't just look at the version and build number, v4.0.30319..
You have to look in the registry for HKLM\Software\Microsoft\NET Framework Setup\NDP\v4\Full.
If you have only version 4.0, "Version" is "4.0" and there is no "Release" key.
The official Microsoft documentation says that if the "Release" key exists and it's value is greater than 378389, then it's 4.5.
 My observation of looking at exactly two machines: If you already have .Net 4.5 installed, the value of "Version" is 4.5.50709.

Monday, June 10, 2013

Visual Studio 2012 Error: Could not find ... specified for Main method

This morning I ran into a problem while trying to compile.  Visual Studio 2012 gave me this error:
"Could not find 'Test.GetWebPage' specified for Main method"
I tried removing the file with the method from the project - since I could find no other bit of code referencing "GetWebPage", but Visual Studio still would not compile, complaining that file "CSC" needed it.
The issue was I had set GetWebPage as the startup object and now the method was gone.
To fix: inside Solution Explorer, right click on the project and select "Properties...".  Then under "Applicaiton" set the "Startup object:" to "(Not set)".