Question

I am trying to add bootstrap NancyFX with RavenDB and I am running into the following error trying to run the application...

"Unable to resolve type: Nancy.IResponseFormatter"

Environment:

ASP.Net

Nancy

Nancy.Hosting.Aspnet

RavenDB

VS2010 DevelopmentServer

In lieu of pasting all of the code, here is a link to the site that I used as an example. By example, I mean I copied it verbatim to see if I could get it to work. http://stuff-for-geeks.com/category/NancyFx.aspx

I have actually seen this code run in a demo before, but I for some reason can not get it to run at all. It fails at start up. It is almost as if Nancy is not using my BootStrapper.

More of the Stack Trace:

[TypeInitializationException: The type initializer for 'Nancy.Hosting.Aspnet.NancyHttpRequestHandler' threw an exception.] Nancy.Hosting.Aspnet.NancyHttpRequestHandler..ctor() +0

[TargetInvocationException: Exception has been thrown by the target of an invocation.]

Any help would be really appreciated.

Was it helpful?

Solution

That code is based on an older version of Nancy. You should be looking at using the IResponseFormatterFactory instead. The custom module builder, that is defined in the blog post, is based on an old copy of the DefaultNancyModuleBuilder and if you have a look at the current version https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Routing/DefaultNancyModuleBuilder.cs you should be able to make the necessary adjustments

OTHER TIPS

Here is the code for the RavenAwareModuleBuilder class under discussion:

Edit 1

The code below has been updated for Nancy Release 0.12. Note the new NegotiationContext lines in BuildModule method.

public class RavenAwareModuleBuilder : INancyModuleBuilder
{
    private readonly IViewFactory viewFactory;
    private readonly IResponseFormatterFactory responseFormatterFactory;
    private readonly IModelBinderLocator modelBinderLocator;
    private readonly IModelValidatorLocator validatorLocator;
    private readonly IRavenSessionProvider ravenSessionProvider;

    public RavenAwareModuleBuilder(IViewFactory viewFactory, IResponseFormatterFactory responseFormatterFactory, IModelBinderLocator modelBinderLocator, IModelValidatorLocator validatorLocator, IRavenSessionProvider ravenSessionProvider)
    {
        this.viewFactory = viewFactory;
        this.responseFormatterFactory = responseFormatterFactory;
        this.modelBinderLocator = modelBinderLocator;
        this.validatorLocator = validatorLocator;
        this.ravenSessionProvider = ravenSessionProvider;
    }


    public NancyModule BuildModule(NancyModule module, NancyContext context)
    {            
        context.NegotiationContext = new NegotiationContext
        {
            ModuleName = module.GetModuleName(),
            ModulePath = module.ModulePath,
        };

        module.Context = context;
        module.Response = this.responseFormatterFactory.Create(context);
        module.ViewFactory = this.viewFactory;
        module.ModelBinderLocator = this.modelBinderLocator;
        module.ValidatorLocator = this.validatorLocator;

        context.Items.Add(
            "IDocumentSession", 
            ravenSessionProvider.GetSession()
        );

        module.After.AddItemToStartOfPipeline(ctx =>
        {
            var session = ctx.Items["IDocumentSession"] as IDocumentSession;
            if (session != null) session.Dispose();
        });

        return module;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top