How to perform calculation in ASP.net Core where the calculation method is present in repository and need to call in controller for testing

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%2FL9KuMlGxtZS0jkQY5wu3raX%2BfcdeJaexR64LKlxAZEevFg2yi2OgH33rxoNy09Kip%2B8k2%2BGALHBwComubsMgU86lD%2BXqDcvH3FV4WbSHu7Kk%2FAehnMuapbWdQPT76kLHcwtcaVtMw9JdnbxFi6gtxusJ%2BVX5zoav0%2BT
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.

2

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();

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật