我正在尝试使用RavenDB添加引导程序NancyFX,并且在尝试运行应用程序时遇到以下错误...
“无法解析类型:Nancy.IResponseFormatter”
环境:
ASP.Net
南西
Nancy.Hosting.Aspnet
RavenDB
VS2010 DevelopmentServer
代替粘贴所有代码,这里是我用作示例的网站的链接。举个例子,我的意思是我逐字复制它,看看能否让它发挥作用。 http://stuff-for-geeks.com/category/NancyFx.aspx
我之前已经看过这个代码在一个演示中运行,但由于某些原因我无法让它运行。它在启动时失败了。这几乎就像南希没有使用我的BootStrapper。
更多Stack Trace:
[TypeInitializationException:'Nancy.Hosting.Aspnet.NancyHttpRequestHandler'的类型初始值设定项引发异常。 Nancy.Hosting.Aspnet.NancyHttpRequestHandler..ctor()+ 0
[TargetInvocationException:调用目标抛出了异常。]
任何帮助都会非常感激。
答案 0 :(得分:3)
该代码基于Nancy的旧版本。您应该考虑使用IResponseFormatterFactory。博客文章中定义的自定义模块构建器基于DefaultNancyModuleBuilder的旧副本,如果您查看当前版本https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Routing/DefaultNancyModuleBuilder.cs,则应该能够进行必要的调整
答案 1 :(得分:2)
以下是正在讨论的RavenAwareModuleBuilder
课程的代码:
修改1
以下代码已针对Nancy Release 0.12进行了更新。请注意BuildModule方法中的新NegotiationContext
行。
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;
}
}