我需要AutoFac
在ASP core 3.0中使用它
当我在启动时使用此代码时:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddControllers();
return services.BuildAutofacServiceProvider();
}
Run Code Online (Sandbox Code Playgroud)
它向我显示此错误:
'不支持返回System.IServiceProvider的ConfigureServices。
然后我通过以下方式更改program.cs:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Run Code Online (Sandbox Code Playgroud)
但这并没有解决。
这是BuildAutofacServiceProvider()
代码:
public static IServiceProvider BuildAutofacServiceProvider(this IServiceCollection services)
{
var ContainerBuilder = new ContainerBuilder();
ContainerBuilder.Populate(services);
ContainerBuilder.AddService();
var container = ContainerBuilder.Build();
return new AutofacServiceProvider(container);
}
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
c# dependency-injection autofac asp.net-core asp.net-core-3.0
我需要填写appsetting.json
ASP.NET Core 2.2 中的以下信息。
"SiteSetting": {
"JwtSetting": {
"SecretKey": "LongerThan-16Char-SecretKey",
"Issuer": "MyWebsite",
"Audience": "MyWebsite",
"NotBeforeMinutes": "0",
"ExpirationMinutes": "60"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的SiteSetting
课:
public class SiteSetting
{
public JwtSetting JwtSettings { get; set; }
}
public class JwtSetting
{
public string SecretKey { get; set; }
public string Issuer { get; set; }
public string Audience { get; set; }
public int NotBeforeMinutes { get; set; }
public int ExpirationMinutes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在使用这段代码来Startup.cs
填充 …
如果文件是图像,我需要调整文件上传的大小。
我写了调整大小的扩展:
public static Image ResizeImage(this Image image, int width, int height)
{
var res = new Bitmap(width, height);
using (var graphic = Graphics.FromImage(res))
{
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, width, height);
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
这是 Upload Action :
[HttpPost("UploadNewsPic"), DisableRequestSizeLimit]
public IActionResult UploadNewsPic(IFormFile file)
{
if (file.IsImage())
{
}
try
{
if (file.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(_applicationRoot.UploadNewPath(), file.Name);
using (var stream …
Run Code Online (Sandbox Code Playgroud) 我需要在 ASP.NET Core 3.0 中使用代码优先创建一个数据库
这是DbContext
:
public class TrelloContext : DbContext
{
public TrelloContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.AddDbSet<IEntity>(typeof(IEntity).Assembly);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(IType).Assembly);
}
}
Run Code Online (Sandbox Code Playgroud)
这是启动:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddControllersAsServices();
services.AddDbContext<TrelloContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
services.Injection();
}
Run Code Online (Sandbox Code Playgroud)
这是我的连接字符串:
"ConnectionStrings": {
"SqlServer": "Data Source=.;Initial Catalog=TrelloDB;Trusted_Connection=True;Trusted_Connection=True;"
}
Run Code Online (Sandbox Code Playgroud)
当我使用这个时add-migration initial
,我收到此错误:
值不能为空。(参数“键”)
有什么问题?我该如何解决这个问题?
我Stored Procedure
在Sql Server中创建了这个:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_GetUsrsofRole] (@RoleId int)
AS
;WITH CTE_UsersInRole AS
(
SELECT
U.Id AS [UserId], U.Name AS [UserName] , U.Family AS [UserFamily] , R.Id AS[RoleID],R.Name AS [RoleName]
FROM AspNetUserRoles AU
INNER JOIN AspNetRoles R ON R.Id=AU.RoleId
INNER JOIN AspNetUsers U ON U.ID=AU.UserId
)
SELECT
C.RoleID,C.RoleName,C.UserFamily,C.UserId,C.UserName
FROM CTE_UsersInRole C
WHERE C.RoleID=@RoleId
GO
Run Code Online (Sandbox Code Playgroud)
这是我的视图模型:
public class UserInRoleVM
{
public string UserName { get; set; }
public string UserFamily { get; …
Run Code Online (Sandbox Code Playgroud) 当我使用 Dapper 插入、更新或删除实体时,我需要返回id
实体的。
我正在使用这段代码:
var role = await roleConnection.ExecuteAsync("UPDATE Role SET IsDelete = @isDelete, RoleName = @roleName, SecurityStamp = @securityStamp WHERE Role.Id = @id SELECT SELECT CAST(SCOPE_IDENTITY() AS INT)"
, new
{
isDelete = request.IsDelete = false,
roleName = request.RoleName,
securityStamp = Guid.NewGuid(),
id = request.Id
});
Run Code Online (Sandbox Code Playgroud)
但它没有向我显示任何内容。
我怎样才能做到这一点?