Speed up Editing of Cshtml Razor files in Visual Studio: Resharper vs Web Essentials

Lately I’ve experienced a very slow editing on Razor pages in Visual Studio 2013. I had a set of extensions installed, but nothing special: Resharper 8.1, Web Essentials and VSCommands. When I was editing Razor .cshtml file, every keystroke was painfully slow, especially when I was trying to edit C# code there: we have a lot of helpers and html generation tools, our view are almost html-free, only control builders.

Long story short, by disabling extension one by one, I’ve narrowed down the performance issue to Web Essentials setting that tries to auto-format HTML.

To fix that in Visual Studio 2013 go to Tools -> Options; select Web Essentials section and HTML sub-section; and disable Auto-format HTML on Enter:

web_essentials

IHtmlString and what it can do for you

IHtmlString and what it can do for you

Start of this year, we have been working very hard on converting our MVC application from free-flowing Html in our Razor views to helper based templates. The ultimate goal is to be able to change one template and the rest of the application will follow the example. And with free-flowing HTML you can’t do that easily.

Continue reading

Uploading large files to Azure Blob Storage through REST API

I did write about uploading files to Azure Storage via REST API before. It turns out that implementation is very naive. As soon as I tried uploading anything larger that 64Mb, I hid a brick wall with exceptions.

Azure Blob Storage has 2 types of blobs: Page Blobs and Block Blobs. Page Blobs are optimised for random read-write operations. Block Blobs are storage. Here more about Page vs Block blobs. If you are just storing files in Azure, you’ll most likely will use Block Blobs.

It turns out that Block Blob storage has some limitations on the upload front. In one go you can upload up to 64Mb: 1024 * 1024 * 64. Add one extra byte and you get an error from the API (I
tested it). So instead of uploading large files, you need to cut them into blocks and then upload separate pieces of no larger that 4Mb. And once all the pieces (Blocks) are uploaded, you need
to commit them all and give order in which they should appear.

Continue reading

Asp.Net MVC ViewBag is BAD!!

Being on holidays gives me a lot of time to go through MVC questions on StackOverflow. And a lot of questions come from misusing of ViewBag and ViewData. A lot of people leave out Model from MVC and just slap all their data into ViewBag. And this is bad.

I can understand why people are lazy: [sarcasm] creating an extra class can be very problematic, takes a lot of keystrokes [/sarcasm]. What people don’t realise is that ViewBag make them work harder.

ViewBag is a dynamic entity and it can contain any property it likes with any type it likes. So every time you do in controller something like this:

    public ActionResult SomeViewBagAction()
    {
        ViewBag.SomeText = "blah";
        return View();
    }

Continue reading

Test for default constructors in models for Entity Framework

Entity Framework relies on models to have a default constructor with no parameters. But if you are working with DDD often you would like to have some parameters in your constructors. And then slam a protected default constructor just for EF not to throw exceptions.

So you end up with code like this:

public class Product
{
    protected Product()
    {
        // This is required for EF
    }

    public Product(String name)
    {
        Name = name;
    }

    public String Name { get; set; }
}

The first constructor must not be used by developers, so you make it protected – this is good enough for EF.

Continue reading

Can not connect to SQL Server from web-site on IIS?

I remember I had to fix this issue about a year ago. And I completely forgotten how to do this. And the same problem strikes again.

The issue was with a web-site (MVC5, 4.5.1 .Net) running on IIS. And I had an issue when trying to get data from SQL Server.
The connection did not go through whatever I did.

The fix was quite simple. All I needed to do is to change the AppPool underlying user to be “NetworkService” instead of “ApplicationPoolIdentity”:

  1. In IIS, in list of Application Pools find the one which works with your site.
  2. In Advanced Settings (link on right) for that AppPool find Section Process Model and Identity setting first
  3. Change Identity to be NetworkService
  4. Restart AppPool and the web-site.

This is possibly one of the reasons that can cause the issue. There is a million of other reasons this can happen, but I have checked most of them before started semi-randomly poking about with app pools and IIS settings.

Test all you queries to have handlers

CQRS architecture seems to be very popular. And I’m on that wave as well. In short CQRS is separating all read-actions from write-actions. And you have different classes for reading and writing. Queries for reading. Commands for writing.

For queries you would have IQuery and IQueryHandler interfaces:

public interface IQuery<out TResult>
{
}

public interface IQueryHandler<in TQuery, out TResult> where TQuery : IQuery<TResult>
{
    TResult Handle(TQuery query);
}

For command you would have very similar set of interfaces:

public interface ICommand
{
}

public interface ICommandHandler<in TCommand>
{
    void Handle(TCommand command);
}

Continue reading

Test Your IoC Container

There are doubts about if you should test registrations in you IoC container. I have been intimidated by this blog-post for a while and did not touch container for writing our tests. However, we did have a problem in the past, when DI container could not create one of our controllers. And we have lost days trying to chase the problem – there was no meaningful error message, because the real exception was swallowed somewhere in transit. It turned out that we have violated DI principal of a simple constructor: “Do nothing in constructor”. I have blogged about this before.

Today I would like to show how we test our IoC container to check if it can create all our controllers. Because controllers are immediate aggregate roots that are used by an end user. And controllers in MVC are the final consumer in the dependency chain. So if you likely to hit a problem in a chain of dependencies, it is likely that you will catch it by creating all the controllers.

Of course, if you use IoC container as a service locator or Mediator pattern you will have to test container separately for the types you get out from container there (mostly Command Handlers and Query Handlers).

Continue reading

Faking HTTP Context for your unit tests

While writing and testing ASP.Net MVC application, many times over I needed to simulate HTTP Request and presence of HttpContext.Current. I need this mostly when I try to test code that interacts with MVC framework, like MVC Action Filters or custom Model Binders or testing routes.

ASP.Net MVC 4 is quite good in this matter – most of the framework can be replaced by a mocked object and there is a minimum number of static calls or sealed objects. So you can do the HTTP Simulation, but usually it involves a lot of boiler-plate code and many mock-objects to be set-up.

Continue reading

How to test code for accessing Azure Storage REST Api

Update: There is an updated implementation of this code: see this blog post

One of my applications has a feature where it is given a URL with Shared Access Signature to Azure Blob Storage and then via REST API it uploads files to the storage. You can do that by provided Azure Storage libraries. Originally I had that functionality implemented with these libraries. But now I’m slimming down the application (this is a small command line app) and getting rid of unnecessary dependencies. And for the sake of one upload method I don’t want to tie myself to masses of extra DLLs. So I’m replacing the libraries with REST API call.

I have spent some time trying to test this functionality. You may say that this is a not a unit test. Yes, it is not a unit test. Other questions? Also you may say that you shouldn’t test these kind of things and wrap a class around these and ignore. That is what I have done in the past, but that particular piece of code caused an endless pain because I’ve done it all wrong and it was not covered by tests.

Continue reading