Home > Enterprise >  .NET 7 Error CS0118 'UserService' is a namespace but is used like a type
.NET 7 Error CS0118 'UserService' is a namespace but is used like a type

Time:12-03

I have my services under a folder called Services in the project and I have grouped the User related parts in a folder called UserService. And under that folder, I have an Interface called IUserService.cs and a class called UserService.cs which contain basic user authentication methods.

Here is a picture of the folder structure:

Here is a picture of the folder structure

To register this User service to dependency injection I have used an extension method class called RegisterServices.cs.

And this is the error I get when i try to register the user service using the below code: Severity Code Description Project File Line Suppression State Error CS0118 'UserService' is a namespace but is used like a type Infinium.API D:\Infinium Projects\Infinium\Infinium.API\Services\ServiceRegister.cs 16 Active

Below is the code of RegisterServices.cs

using Infinium.API.Authorization;
using Infinium.API.DataManager;
using Infinium.API.Services.UserService;

namespace Infinium.API.Services
{
    public static class ServiceRegister
    {
        public static void RegisterServices(this IServiceCollection services)
        {
            // vNext DB
            services.AddSingleton<IDataAccessor, DataAccessor>();

            //services.Configure<AppSettings>(getser)
            services.AddScoped<IJwtUtils, JwtUtils>();
            services.AddScoped<IUserService, UserService>();

        }
    }
}

I have added the RegisterServices extension method class to Program.cs as follows;

using Infinium.API.Authorization;
using Infinium.API.Helpers;
using Infinium.API.Services;
using MaxRAV.API.Helpers;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddCors(options =>
{
    options.AddPolicy(name: "AllowedCorsOrigins",
builder =>
{
    builder
.SetIsOriginAllowed((_) => true)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// configure strongly typed settings object
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));

builder.Services.RegisterServices();

builder.Host.UseSerilog((ctx, lc) => lc
    .WriteTo.Console()
    .WriteTo.Seq("http://localhost:5341") // comment if not configired
    );

var app = builder.Build();

app.UseCors("AllowedCorsOrigins");


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

// global error handler
app.UseMiddleware<ErrorHandlerMiddleware>();

// custom jwt auth middleware
app.UseMiddleware<JwtMiddleware>();

//app.UseAuthorization();

app.MapControllers();

app.Run();

The same code used to work with .NET 5 and in .NET 6 sometimes this error was thrown. I assume that the folder name conflicts with the class name, but I am not sure why it doesn't happen all the time. Maybe the solution is to rename the folder?

CodePudding user response:

There are multiple ways to handle this, e.g. use fully qualified type names, like

services.AddScoped<Infinium.API.Services.UserService.IUserService, 
                   Infinium.API.Services.UserService.UserService>();

Namespace names conflicting with class names sometimes can cause this kind of issues.

Another idea would be to maintain a separation between these, so that if your namespace ends with UserService, the class has a different name. An advice would be to have the namespace ending with a plural version of whatever you have, like

Infinium.API.Services.UserServices

and the class name in the namespace is

UserService
  • Related