我正在一個asp.net MVC6項目,我在我的web api上有一些麻煩。當我測試某個Api時,我得到內部錯誤(500 http響應代碼)。在web api上的異常asp.net mvc6應用程序,我沒有註冊服務
我調查,我發現這個錯誤,它說,我還沒有註冊我試圖從服務來獲取對象:
類型 ' DotnetProject.BusinessLayer.Implementation.QuerryHandlers沒有服務。 GetTestByIQueryQueryHandler ' 已被註冊。
的問題是,我的控制器請求從IoC容器中的某個對象與[FromServices]
語法和我得到一些未註冊該服務甚至強硬。我做了一個例外。
如果我不從服務請求這個對象,我不會再有內部錯誤了。
我得到該錯誤的所有API函數,請求與[FromServices]
語法的對象。所以問題在那裏,但我無法弄清楚。
這裏是我的控制器類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotnetProject.BusinessLayer.Interfaces;
using DotnetProject.DataLayer.Domain_Entities;
using DotnetProject.DataLayer.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using DotnetProject.BusinessLayer.Implementation.Queries.Users;
using DotnetProject.BusinessLayer.Implementation.QuerryHandlers.Users;
using DotnetProject.BusinessLayer.Implementation.QuerryResults.Users;
using DotnetProject.BusinessLayer.Implementation.Queries;
using DotnetProject.BusinessLayer.Implementation.QuerryHandlers;
using DotnetProject.BusinessLayer.Implementation.Commands.Users;
using DotnetProject.BusinessLayer.Implementation.CommandHandlers.Users;
using DotnetProject.BusinessLayer.Implementation.Commands;
using DotnetProject.BusinessLayer.Implementation.CommandHandlers;
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace DotnetProject.Controllers.Api
{
[Route("api/[controller]")]
[AllowAnonymous]
public class TestController : Controller
{
private ITestRepository testRepository;
private ITestService testService;
public TestController(ITestRepository _testRepository,ITestService _testService)
{
testRepository = _testRepository;
testService = _testService;
}
// GET: api/values
[HttpGet]
[Route("getAllTests")]
public GetAllTestsQueryResult GetAll([FromServices] GetAllTestsQueryHandler handler)
{
return handler.Retrieve(new GetAllTestsQuery());
}
// GET api/values/5
[HttpGet]
[Route("getTest")]
public GetTestByIdQueryResult GetTest([FromServices]GetTestByIdQueryHandler handler,GetTestByIdQuery query)
{
return handler.Retrieve(query);
}
// DELETE api/values/5
[HttpPost]
[Route("delete")]
public void Delete(DeleteTestCommand command,[FromServices]DeleteTestHandler handler)
{
handler.Execute(command);
}
[HttpPost]
[Route("add")]
public void AddTest(AddTestCommand command, [FromServices]AddTestHandler handler) {
handler.Execute(command);
}
[HttpPost]
[Route("update")]
public void UpdateTest(UpdateTestCommand command,[FromServices]UpdateTestHandler handler) {
handler.Execute(command);
}
}
}
這裏是被發現爲「未註冊」一個對象實例:
using AT.Core.Cqrs;
using DotnetProject.BusinessLayer.Implementation.Commands.Users;
using DotnetProject.DataLayer;
using DotnetProject.DataLayer.Domain_Entities;
using DotnetProject.DataLayer.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DotnetProject.BusinessLayer.Implementation.CommandHandlers.Users
{
public class AddTestHandler : ICommandHandler<AddTestCommand>
{
private ITestRepository _testRepository;
public AddTestHandler(ITestRepository testRepository)
{
_testRepository = testRepository;
}
public void Execute(AddTestCommand newTest)
{
Test test = new Test();
test.Id = newTest.Id;
test.QuestionList = newTest.QuestionList;
test.TestDuration = newTest.TestDuration;
_testRepository.Add(test);
}
}
}
這裏是啓動文件我ConfigureServices功能:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<ITestRepository, TestRepository>();
services.AddScoped<ITestService, TestService>();
services.AddScoped<IRoleService, RoleService>();
services.AddScoped<IRoleRepository, RoleRepository>();
services.AddScoped<ICommandHandler<DeleteUserCommand>, DeleteUserHandler>();
services.AddScoped<ICommandHandler<UpdateUserCommand>,UpdateUserHandler>();
services.AddScoped<ICommandHandler<AddUserCommand>, AddUserHandler>();
services.AddScoped<ICommandHandler<DeleteTestCommand>, DeleteTestHandler>();
services.AddScoped<ICommandHandler<UpdateTestCommand>, UpdateTestHandler>();
services.AddScoped<ICommandHandler<AddTestCommand>, AddTestHandler>();
services.AddScoped<IQueryHandler<GetAllTestsQuery,GetAllTestsQueryResult>,GetAllTestsQueryHandler>();
services.AddScoped<IQueryHandler<GetTestByIdQuery, GetTestByIdQueryResult>,GetTestByIdQueryHandler>();
services.AddScoped<IQueryHandler<GetAllUsersQuery, GetAllUsersQueryResult>, GetAllUsersQueryHandler>();
services.AddScoped<IQueryHandler<GetUserByIdQuery, GetUserByIdQueryResult>, GetUserByIdQueryHandler>();
services.AddDbContext<ApplicationContext>();
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
config.OutputFormatters.Clear();
config.OutputFormatters.Add(new JsonOutputFormatter(new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
}, ArrayPool<char>.Shared));
});
services.AddMvc();
// Inject an implementation of ISwaggerProvider with defaulted settings applied
services.AddSwaggerGen();
}
恩科西嗨。感謝你的回答!添加這個 - >服務。AddScoped(); < - 做了訣竅。 –