【问题标题】:Error when trying to use WEB API on Azure App service尝试在 Azure 应用服务上使用 WEB API 时出错
【发布时间】:2018-10-27 06:36:00
【问题描述】:

我正在使用 Azure SQL 将应用程序从独立的 IIS/MS SQL 迁移到 AZURE appService,并且我启动并运行了基本应用程序。我创建并迁移了数据库,但是当我尝试迁移一个实际连接到数据库的 API 时,我收到以下错误消息,我用谷歌搜索了 o 尝试找到解决方案,但没有成功。

An unhandled exception occurred while processing the request.
InvalidOperationException: Unable to resolve service for type 'NWMPosNGTrStore.Data.ApplicationDbContext' while attempting to activate 'NWMPosNGTrStore.Controllers.OgetTillConfigObject'.
Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

在发生这种情况之前,我没有收到任何编译器警告或错误,并且我想导航站点本身可以​​正常工作,因为它不使用数据库,但是一旦我想访问数据库,应用程序就会崩溃并出现上述错误留言

API 控制器的相关部分如下

namespace NWMPosNGTrStore.Controllers
{
    [Route("api/online/till/[controller]")]
    public class OgetTillConfigObject : Controller
    {
        private ApplicationDbContext _context;

        public OgetTillConfigObject(ApplicationDbContext context)
        {
            _context = context;
        }

startup.cs 注册依赖

namespace NWMPosNGTrStore
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
 services.AddTransient<ApplicationDbContext>();

ApplicationDbContext

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using NWMPosNGTrStore.Models;

namespace NWMPosNGTrStore.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
            builder.Entity<OrderHeader>()
                   .HasKey(c => new { c.OrderNumberId, c.TillId });
            builder.Entity<OrderRow>()
                   .HasKey(c => new { c.OrderNumberRowId, c.TillId });
            builder.Entity<Payment>()
                   .HasKey(c => new { c.PaymentId, c.TillId });
            builder.Entity<TillBasicData>()
                    .HasKey(c => new { c.ItemId, c.CompanyId, c.languageCode });
            builder.Entity<MerchHierarchy>()
                    .HasKey(c => new { c.CompanyId, c.CategoryId, c.LanguageCode });
            builder.Entity<MerchHierarchyMapping>()
                    .HasKey(c => new { c.ParentId, c.ChildId, c.LevelId, c.CompanyId });
            builder.Entity<Color>()
                    .HasKey(c => new { c.CompanyId, c.ColorId, c.LanguageCode });
            builder.Entity<Size>()
                    .HasKey(c => new { c.SizeCode, c.SizeProfile, c.CompanyId });
            builder.Entity<LevelDesc>()
                    .HasKey(c => new { c.CompanyId, c.LevelId, c.LanguageCode });
            builder.Entity<LevelMapping>()
                    .HasKey(c => new { c.CompanyId, c.ParentId, c.LevelId });
            builder.Entity<Pricelist>()
                    .HasKey(c => new { c.PricelistId });
            builder.Entity<PriceListRow>()
                  .HasKey(c => new { c.PricelistId, c.ItemId });
            builder.Entity<BackEndConfig>()
                  .HasKey(c => new { c.CompanyId });
            builder.Entity<oh>()
                  .HasKey(c => new { c.BrandId, c.OrderNumber });
            builder.Entity<orp>()
                  .HasKey(c => new { c.BrandId, c.OrderNumber, c.OrderRowNumber });
            builder.Entity<pts>()
                  .HasKey(c => new { c.BrandId, c.OrderNumber, c.ExtRefNo });
            builder.Entity<ItemRef>()
                  .HasKey(c => new { c.ItemId, c.CategoryId });
            builder.Entity<WarehouseLocations>()
                  .HasKey(c => new { c.LocationId });
            builder.Entity<Bin>()
                  .HasKey(c => new { c.BinId });
            builder.Entity<InventoryQty>()
                  .HasKey(c => new { c.BinId, c.ItemId });
            builder.Entity<StorePart>()
                 .HasKey(c => new { c.Id });
            builder.Entity<Safe>()
                  .HasKey(c => new { c.SafeId });
            builder.Entity<NumberSeparator>()
                  .HasKey(c => new { c.Id });
            builder.Entity<Address>()
                  .HasKey(c => new { c.Id });
        }

        public DbSet<Brand> Brand { get; set; }
        public DbSet<Region> Region { get; set; }
        public DbSet<Receipt> Receipt { get; set; }
        public DbSet<TAXType> TAXType { get; set; }
        public DbSet<Pricelist> Pricelist { get; set; }
        public DbSet<CampaignPricelist> CampaignPricelist { get; set; }
        public DbSet<Language> Language { get; set; }
        public DbSet<Currency> Currency { get; set; }
        public DbSet<Promotion> Promotion { get; set; }
        public DbSet<Country> Country { get; set; }
        public DbSet<Area> Area { get; set; }
        public DbSet<City> City { get; set; }
        public DbSet<Store> Store { get; set; }
        public DbSet<Till> Till { get; set; }
        public DbSet<ConfigurationNode> ConfigurationNode { get; set; }
        public DbSet<Payment> Payment { get; set; }
        public DbSet<OrderRow> OrderRow { get; set; }
        public DbSet<OrderHeader> OrderHeader { get; set; }
        public DbSet<TillBasicData> TillBasicData { get; set; }
        public DbSet<MerchHierarchy> MerchHierarchy { get; set; }
        public DbSet<MerchHierarchyMapping> MerchHierarchyMapping { get; set; }
        public DbSet<Color> Color { get; set; }
        public DbSet<Size> Size { get; set; }
        public DbSet<LevelDesc> LevelDesc { get; set; }
        public DbSet<LevelMapping> LevelMapping { get; set; }
        public DbSet<PriceListRow> PriceListRow { get; set; }
        public DbSet<BackEndConfig> BackEndConfig { get; set; }
        public DbSet<oh> oh { get; set; }
        public DbSet<orp> orp { get; set; }
        public DbSet<pts> pts { get; set; }
        public DbSet<CRMPerson> CRMPerson { get; set; }
        public DbSet<ItemRef> ItemRef { get; set; }
        public DbSet<WarehouseLocations> WarehouseLocations { get; set; }
        public DbSet<Bin> Bin { get; set; }
        public DbSet<InventoryQty> InventoryQty { get; set; }
        public DbSet<StorePart> StorePart { get; set; }
        public DbSet<Safe> Safe { get; set; }
        public DbSet<NumberSeparator> NumberSeparator { get; set; }
        public DbSet<Address> Address { get; set; }
    }
}

【问题讨论】:

  • ApplicationDbContext 未注册任何 DI 容器正在使用。解析控制器时,它无法初始化未知的数据库上下文。
  • 好的,这到底是什么意思?对不起,我没明白你的意思
  • 这是一个依赖注入问题。
  • 我理解那部分,但我真的没有看到任何错误,有什么可能的修复?
  • 以下帖子可能会有所帮助:hassantariqblog.wordpress.com/2017/02/20/…

标签: c# azure asp.net-core azure-sql-database azure-web-app-service


【解决方案1】:

在 ApplicationDBContext 类中添加以下代码行解决了该问题:

protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        IConfigurationRoot Configuration = null;
        string basePath = Directory.GetCurrentDirectory();


        var builder = new ConfigurationBuilder()
            .SetBasePath(basePath)
           .AddJsonFile("appsettings.json");
        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
        options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]);
    }

【讨论】:

    猜你喜欢
    • 2022-09-29
    • 1970-01-01
    • 2021-12-07
    • 2016-04-15
    • 2017-11-22
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多