ASP.Net核心实体MVC-编译器在执行ConfigureServices-Method

问题描述

该想法是在有人登录或创建客户资料之前为购物袋对象提供唯一的ID。这样,访客就可以在购物袋中放一些物品,调整数量,然后在让他/她自己知道之前替换它们。

它曾经通过

services.AddScoped<ShoppingBag>(sp => ShoppingBag.GetShoppingBag(sp));

但是现在,编译器拒绝执行此方法。 (基本上在它上面运行,而无需在调试时进入它。)

我不明白他为什么不再执行该方法。

在start.cs

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using BikeShop.DAL;
    using BikeShop.Models;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.AspNetCore.Http;
    using BikeShop.DAL.Repositories;
    
    namespace BikeShop
    {
        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<BikeShopContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("defaultconnection")); });
    
                services.AddScoped<IProductRepository,ProductRepository>();
                services.AddScoped<ICustomerRepository,CustomerRepository>();
                services.AddScoped<IShoppingBagRepository,ShoppingBagRepository>();
                services.AddScoped<IShoppingItemRepository,ShoppingItemRepository>();
    
                services.AddScoped<ShoppingBag>(sp => ShoppingBag.GetShoppingBag(sp));
                services.AddHttpContextAccessor();
                services.AddSession();
    
                services.AddControllersWithViews();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
            {
                    if (env.IsDevelopment())
                    {
                        app.UseDeveloperExceptionPage();
                    }
                    else
                    {
                        app.UseExceptionHandler("/Home/Error");
                        // The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
                        app.UseHsts();
                    }
                    app.UseHttpsRedirection();
                    app.UseStaticFiles();
    
                    app.UseSession(); // middleware for shoppingBagID created during a session
    
                    app.UseRouting();
    
                    app.UseAuthorization();
    
                    app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllerRoute(
                            name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
                    }
                );
            }
        }
    }

在ShoppingBag.cs​​

public static ShoppingBag GetShoppingBag (IServiceProvider services)
        {
            ISession session = services.GetRequiredService<IHttpContextAccessor>()?
                .HttpContext.Session;

            var context = services.GetService<BikeShopContext>();

            string cartId = session.GetString("CartId") ?? Guid.NewGuid().ToString();

            session.SetString("CartId",cartId);
            ShoppingBag currentShoppingBag = new ShoppingBag(context) { ShoppingBag_Id = cartId,Date = DateTime.UtcNow };
            context.ShoppingBags.Add(currentShoppingBag);
            context.SaveChanges();

            return new ShoppingBag(context) { ShoppingBag_Id = cartId,Date = DateTime.UtcNow };
        }

完整的项目文件在这里:

[https://github.com/qqtf/AspNetCoreAssignments/tree/Debugging2] [1]

启动就足够了,因为GUID应该在启动过程中完成的。

De NuGet-Packages:

DAL项目:

AspNetCore.Http
AspNetCore.Http.Extensions
Entity.frameworkcore.sqlserver
Entity.frameworkcore.tools
system.componentmodel-annotations
xamarin-forms

前端:

Entity.frameworkcore.sqlserver
Entity.frameworkcore.tools
Entity.frameworkcore.sqlite
visualstudio.web.codegeneration.design
x.pagedlist.mvc.core

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...