1
爲什麼下面的設置無法正常工作,我一直在琢磨幾個小時。它拋出404爲什麼使用Autofac設置此webapi 2不起作用?
我做了什麼至今:
- 我已經檢查了的NuGet軟件包 - 它們是很好的,最新的,最偉大的
- 我已經檢查了Autofac文檔 - 下面的代碼來自除了
- 我檢查了主機文件typeregisters有 - 本地主機設置正確
- 我已經創建了一個空的,新的WebApi2項目,我已經使用了相同的Web應用程序,以檢查它是否工作,它的工作原理 - 這樣的問題圍繞Autofac整合
- 我已經評論了所有Autofac安裝程序,並且我回到了基本的webapi安裝程序,請求拋出錯誤,因爲控制器在構造函數中有一個參數,它沒事,因爲請求碰到控制器
- 我搜索了Autofac WebApi2集成和示例,我的代碼似乎很好
我錯過了什麼?我還應該檢查什麼?
它是如何工作的,或者我想要工作的。 WebApi控制器只獲取businesslogic作爲參數,Autofac應該處理它,而BusinessLogic庫獲取其他樹組件(Mapper,SoapClient,Repository)。它應該工作,或者至少我在我的其他應用程序中有類似的設置,但也有一個Owin設置。
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var builder = new ContainerBuilder();
var config = GlobalConfiguration.Configuration;
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);
builder.RegisterType<Repository.Repository.Repository>()
.As<IRepository.IRepository.IRepository>();
builder.RegisterType<BusinessLogic.BusinessLogic.BusinessLogic.BusinessLogic>()
.As<IBusinessLogic>();
builder.RegisterType<SoapClient>()
.As<ISoapClient>();
builder.RegisterType<Mapper.Mapper.Mapper.Mapper>()
.As<IMapper>();
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
//GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
[RoutePrefix("PipelineWorkflow")]
public class PipelineWorkflowController : ApiController
{
#region Private variables
private IBusinessLogic _businessLogic;
#endregion
#region Constructors
public PipelineWorkflowController(IBusinessLogic businessLogic)
{
_businessLogic = businessLogic;
}
#endregion
[HttpGet]
[Route("GetPipelineWorkflows")]
public string GetPipelineWorkflows()
{
return "asd";
}
}
安裝的NuGet包
PM> Get-Package -project GoNoGo.Services.webapi | Format-Table -Autosize
Id Versions ProjectName
-- -------- -----------
Autofac {3.5.2} GoNoGo.Services.WebApi
Autofac.WebApi2 {3.4.0} GoNoGo.Services.WebApi
EntityFramework {6.1.3} GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi {5.2.3} GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.Client {5.2.3} GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.Core {5.2.3} GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.WebHost {5.2.3} GoNoGo.Services.WebApi
Newtonsoft.Json {8.0.2} GoNoGo.Services.WebApi
感謝您的幫助提前!
更新:
- 我已經取代Autofac團結與它的工作就像一個魅力。
請添加代碼的路由配置和控制器,竟然放棄了404 –
問題得以延長,並感謝您的幫助! – SayusiAndo