1

I created a Service class in .Net 6. How to call it in .net 6?

.Net 6

using API.Data;
using API.Interfaces;
using API.Services;
using Microsoft.EntityFrameworkCore;

namespace API.Extensions
{
    public static class ApplicationServiceExtensions
    {
        public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration config, WebApplicationBuilder builder)
        {
            builder.Services.AddDbContext<DataContext>(options =>
            {
                options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
            });
            builder.Services.AddScoped<ITokenService, TokenService>();
        }
    }
}

I was calling it as given below.

.net core 3

public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationServices(_config);
}

When I try to call like this. It shows an error Can anyone suggest the right way to call it?

Program.cs .net 6

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddApplicationServices(_config);
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Ahsan Khan
  • 51
  • 3
  • 2
    The method `AddApplicationServices` you show up first has a third (second explicit) parameter, that you don't seem to pass in .NET Core 3 as well. So is this really the actual code? Other than that, have you tried `builder.Services.AddApplicationServices(builder.Configuration)`? – Christian.K Aug 05 '22 at 12:39
  • It shows an error and suggestion to add like this. builder.Services.AddApplicationServices(builder.Configuration, builder); Is it ok? – Ahsan Khan Aug 05 '22 at 13:41
  • @Christian.K is this technically correct? – Ahsan Khan Aug 05 '22 at 14:22
  • That's what I meant with your code differs from your example. Basically, the ".net core 3" version could have never worked the way you show it (not having the second "builder" parameter). The AddApplicationServices(builder.Configure, builder) "looks" OK. But you really have to show consistent code in your question to be sure. – Christian.K Aug 07 '22 at 16:17

0 Answers0