Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A suitable constructor for type 'RestDataService' could not be located

While running the .Net Core 2.0 API endpoint getting below error.

A suitable constructor for type 'RestDataService' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

 public partial class RestDataService : IRestDataService
    {
        private static HttpClient _client;
        private static AppConfiguration _configuration;
        private const short MaxRetryAttempts = 3;
        private const short TimeSpanToWait = 2;

        public RestDataService(AppConfiguration configuration)
        {
            _client = configuration.HttpClient;
            _configuration = configuration;
        }
........

And my startup class is something like this :

  // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            var config = new AppConfiguration
            {
                Environment = Configuration["environment"],
            };

            services.AddMvc().AddJsonOptions(o => o.SerializerSettings.NullValueHandling = NullValueHandling.Include);
            services.AddMemoryCache();
            services.AddCors();
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
                services.AddSingleton(Configuration);
                services.AddSingleton(config);
            services.AddLogging();
            services.AddTransient<IRestDataService, RestDataService>();

services.AddHttpClient<IRestDataService, RestDataService>()
                .AddPolicyHandler(request => request.Method == HttpMethod.Get ? retryPolicy : noOp);

Any suggestions, to get rid of this? constructor is already public and all the parameters are registered in startup file

like image 961
mayank gupta Avatar asked May 11 '19 04:05

mayank gupta


People also ask

Why a suitable constructor for'restdataservice'could not be located?

A suitable constructor for type 'RestDataService' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

Why is restdataservice getting below error in API endpoint?

Bookmark this question. Show activity on this post. While running the .Net Core 2.0 API endpoint getting below error. A suitable constructor for type 'RestDataService' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

Why do I receive invalidoperationexception when using constructor?

The error I receive is below: InvalidOperationException: A suitable constructor for type 'MyApp.ViewComponents.WidgetViewComponent' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.

Is there a suitable constructor for cultureinfo?

:/ {System.InvalidOperationException: No suitable constructor found for entity type 'CultureInfo'.


2 Answers

I received the error "A suitable constructor for type '<type>' could not be located." after accidentally generating a protected constructor instead of public constructor. Marking it back to public fixed it.

like image 85
Cryptc Avatar answered Nov 02 '22 06:11

Cryptc


For AddHttpClient, you need to provide HttpClient parameter for RestDataService. And, you need to register AppConfiguration.

  1. RestDataService

    public class RestDataService: IRestDataService
    {
        private static HttpClient _client;
        private static AppConfiguration _configuration;
        private const short MaxRetryAttempts = 3;
        private const short TimeSpanToWait = 2;
    
        public RestDataService(AppConfiguration configuration
            , HttpClient client)
        {
            _client = configuration.HttpClient;
            _configuration = configuration;
        }
    }
    
  2. Startup.cs

    var config = new AppConfiguration
    {
        Environment = Configuration["environment"],
    };
    
    services.AddSingleton(typeof(AppConfiguration), config);
    services.AddHttpClient<IRestDataService, RestDataService>();
    
like image 26
Edward Avatar answered Nov 02 '22 06:11

Edward