Domanda
Sto cercando di aggiungere Bootstrap Nancyfx con Ravendb e sto facendo in esecuzione nel seguente errore cercando di eseguire l'applicazione ...
"Impossibile risolvere il tipo: Nancy.iresponseformatter"
Ambiente:
ASP.NET
Nancy
nancy.hosting.aspnet
Ravendb
VS2010 SviluppoServer
In luogo di incollare tutto il codice, ecco un link al sito che ho usato come esempio. Per esempio, voglio dire che ho copiato Verbatim per vedere se potevo farlo funzionare. http://stuff-for-geeks.com/category/nancyfx.aspx
Ho effettivamente visto questo codice correre in una demo prima, ma io per qualche motivo non riesco a farlo funzionare affatto. Fallisce all'avvio. È quasi come se Nancy non stia usando il mio bootstrapper.
Più della traccia dello stack:
[TypeinitiLizationException: l'inizializzazione del tipo per 'nancy.hosting.aspnet.nancyhttprequesthandler' ha gettato un'eccezione.] Nancy.hosting.aspnet.nancyhttprequesthandler..ctor () +0
[TargetInvocationException: l'eccezione è stata gettata dall'obiettivo di un'invocazione.]
Qualsiasi aiuto sarebbe molto apprezzato.
Soluzione
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
Altri suggerimenti
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;
}
}