Thycotic is Hiring!

My company, Thycotic Software, is looking to hire a seasoned Senior ASP.NET Web Developer.

If you aren’t familiar with us, we a identity management product company based out of Washington DC. I’ve been with the company 6 years myself in various different roles. At the moment I am a Team Lead working on Technical Support and “SkunkWorks”, a team dedicated to building interesting things and pushing the limits of our products.

We also embrace Agile principles, such as Test Driven Development, Scrum, and Paired Programming. Our environment is fun, fast-paced, and team oriented.

If all of that sounds good to you, take a look at our job posting for all of the details and how to get started.

CMAP MVC4 Slides and Code Available

I did a small presentation on some of the upcoming new stuff in MVC4 around these features:

  • Mobile Support
  • Async Functionality

Slides are available for Keynote and PowerPoint. Code requires Visual Studio 2010 with MVC4 and Async CTP 3. No guarantees these will continue to work as Microsoft continues to work on both of these frameworks.

Other thing to note is if you use a plugin for Visual Studio with its own Syntax Highlighting, such as Resharper, may incorrectly report syntax errors due to the introduction of the async keyword.

Finally, both of these technologies are pre-release. Don’t install either of these on an environment you can’t live without.

Get the code and slides here: https://github.com/vcsjones/Presentations/tree/master/CMAP.2011.11

NUnitAsp – Versioning and ASP.NET History

One of the things I wanted to go for with NUnitAsp was to bring it forward with compatibility for ASP.NET v4; but also retain compatibility with ASP.NET v2.

There are some hurdles to jump over to make that work.

NUnitAsp disregarded compatibility with ASP.NET 1.x. I don’t mind that either. It’s old, had many performance issues, lacked 64-bit, and a boatload of other reasons to move on.

Moving off of ASP.NET v2 is harder though. ASP.NET v2 effectively existed until ASP.NET v4. Sure, the .NET Framework 3.0 and 3.5 came out since then – but it still remained ASP.NET 2.0. Fire up Visual Studio 2008, create a new Web Application, throw an exception and take a look what the version is listed. It’ll be 2-something. Lots of apps will be tied to this version for a bit longer.

Microsoft tends to keep with the old school versioning scheme, too. Big changes, possibly breaking changes, are reserved for major releases only. That isn’t the law of the land (they’ve done it), but it certainly seems to be there goal.

Thus, we effectively had the same version of ASP.NET; save a few added framework pieces such as AJAX and MVC; from November 2005 to April 2010.

Breaking changes happened in ASP.NET v4. Most of them were bug fixes reported by customers. One notable one is how a textarea is rendered if it begins with line endings.

Assuming a text area starts with empty line, then “Hello World!”

ASP.NET v4 ASP.NET v2
<textarea>Hello World!</textarea> <textarea>
Hello World!</textarea>

There is an important, subtle different there. The number of line breaks. Actual “rn” in the HTML. It’s one of the rare cases in HTML where it makes a difference. In ASP.NET 2.0 – this was a bug. Setup a simple page that has a textarea, and a button that does a simple postback. It doesn’t even need a click handler. Fire up a respectable browser such as Chrome, enter “Hello World!” preceded by an empty line. Now do your post-back. You’d be surprised that your return in the textarea is now gone. Do the same thing except with two empty lines, and you’ll see that only one disappears.

This was a bug in how ASP.NET v2 rendered a textarea. A textarea should always have a new line after the opening tag in order to preserve empty lines. ASP.NET did not follow that rule.

Those that have been bitten by it know it’s a pretty simple fix by creating your own TextBox control and fixing the rendering by implementing their own Render().

Now that it was fixed in ASP.NET; NUnitAsp was confused. Why are there extra line endings?

So this simple assertion for NUnitAsp would break in ASP.NET 4:

[Test]
public void ShouldHandleAspNet4Rendering()
{
    var text = string.Format("Hello World");
    multiline.Text = text;
    doPostBack.Click();
    Assert.AreEqual(text, multiline.Text);
}

With this error:

expected:<"Hello World">
but was:<"rnHello World">

You’d think the fix for this is simple. Just strip off the first 2 characters.

That would work under ASP.NET 4 for only; and only for Windows. It’s entirely possible that Mono would only render the line-feed (Unix style line endings). For ASP.NET v2; that might strip off 2 characters that shouldn’t be. More importantly, we can’t just trim them off because what if you wanted to write a test that asserted a textarea began with a new line?

This is where things got a bit sticky. There are two unknowns: Platform and ASP.NET version. The platform bit I am not entirely worried about. It is a legacy project, and NUnitAsp wasn’t designed with that in mind. The ASP.NET version is a doozy though. In order to know if the new line needs to be stripped off, we need to know what version of ASP.NET rendered it. There is no reliable way to detect the ASP.NET version purely from rendered markup. Knowing that, I weighed my options.

Do Something Unreliable

The thought crossed my mind. There is, one arguably reasonable way to detect the ASP.NET version. IIS will serve an HTTP header called “X-AspNet-Version” depending on the ASP.NET version that’s loaded up. This is unreliable for a number of reasons:

  1. No one is stopping you from cracking open the IIS Manager and removing the version all together. I’ve seen some people do this, particularly in production environments.
  2. Different web-servers are starting to pop up. IIS Express is common for development, as is Cassini. Being at the mercy of the server technology felt uncomfortable.

OK, so nix that.

Make It Configurable

I generally hate any project (that should be simple) that requires me to add configuration before it will work properly. I’ve found (including myself) that people generally just want to see it work to see if it is worth their time. If it takes time to get it to work, they’d lose patience and give up. Again, not a huge issue, we’re here for legacy support only – I don’t plan on many people starting fresh with this tool.

Hybrid

Try to deduce the ASP.NET version from the HTTP Header, and allow override from configuration.

No.

Different Builds

This is harder on the developer more than anything, but provides the cleanest experience for the developer. If you are using ASP.NET v2; reference NUnitAsp2. If you are on ASP.NET v4; use NUnitAsp4. Upgrading? Switch versions.

Ultimately, different builds is the solution I am going to go with. I’m not entirely sure how yet; but it’s in the works.

MVP 2011

Today was my newal date for being an ASP.NET MVP. I am happy to say that I have been awarded for a 5th year. Big thanks to Microsoft for supporting the community, and everyone in the community.

Regardless of MVP or not, I love to interact with the community and will continue to do it.

Centering Content Properly

As an ASP.NET Developer, not a designer, it’s pretty easy to write crappy HTML. As I have come to learn, content designers will hate you for that. One that has always fooled me is how to properly center content. Centered content is nice sometimes. It put information right in front of the users face, exactly where they expect it. HTML has come a long way since 2000, and it’s worth looking at some of the right and wrong ways to do it. Your designers will love you for it.

The oldest, and simplest way to center content is to use a <center> tag. Designers hate this. It works differently between IE 6 & 7, and 7 will display it differently depending on the DOCTYPE. In quirks mode, it has an interesting effect. If you center a DIV tag in quirks in IE, the DIV will be centered, but the DIV’s contents will be left aligned. That’s actually the behavior we want, but unfortunately not a good option. It only works that way in IE, and your forced to use quirks. If you use standards mode, the center tag behaves correctly: everything is centered, even the content of the DIV, which is not what we are looking for.

To that extent though, you could use a center tag, and just left align the contents of the DIV. Something like this:

<html>
    <body>
        <p>Some other content here, maybe a header.</p>
        <center>
            <div style="width: 500px; border: solid 1px black; text-align:left;">I want this DIV centered, but the text still left aligned.</div>
        </center>
        <p>Some other content here, maybe a footer.</p>
    </body>
</html>

Of course I would put my CSS in an external file, otherwise you’ll really catch hell from a designer. Needless to say, it gives the right effect:

center

But this really isn’t the best choice. Depending on what your designers have to work with, for say, a product, they may only have access to the style sheets. A center tag is not very flexible, and cannot be put into an external file like a CSS stylesheet.

So what other options are there? Tables would work, but that’s even worse than the center tag, and has the same problem as the center tag: It’s not CSS. So how do you do this using CSS? The trick is auto margins. It’s a fantastic part of the CSS layout that I find surprisingly, not enough developers know about. The HTML looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
        <p>Some other content here, maybe a header.</p>
        <div style="width:500px; margin: 0px auto; border: solid black 1px;">
            test
        </div>
        <p>Some other content here, maybe a footer.</p>
    </body>
</html>

Note that IE, even the famed standards compliant IE 8, will NOT render this correctly without the correct DOCTYPE. IE will leave it left aligned, but Gecko based browsers (Firefox) will still render it correctly.

So why is this the best solution (IMHO)? It’s completely controlled by CSS, and content designers love that.

What’s essentially happening here is the browser is automatically computing the margins for both the left and right side, giving it the appearance of being in the center.

Managing IIS Pipeline Mode for Backward Compatibility

Ever since the induction of IIS 7, there came a cool new feature for developers to leverage called Integrated Pipeline. This feature in a short explanation closely couples ASP.NET and IIS more closely. It allows allows writing of IIS Modules in managed code, how neat is that? This also slightly changes the behavior of ASP.NET, such as introducing new events on the HttpApplication (Global.asax) and the event cycle. A classic example is where in Integrated mode you cannot access the current request using HttpContext.Current.Request during theApplication_Start event, which makes sense.

Now for obvious reasons this can break existing applications, and cannot be used for applications that will be installed in mixed environments, such as Server 2003 and Server 2008. Fortunately, IIS 7 make it easy to switch it back back to its old behavior using the Classic Pipeline by configuring the IIS AppPool. Most commonly, if you make a web-based product then your application will usually require Classic Pipeline to stay backwards compatible.

Even if you have a detailed instruction manual, this can be an easy step to miss – and results in some icky error messages. Wouldn’t it be nice if you could display an nice error message to your customers that their pipeline is configured improperly?

Well fortunately there is a way to do that. Ironically, we will be using part of the Integrated Pipeline to get that accomplished with the help of an HttpModule.

What we will be doing is writing an HttpModule that will basically stop the current request, write out a friendly message that links to a Knowledge Base article, and only do this if our application is running in Integrated Pipeline. The module to get this done is pretty simple, we would write it like any other HttpModule. Here is the code for mine:

 

   1:  using System;
   2:  using System.Web;
   3:   
   4:  namespace MyWebApplication
   5:  {
   6:      public sealed class IisPiplineCheckModule : IHttpModule
   7:      {
   8:          public void Init(HttpApplication context)
   9:          {
  10:              context.BeginRequest += context_BeginRequest;
  11:          }
  12:   
  13:          private void context_BeginRequest
  14:               (object sender, EventArgs e)
  15:          {
  16:              HttpContext.Current.Response.ClearContent();
  17:              HttpContext.Current.Response
  18:    .WriteFile(VirtualPathUtility.ToAbsolute("~/pipeline.htm"));
  19:              HttpContext.Current.Response.End();
  20:          }
  21:   
  22:          public void Dispose()
  23:          {
  24:          }
  25:      }
  26:  }

As you can see, there is nothing particularly difficult. It just hooks into the BeginRequest event of the Application, and writes a file called pipeline.htm to the response stream and closes the response. Pretty simple, right?

Well the final trick is getting this to work only in Integrated Pipeline. Unusually, there is no easy way to do this with the .NET Framework (that I was able to find). However we will leverage the web.config to accomplish this.

For IIS 7, there is a new section in the web.config file called <system.webServer>. This is one of the sections that IIS 7 pays attention to. What we will do is introduce our module into IIS 7′s Integrated Pipeline, but not into the old <httpModules> that has been existing for a long time. Let’s look at our new section and examine it.

   1:  <system.webServer>
   2:      <validation validateIntegratedModeConfiguration="false" />
   3:      <modules>
   4:          <add
   5:   preCondition="integratedMode"
   6:   name="IisPipelineCheckModule"
   7:  type="MyWebApplication.IisPiplineCheckModule,MyWebApplication"
   8:          />
   9:      </modules>
  10:  </system.webServer>

There are two interesting points here. The first is actually adding out module. Notice the preCondition. This is telling IIS 7 that our module should only be running in Integrated Pipeline by specifying the integratedMode flag. For the Type we just set the full namespace and class to our HttpModule and the assembly it is located in.

The second interesting point is that if you have standard HttpModules registered in the configuration/system.web/httpModules section IIS 7 will point out that there are modules in there that are not in the system.webServer configuration. We can, if we want, disable that through the validation element.

And ta-da, we now have a module that writes out a file, but only for Integrated Pipeline. You can customize the pipeline.htm file as much as you’d like. As far as I can tell, this is the only easy way to determine if your application is in Integrated or Classic Pipeline. The alternative is to WMI query the IIS metabase, which is a little more high risk and more complex.