This was originally an email I sent to .NET team in my company, then decided to share as a blog post.

The problem:

  • Let’s say you have a complex application, and this application (or part of it) runs very slowly. No bug s in results, no errors or exceptions, but it just so slow! Now you want to know which part of your code is the reason, which method(s) you need to go and modify., which methods take so long to execute or consume so much memory/CPU. How would you know that?
  • Let’s say you want to improve the performance of your application in general (say add caching or such), so, you want to identify which parts of your code deserve your attention and will really make difference (so that you don’t waste your time on optimizing something that will not have big effect in performance), for  example, you might want to identify which methods are called more than others from different parts of your code. How would you do that?

How to solve it, or, what is a profiler (v. short):

It is an application that you can run along with your own program. It’ll track all method calls and how any method call other method and most importantly how long each method call will take, and how it consumes resources.

 

There are many .NET profilers out there.

 

So, what about EQATEC:

Quoting from homepage:

Spot slow code

Are you a .NET developer? Would you like your application to run faster? Then use our free profiler to spot you app’s slow code.

Point and go

No source code changes are needed. Just point the profiler to your app, run the modified code, and get a visual report.

Speedup any .NET app

As the only code profiler in the world, our profiler can even handle Compact Framework applications.


Language

Version

Platform

clip_image001 C#

clip_image001[1] VB.NET

clip_image001[2] Managed C++

clip_image001[3] F#

clip_image002 C or C++

clip_image002[1] Java

clip_image001[4] .NET 3.5

clip_image001[5] .NET 3.0

clip_image001[6] .NET 2.0

clip_image001[7] .NET CF 3.5

clip_image001[8] .NET CF 2.0

clip_image002[2] .NET 1.1

clip_image001[9] WinXP, Vista

clip_image001[10] Windows Mobile

clip_image001[11] Windows CE

clip_image001[12] XP embedded

clip_image001[13] PocketPC, PDA

clip_image002[3] Linux

To use it with ASP.NET application, all you need to is:

  • to put the path of the “bin” folder of your website as “App Path” (no need for source code or debug files), then it shows a list of all assemblies in it so you choose the DLL(s) you want to profile, and click “Build” at the right corner of the screen:
    EQUATEC
  • Click “Open output folder” on the bottom left corner, copy the DLLs from there to your website “bin” folder
    • Alternatively you can click “App Options” and set the output folder to “$(AppDir)” so that the generated files replace the old one
    • You need to repeat these previous steps if you build the website again using Visual Studio
  • run the website and start using it for a while
  • go to EQUATEC “Run” tab and click “Take Snapshot
  • go to the “View” tab and start reading the results

It might be important also to spot the limitations:

Known limitations

This is a list of known limitations and problems in version 2.0.

  • Blocking methods, such as Read(), will be counted as the total time, including the time the thread spends being descheduled and waiting for the call to complete. For now, you will have to recognize and judge situations like this manually.
  • No debug information is available for the profiled assemblies. It means that you cannot debug the profiled versions – but you would probably not like to do that, anyway.
  • No Visual Studio integration yet. We are looking into providing an add-in that will make it even easier to enable profiling your solution’s assembly files.
  • No full method signature is displayed yet. So if you have several overloaded methods by the same name, or use generic methods, you cannot easily distinguish them as their signature (the method’s parameter list) is not displayed anywhere.
  • Only defined methods are currently profiled, not referenced ones. So all the methods that your application itself defines will be profiled, but not System.* etc.

I hope this is useful for some of you as it was a real saver at times to me. Thank you very much!

, , , , , , , ,

Click here to go directly to the instructions…

Background (Click above to skip if you know Web Application Projects)

In VS 2002/2003, the web project model for a website was similar to “class library” projects, where you have a .CSPROJ or .VBPROJ file that keeps track of files “included” in the project, and compiles all the pages and controls code behind to a single assembly under “bin”. Each page/control has an automatically generated .DESIGNER.CS or .DESIGNER.VB file, which contains objects mapping to the server controls in the page/control markup (the generation of those files was not always in synch with markup, and that was problematic).

With VS 2005, there was a new “website” model for web projects that compiles each page/control individually as a separate assembly (or each folder, depending on optimization features), and applies this to all files in a given directory and its sub folders. This was a total mess in most “real world” projects, as VS takes so long to build the entire website, and even at deployment, you get sometimes many problems when you have pages that “reference” other pages/controls when IIS it trying to dynamically load the right assemblies to reference, and many other problems.

So, Microsoft came with a new add in to VS 2005 called “Web Application Projects“. This is typically the same old VS 2002/2003 project model with no problems in generating DESIGNER files and with integration with both IIS and ASP.NET development server that comes embedded in VS 2005/2008. It was later merged with VS 2005 SP1, and shipped as part of VS 2008 (without removing the “website” model). Note that most stuff that has to do with Microsoft like ASP.NET AJAX Toolkit Sample website and so are actually “web applications” not “websites”.

The problem

Typically, when you are converting any project from VS 2003 to VS 2005 SP1, it converts as “web application” not “website”. You can also convert a “website” to a “web application”. There’s an option “Convert to web application” to look for.

In my company, all our web projects are “web applications”, well, except that other web project I was code reviewing and helping with its deployment! After spending number of days with the brilliant team and not finding as many items to code review and getting sick of some problems at sometime in deployment, I cried to them to convert it to to “web application” (maybe I was looking for some job to be doing :D). Very confidently, I said, ” remember the option exists and I did conversion before in VS 2005. All it takes is a right click on the ‘website’ root node in solution explorer in VS 2008 and ‘Convert to web application’. It almost never causes any problems, and we have our source control anyway”.

They believed they had time to do it, so, they went to look for that menu item  “Convert to web application” and guess what ? They didn’t find it! They tried resetting VS 2008 settings and everything, and still, nothing there!!! Yeah, it was embarrassing :D :D :D

Workaround, or, how to convert a “website” to “web application” in VS 2008

Well, it turns out that the option “Convert to web application” does NOT exist for “websites”. The option “Convert to web application” does exist only for “web applications” !!!!

So, here’s the deal, to do the conversion, you need to:

  • Add a new “Web Application” to your VS 2008 solution (File->Add->New Project->C#->Web->ASP.NET Web Application).
  • Afterwards, you copy all the files in the old “website” to your newly created “web application”, and override any files created in it by default
  • The next step is the most ugly, you need to “manually” add the references in your “website” to the new “web application”. I thought the VS 2008 PowerCommands toy would do this for me as it does copy references from other project types, but it didn’t. You have to do it by yourself, manually, and you have to be cautious in this step if you have multiple versions of the same assembly (like AJAXToolkit in my case) or assemblies that have both GAC and local versions or so.
  • Keep repeating the last step and trying to build the “web application”. You’ll keep getting errors like ” ‘….’ is unknown namespace. Are you missing an assembly reference? “. Make sure you have none of those except the ones where ‘….’ is replaced by the IDs of the server controls you use. In other words, keep adding references and building the project until only the errors that exist because of missing .DESIGNER.CS or .DESIGNER.VB files.
  • Afterwards, go to the “web application” root project node in VS 2008 solution explorer, and right click it, then you WILL find the option “Convert to web application”. What this option does is actually making small changes to the “@Page” and “@Control” directives of pages and controls, and creating the required .DESIGNER.CS or .DESIGNER.VB files.
  • Try building the “web application” again. If you get errors, see what references may be missing and/or go click the “Convert to web application” again. Sometimes, if there’s any error other than those caused of missing DESIGNER files, not all the pages/controls will have those DESIGNER files created for them. Fixing the non DESIGNER problem and clicking “Convert to web application” again should do the job for this.
  • Once you are done successful VS build, you should be ready to go. Start testing your web application. Optionally, you can right click the “web application” root project node in VS 2008 Solution Explorer and click “Properties” then go to the tab “Web” to set the “web application” to a virtual folder in IIS (you can create new virtual directory from there in VS). If you want to use the IIS virtual directory that the old “website” used, you need to remove that from IIS first.
  • Update: When testing your pages, pay MOST ATTENTION to classes in “App_Code” folder, especially those with NO NAMESPACE. Those can be a big trap. We had a problem with two extension method overloads in the same static class that had no namespace,one extends DateTime? (Nullable<DateTime>) and calls another overload that extends DateTime itself. Calling the other overload as extension method passed VS 2008 compilation and gave us a compilation error ONLY IN RUNTIME (With IIS). Changing the call to the other overload from calling it as extension method to calling it as normal static method (only changing the call in the same class, calls from other classes remained extension method calls) did solve this one, but clearly, it’s not as safe as it used to be in VS 2005. Especially with classes with no namespaces.
  • Update2: During the conversion, VS 2008 renames your “App_Code” to “Old_App_Code”. This new name sounds ugly, but DO NOT RENAME IT BACK. In the “web application” model, all code will be in one assembly. In runtime, the web server does not know what web project type you are using. It does take all code in “App_Code” folder and create a new assembly for it. This way, if you have code in folder named “App_Code”, you’ll end up with RUNTIME compilation errors that the same types exist in two assemblies, the one created by VS, and the one created by IIS / ASP.NET Development Server. To avoid that. leave the “Old_App_Code” with the same name, or rename it to ANYTHING EXCEPT: “App_Code”. Do not place any code in such “App_Code” folder and prefereably do NOT have a folder with such name in your “web application” at all.
    I know this since before but forgot it now as I have not used “website” model for long :(.

I hope this helps anyone to avoid my embarrassment, and still get rid of the weird errors of “website” model :).

, , , , ,

This is problematic with ASP.NET AJAX. The main Script Components are NOT sent to the client when in :Legacy” mode. This is “By design” in ASP.NET AJAX, although it is a clear limitation!!

I’m investigating the problem for other solutions and will be sending an update soon.

Thank you, Iman Halawany, for making me note this. I’ve been working on WCF services and ASP.NET MVC stuff lately than normal webforms, so, didn’t realize this obvious showstopper.

To all my readers, I owe you a BIG apology.

The Problem

ASP.NET validators and ValidationSummary controls are rendered as SPAN tags that are shown and hidden based on validation state. The properties of the validators are written normally via JavaScript calls similar to these:


<script type=“text/javascript”>

//<![CDATA[

var Page_ValidationSummaries =  new Array(document.getElementById("vdsSiteLogin"));

var Page_Validators =  new Array(document.getElementById("rfvEmail"), document.getElementById("revEmail"), document.getElementById("rfvName"));

//]]>

</script>

 

<script type=“text/javascript”>

//<![CDATA[

var rfvEmail = document.all ? document.all["rfvEmail"] : document.getElementById(“rfvEmail”);

rfvEmail.controltovalidate = “txtEmail”;

rfvEmail.errormessage = “Email Missing”;

rfvEmail.validationGroup = “SiteLogin”;

rfvEmail.evaluationfunction = “RequiredFieldValidatorEvaluateIsValid”;

rfvEmail.initialvalue = “”;

var revEmail = document.all ? document.all["revEmail"] : document.getElementById(“revEmail”);

revEmail.controltovalidate = “txtEmail”;

revEmail.errormessage = “Email is invalid”;

revEmail.validationGroup = “SiteLogin”;

revEmail.evaluationfunction = “RegularExpressionValidatorEvaluateIsValid”;

revEmail.validationexpression = “\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”;

var rfvName = document.all ? document.all["rfvName"] : document.getElementById(“rfvName”);

rfvName.controltovalidate = “txtEmail”;

rfvName.errormessage = “Password is missing”;

rfvName.validationGroup = “SiteLogin”;

rfvName.evaluationfunction = “RequiredFieldValidatorEvaluateIsValid”;

rfvName.initialvalue = “”;

var vdsSiteLogin = document.all ? document.all["vdsSiteLogin"] : document.getElementById(“vdsSiteLogin”);

vdsSiteLogin.validationGroup = “SiteLogin”;

//]]>

 

</script>

 

Note that this is for ONLY 3 validators, 1 validation summary and in directly a page that doesn’t use a master page, not in nested user control or such!

How about a page with over 30+ validators (yeah, those forms!!), and each with ClientID like “ctl00_cphBody_ct00_fvUserLogin_rfvEmail_” and such?

If you have ever wondered why those pages take so much time loading, this code block (multiplied per number of validators you have and their properties set) is one reason.

You cannot even take the JavaScipt in separate file that can be cached, because this is dynamically created as per the visible validation controls.

The Solution (See Above Note)

The clear alternative to setting those properties via JavaScript long block with huge ClientIDs is to put the properties in the SPAN tags of the validation controls themselves.

The reason that AS.NET does not do this by default is that this is not XHTML 1.0 Transitional compliant, because the validator properties are not XHTML attributes of the SPAN tag.

ASP.NET tries to render XHTML 1.0 Transitional markup by default. But you can change that in your web.config file by adding one line under <system.web>:


<system.web>

        <xhtmlConformance mode=Legacy/>

 

This will make the properties render in the SPAN tags themselves, saving so much code in real life scenarios.

Personally I’d recommend: DO THIS IN EVERY WEBSITE YOU HAVE (See above note)

 

Thanks to Paulo Morgado for mentioning this.

, ,

If you have ever thought that the famous if(Page_ClientValidate("validationGroup")) {/*JS Code*/}  and myValidator.ValidationGroup = "validationGroup"; are sure not enough client side capabilities in ASP.NET validators, you are right.

The list of client side API for ASP.NET Validators can be found on this MSDN page "ASP.NET Validation in Depth":

http://msdn.microsoft.com/en-us/library/aa479045.aspx#aspplusvalid_properties

Look for subtitle: "Client-Side APIs".

 

Thanks Simone Chiaretta for mentioning the topic, Mohamed Tayseer for sharing the topic on facebook, and Richard Cook for his comment on the post making me search for the complete list.

, ,

The Problem

In an N-tier application, you keep your logic in a business logic tier, typically a different VS project that can be used from a website, a windows service, or desktop application, and that should be valid to writ unit tests against on its own.

But how about if your requirements say that you need to to upload some file for the business logic to work? Think of a scanned image (signed contract maybe?) or just a comma separated value file containing some emails.  Typically the business logic tier will be the place to to handle this, but how can you send the uploaded file to it? You can get the file as "HttpPostedFile" from the "Request.Files" collection or the file upload control itself, but, to receive it in the business logic, project, the easiest way is to add reference to "System.Web" dll and accept the type "HttpPostedFile" as method argument or so, and when in need to save the file physically, you call the "SaveAs" method of "HttpPostedFile" … So simple right ?

But how about unit testing? "HttpPostedFile" has no public constructor and is sealed class (you cannot inherit from). How will you write a unit test for a method that accepts "HttpPostedFile" as argument to work? You’d go finding a way to mock that or just forget about testing that particular method!!

The Simple Solution

Well, it’s more simple than you think. An "HttpPostedFile" has a property called "InputStream", which is of type "System.IO.Stream". Hey, that has nothing to do with "system.Web" :). You can create your own FileStream or whatever other stream in the unit test, and then only worry about other areas you want to test related to the method in your business logic.

Because I know dealing with streams is quite ugly (at least it is to me!), I wrote an example that will walk you though a sample usage of that property. I have the complete example VS solution as attached file at the end of the example code below.

Example


The Business Logic

In a real world example, the business class will have various parameters including the file, but in this example, I have made my business class (I call it "FileLogic") just worry about the file. It has a method that treats the stream coming to it as containing just text, reads that text and returns it. Note the type of the only method parameter

Another example of a method in the business logic project is one that actually saves the file. Instead of just calling "SaveAs(filepath)", you need to copy the stream yourself. Usually there’s more than one way to do so, the problem in most of the ways is determining the size of the stream. An "HttpPostedFile" knows the size of the file, but as I’m not getting the "HttpPostedFile" (and it wouldn’t sound great if I requested the file size as a method parameter!), I dealt as if I do not know the actual file size. See the code for this method:



The UI (Website)

Before we dig into the unit test sample, let’s see how our page code will look when dealing with this business logic class.

Consider a page with controls like these:

The code behind for such page with our custom logic class (in a sample manner still), would look like this:

I’d say that this part is not much different than it’d be if we were passing the "HttpPostedFile" object complete.



The Unit Test

So, that’s what we have been hassling, complicating code, and dealing directly with streams for its sake!

As our application will not necessarily be a file sharing application, we’ll be in need for the file as part of a bigger operation. A unit test will not be only concerned with the file upload task, but also the rest parts of the business operation our method intends to do. However, in this sample, I’ve decided to make it just test the file uploading functionality, and included really simple ways in the unit test.

Instead of the file upload, I used a physical file that I get its path from a method called "GetTestFilename" (I could have used configuration file, static property or else for the file stream, or used any other kind of stream still).

See how the test methods look in this example (using NUnit):

 

The Complete VS 2008 Solution

I’ve attached a complete VS Solution of this example with few more comments than here so that you can see it more closely. This is a ZIP compressed VS 2008 solution.

File iconMeligy.Samples.TestableWebFileUpload.zip

 

Background of this Article (Totally Useless. Serious Warning)

Almost a year and half ago (Just a month after joining SilverKey), I was asked to build a file sharing area as a module of community site for one of our biggest clients. This involved much extension of an existing file upload business logic component that was written for internal usage in other systems for the same client to allow logical folders and large file upload with very high frequency at certain times of the year. A month or more ago (just before my exam vacation), another developer was asked to build a new file sharing component for another client but for certain business field not community stuff, and to make it generically reusable in any other systems (as our original component was quite tied to other rules in the general infrastructure of all the systems for the same big client). I was involved in design meetings with the other developer of course for what insights and snippets could be taken from the original component and my own extension as well.

After I got back from vacation (last week or so). I heard some notes from our tester (who also does his own unit tests whether or not the developer does :D), about the business logic not being "testable" because the business logic was passed "HttpPostedFile". The same applied to the old component actually.

So, today I wasn’t exactly in the best mood, and a friend was asking me why I wasn’t blogging for long (I said because I was more into reading than writing nowadays), so, I thought why not spend the night in refreshing my mind by writing some really basic example to show how to overcome that simple issue that I believe most people just do not realize!

Disclaimer

Again, this is something I wrote as a fix for some bad mood and was intended just to be a very simple example. I know 1000% that there’re other better ways to write each and every part of this code, and many issues was not covered. The desired parts to show here were the layering (including unit tests) and different ways to deal with streams. To be clear, I’ll make sure to support questions and such about it, but I’m not sure whether you can consider this a promise !!

 

, ,