尝试在Azure App服务上使用WEB API时出错

时间:2018-05-17 05:25:29

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

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

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; }
    }
}

1 个答案:

答案 0 :(得分:0)

在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"]);
    }