【发布时间】:2017-01-30 13:28:10
【问题描述】:
我目前面临一个问题,我需要将 ASP.NET Web API 用于 NopCommerce 网站,我需要连接到我的跨平台应用程序。
调用 API 时代码中断。它在以下位置产生异常:
var scope = EngineContext.Current.ContainerManager.Scope()
在task.cs 页面中。
我已经尝试调试过代码,但是没有定期产生异常;有时来,有时不来。但是,我确实找到了一些中断的地方:
var sis = EngineContext.Current.Resolve<StoreInformationSettings>().
我已经尝试加载它直到它加载。即,将其放入 while 语句中:
var sis = EngineContext.Current.Resolve<StoreInformationSettings>()`
while(sis == null)
{
sis = EngineContext.Current.Resolve<StoreInformationSettings>()
}
同样,还有几个实例。这意味着它会加载,但可能会在延迟之后加载。
在 nopcommerece 论坛中有更新autofac 包的说明,我照做了。
所有 Autofac Nuget 都在 Nop.Web 中单独试用。
参考此链接here,我尝试过修复,但没有解决我的问题。
I came to the conclusion that it has some issue with the Autofac DI settings (not sure)。还是 NopCommerce 的构建不支持 API?
正如 Rigueira 先生所建议的,我已尝试从插件本身的路由配置类中注册路由。
API 的路由配置在这里:
public void RegisterRoutes(RouteCollection routes)
{
GlobalConfiguration.Configure(config =>
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { controller = "TestApi", id = RouteParameter.Optional }
);
});
}
不过,它似乎没有任何区别。
我尝试以正常模式从插件注册路由:
public void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"WebApi",
"api/{controller}/{action}/{id}",
new { controller = "WebApi", action = "", id = RouteParameter.Optional },
new[] { "Nop.Plugin.WebApi.EduAwake.Controllers" }
);
}
还是不行。 这是我得到的错误:
Multiple types were found that match the controller named 'Topic'. This can happen if the route that services this request ('api/{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
Line 13: <div class="col-md-12 padding-0" id="home-topic" style="margin-left:-14px;">
Line 14: <div id="home-topic-content" style="width:104%;">
Line 15: @Html.Action("TopicBlock", "Topic", new { systemName = "HomePageText" })
Line 16: </div>
Line 17: </div>
在 index.cshtml 中。
这似乎是一个绝望的条件。
几个月以来我一直在研究这个!
【问题讨论】:
-
是不是 NopCommerce 的构建不支持 API? 你是什么意思?当然支持API
-
你想实现什么,问题没有解决,你得到什么异常?
-
@Div 抛出的异常是空引用异常,这就是为什么反复尝试直到使用 while 循环将某个值分配给变量的原因。我编写了许多 API 用于与 nopcommerce 的功能挂钩。但由于此异常,它们的工作方式非常不一致!
-
您在 task.cs 中遇到的错误很可能与 webapi 无关。检查出现错误的线程是否是实际为请求提供服务的线程,或者检查是否有可用的 httpcontext。还要检查调用堆栈,看看调用来自哪里。此外,第二部分似乎与 task.cs 无关,指出您过早地从容器请求实例并且它们尚未注册(这可能看起来很奇怪),正确诊断需要更多信息和详细错误。
-
应该是这样,但我发现它会创建分散的代码,难以理解和理解不同部分的连接方式。它似乎也倾向于隐藏错误的真正来源,而是将构造一个对象的错误转换为空引用或其他类型的错误。如果它可以用于提高可维护性,那么就像软件中的几乎任何东西一样,大多数都应该谨慎而明智地应用。不能一味的用在所有的事情上,从而有所改善;没有没有灵丹妙药。
标签: c# asp.net-web-api autofac nopcommerce