Tuesday, July 28, 2009

WCF and IIS 7 Error

I ran across this little nugget of frustration today when trying to hit my WCF services hosted by IIS 7 on a computer with a new build:

Error 403.3
The page you are requesting cannot be served because of the extension configuration...

Turns out that the .svc extension was never registered with IIS.

Simple problem, simple solution. With administrative privelages run:

C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\servicemodelreg -i

or for 64 bit OS's

C:\Windows\Microsoft.NET\Framework64\v3.0\Windows Communication Foundation\servicemodelreg -i

Just another post to bump the solution up Google a bit.

Saturday, July 25, 2009

Rhino Mocks Out Ref Parameters

I started playing with Rhino Mocks recently. Yes, I'm late to the game. And yes, that game is great. If you've made it to this post I'm obviously preaching to the choir, so let's get to it.

I ran across a need to mock up an out parameter today. For the context of this article that would be a ByRef parameter to all you VBers. What to do? Well, it turns out to be a very simple task.

I have several repository methods in a little project of mine that read paged data. I like to return the total number of records in the same query from the database and so I use an out parameter in my repository methods. The methods look something like this:

public IList<RecordModel> ReadPaged(int pageNumber, int pageSize, out int recordCount);

This project of mine is written for ASP.NET MVC which, like every other ASP.NET MVC project, contains controllers. My controllers makes calls on the repositories and push the models returned into the ViewDataDictionary. The goal then is to create a stub for the IRecordRepository.ReadPaged() method that passes a content count, among other values, back to the controller so that the value may be pushed into the ViewDataDictionary and then validated in a unit test. That's a lot of talk for one small method call:

recordRepositoryStub
.Stub(m => m.ReadPaged(pageNumber, pageSize, out recordCount))
.OutRef(250)
.Return(records);

That call to OutRef(250) is what takes care of the out parameter. The method accepts a parameter array of objects. When the stub method is called by the controller, it populates its arguments into the out parameters in the order in which they are declared in the method signature.

Goal accomplished.

Monday, July 6, 2009

IoC and Structure Map

I started playing with StructureMap a couple of days ago. It is a pretty decent framework for IoC and dependency injection. It works well with ASP.NET MVC and makes it possible to write some good, testable code. That's not to say that there isn't anything better out there, just that this gets the job done well and so I didn't need to look for anything better. The features that I like most are the registry configuration system, and the dependency injection approach.

One way to configure StructureMap is to subclass the Registry class. Methods in the class allow you to create mappings between interfaces or abstract classes and the concrete types that will be used during run time. The simplest approach is to create one to one mappings for each interface / concrete type pair. Nothing you couldn't do with a simple text or configuration file of some sort. However, there are more powerful methods available that will automatically map all types against the interfaces they implement within a given assembly or namespace. You can even map multiple types against the same interface and use a key to get the correct implementation for a given situation at run time. Score one for the IoC framework.

As for creating the mapped instances, there are a couple different ways to get that done. The simpler approach is just a factory method that accepts the interface name and optionally a key and returns the configured implementation. Again, this is nothing that you can't do very quickly and easily with the Activator object in .NET. Once again, a more powerful technique exists and that is to request an instance of an object that accepts mapped types in its constructor. E.g. Suppose I have a class SiteController with a constructor defined as void SiteController(ISiteMapper mapper, ISiteRepository repository) and that both the interfaces have mapped concrete classes configured. I could request an instance of SiteController from the StructureMap ObjectFactory and it would create the SiteController container with the appropriate dependencies passed to the constructor. This is another nice convenience that good IoC frameworks provide you with.

Martin Fowler said something I like about IoC:
"When these containers talk about how they are so useful because they implement 'Inversion of Control' I end up very puzzled. Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels."
My take on this quote is that IoC is nothing more than good object oriented design that takes advantage of polymorphism and factory patterns. It's a technique that has been around since the first OO languages and isn't some new advance in software design. That being said, StructureMap and other frameworks like it do offer some great utilities that simplify the task of writing the code.