Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Some services are not able to be constructed Error is coming in program.cs please help to fix

$
0
0

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType in program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace MyWeather
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

My code is given below

Interface

using MyWeather.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyWeather.Repository.IRepository
{
    public interface IWeatherRepository
    {
        Task<IEnumerable<Weather>> GetWeatherAsync();
    }
}

Repo

using MyWeather.Models;
using MyWeather.Repository.IRepository;
using MyWeather.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace MyWeather.Repository
{
    public class WeatherRepository : IWeatherRepository
    {
        private readonly IHttpClientFactory _clientFactory;
        public WeatherRepository(IHttpClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
        }
        public async Task<IEnumerable<Weather>> GetWeatherAsync()
        {
            var url = SD.APILocationUrl;
            var request = new HttpRequestMessage(HttpMethod.Get, url);
            var client = _clientFactory.CreateClient();
            HttpResponseMessage response = await client.SendAsync(request);
            IEnumerable<Weather> weather = new List<Weather>();
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var jsonString = await response.Content.ReadAsStringAsync();
                return JsonConvert.DeserializeObject<IEnumerable<Weather>>(jsonString);
            }
            return null;
        }

    }
}

startup.cs
public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddScoped<IUnitOfWork,UnitOfWork>();
            services.AddScoped<IWeatherRepository, WeatherRepository>();
            services.AddControllersWithViews().AddRazorRuntimeCompilation();
            services.AddRazorPages();
        }

Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyWeather.Models;
using MyWeather.Repository.IRepository;
using Microsoft.AspNetCore.Mvc;

namespace MyWeather.Areas.Customer.Controllers
{
    [Area("Customer")]
    public class ForcastController : Controller
    {
        private readonly IWeatherRepository _weatherRepository;
        public Weather Weather { get; set; }
        public ForcastController(IWeatherRepository weatherRepository)
        {
            _weatherRepository = weatherRepository;
        }
        public async Task<IActionResult> Index()
        {
            var Weather = new List<Weather>();
            var weatherList = await _weatherRepository.GetWeatherAsync();
            return View();
        }
    }
}


Weather.Repository.IRepository.IWeatherRepository Lifetime: Scoped ImplementationType:

My code


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>