I'm trying to perform a test using the Swagger API. Whenever I'm trying to input the timesheedid it gaves Error 500.
Here's the method
public void ComputeProductivity(long timesheetId)
{
var timesheet = _artDBContext.Timesheets.FirstOrDefault(x => x.Id == timesheetId);
if (timesheet != null)
{
var percentage = 100;
var conversionValue = 60;
var weekValue = 5;
var totalWorkingHoursPerWeek = 7 / 365;
timesheet.TotalHoursWorked = Convert.ToDecimal(timesheet.TimesheetDateOut - timesheet.TimesheetDateIn);
timesheet.ProductivityPerc = timesheet.TotalHoursWorked * percentage;
timesheet.ProductivityHr = timesheet.ProductivityMin / conversionValue;
timesheet.ProductivityMin = timesheet.TotalHoursWorked * conversionValue;
timesheet.FTERatio = (timesheet.TotalHoursWorked * weekValue) / (timesheet.TotalHoursWorked * totalWorkingHoursPerWeek );
_artDBContext.Entry(timesheet).State = EntityState.Modified;
_artDBContext.SaveChanges();
}
}
and here's the error I'm getting
Undocumented
Error: response status is 500
Response body
Download
System.InvalidOperationException: Unable to resolve service for type 'ArchPortal.ART.Repository.Interface.ITimesheetRepository' while attempting to activate 'ArchPortal.ART.WebAPI.Controllers.TimesheetController'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method34(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass7_0.<CreateActivator>b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Accept: */*
Host: localhost:7154
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
:method: POST
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: .AspNetCore.Antiforgery.hST4KUX5M6E=CfDJ8EXEmdxP_L9KuMlGxtZS0jlhrSBRZ4IrUDAEaYEvVQFB1cU3eYpFpnLe7UcI4uWnoBnl4laufEKpAfzGZxNi9FIRogfZUw748eKqm5FdObxE5qvuYse9jrb5UjVShfoanL8EajyKSdQ7sA0Hd7Kah1s,.AspNetCore.Session=CfDJ8EXEmdxP/L9KuMlGxtZS0jkQY5wu3raX+fcdeJaexR64LKlxAZEevFg2yi2OgH33rxoNy09Kip+8k2+GALHBwComubsMgU86lD+XqDcvH3FV4WbSHu7Kk/AehnMuapbWdQPT76kLHcwtcaVtMw9JdnbxFi6gtxusJ+VX5zoav0+T
Origin: https://localhost:7154
Referer: https://localhost:7154/swagger/index.html
Content-Length: 0
sec-ch-ua: "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-site: same-origin
sec-fetch-mode: cors
sec-fetch-dest: empty
Response headers content-type: text/plain; charset=utf-8 date: Mon,09 Jan 2023 08:35:26 GMT server: Kestrel
I'm trying to test if the computation works. And the data exist already in the database.
CodePudding user response:
Your issue means that your ITimesheetRepository
interface is not registered in your DI container, so it can't be found when you're constructing your Controller.
To fix this, assuming you're using the default ASP.NET Core DI (as it seems from the error message) and assuming that your implementation class is called TimesheetRepository
, you need to add one line to either your Program.cs
or Startup.cs
depending on the ASP.NET Core version:
// I assume you have the following line somewhere in the same file:
var builder = WebApplication.CreateBuilder(args);
...
// Line to insert
builder.Services.AddScoped<ITimesheetRepository, TimesheetRepository>();
...
// and you should have the following lines _below_ the inserted one:
var app = builder.Build();
...
app.Run();