mjmoschella
mjmoschella

Reputation: 1

Program.cs Compiler Error CS0311 With line builder.Services.AddScoped<IModelBuilder<T>, AdminModelBuilder>(); in vs 2022 .netcore 6

In visual studio .netcore 6, my Program.cs file I want to register a builder class to use in my application. The line

builder.Services.AddScoped<IModelBuilder<T>, AdminModelBuilder>(); 

gives me a Compiler Error

Error CS0311 The type 'WorkbenchAPI.Builders.AdminModelBuilder' cannot be used as type parameter 'TImplementation' in the generic type or method 'ServiceCollectionServiceExtensions.AddScoped<TService, TImplementation>(IServiceCollection)'. There is no implicit reference conversion from 'WorkbenchAPI.Builders.AdminModelBuilder' to 'WorkbenchAPI.Builders.IModelBuilder'. and Error CS0311 The type 'WorkbenchAPI.Builders.AdminModelBuilder' cannot be used as type parameter 'TImplementation' in the generic type or method 'ServiceCollectionServiceExtensions.AddScoped<TService, TImplementation>(IServiceCollection)'. There is no implicit reference conversion from 'WorkbenchAPI.Builders.AdminModelBuilder' to 'WorkbenchAPI.Builders.IModelBuilder'.

and also Error CS0246 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

This code did work fine in vs 2015.net.

How can I get this to compile so I can use it in my application. Below is the IModelBuilder.cs interface and AdminModelBuilder.cs file that uses the interface and finally the model AdminModel.cs.

IModelBuilder.cs

    namespace MyApplication.Builders
        {
            public interface IModelBuilder<T>
            {
                IModelBuilder<T> WithId(int id);
                IModelBuilder<T> WithModel(T model);
                T Build();
            }
        }

AdminModelBuilder.cs

    namespace MyApplication.Builders
    {
        public class AdminModelBuilder : IModelBuilder<AdminModel>
        {

            public AdminModelBuilder()
            {
           .
       .    
       .
           }

        public IModelBuilder<AdminModel> WithId(int id)
        {
           .
       .    
       .
        }

        public IModelBuilder<AdminModel> WithModel(AdminModel model)
        {
           .
       .    
       .
        }

        public AdminModel Build()
        {
           .
       .    
       .

        }
       }
   }
AdminModel.cs
    namespace MyApplication.Data.ViewModels
    {
    public class AdminModel
    {
        public int param1 { get; set; }
        public int param2 { get; set; }
        public int param3 { get; set; }
    }
   }

This did work with .net vs 2015, I tried replacing the T with AmdinModel and got same error.

builder.Services.AddScoped<IModelBuilder<AdminModel>, AdminModelBuilder>(); 

Upvotes: 0

Views: 372

Answers (1)

tmaj
tmaj

Reputation: 35075

You have to register using the actual type IModelBuilder<AdminModel>, not the generic variant IModelBuilder<T>.

Instead of

builder.Services.AddScoped<IModelBuilder<T>, AdminModelBuilder>(); 

please use

builder.Services.AddScoped<IModelBuilder<AdminModel>, AdminModelBuilder>(); 

Upvotes: 1

Related Questions