我开始将我的asp.net核心RC1项目转换为RC2,并面临现在IHttpContextAccessor无法解决的问题.
为简单起见,我使用Visual Studio模板创建了新的ASP.NET RC2项目ASP.NET Core Web Application (.Net Framework).比我添加了HomeController的构造函数,为我创建了哪个模板.
public HomeController(IHttpContextAccessor accessor)
{
}
Run Code Online (Sandbox Code Playgroud)
在我开始申请后,我收到下一个错误:
InvalidOperationException:尝试激活"TestNewCore.Controllers.HomeController"时,无法解析类型"Microsoft.AspNetCore.Http.IHttpContextAccessor"的服务.Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,Boolean isDefaultParameterRequired)
在我的实际应用中,我需要解决IHttpContextAccessor我自己的服务类获得访问_contextAccessor.HttpContext.Authentication和_contextAccessor.HttpContext.User.在RC1中,Everething运行良好.那么怎么能想到RC2呢?
我今天更新了一个项目到ASP.NET Core 2,我收到以下错误:
不能从singleton IActiveUsersService使用作用域服务IMongoDbContext
我有以下注册:
services.AddSingleton<IActiveUsersService, ActiveUsersService>();
services.AddScoped<IMongoDbContext, MongoDbContext>();
services.AddSingleton(option =>
{
var client = new MongoClient(MongoConnectionString.Settings);
return client.GetDatabase(MongoConnectionString.Database);
})
public class MongoDbContext : IMongoDbContext
{
private readonly IMongoDatabase _database;
public MongoDbContext(IMongoDatabase database)
{
_database = database;
}
public IMongoCollection<T> GetCollection<T>() where T : Entity, new()
{
return _database.GetCollection<T>(new T().CollectionName);
}
}
public class IActiveUsersService: ActiveUsersService
{
public IActiveUsersService(IMongoDbContext mongoDbContext)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
为什么DI无法使用该服务?一切都适用于ASP.NET Core 1.1.
.Net Core 1.0.0 - SDK Preview 2(x64)
.Net Core 1.0.0 - VS"15"预览版2(x64)
.Net Core 1.0.0 - 运行时(x64)
因此,我们将RC1应用更新为上述最新版本.经过几个小时的切换参考,它正在运行.但是,登录时(AccountController/Login),我收到错误:
public class AccountController : BaseController
{
public UserManager<ApplicationUser> UserManager { get; private set; }
public SignInManager<ApplicationUser> SignInManager { get; private set; }
private readonly IEmailSender EmailSender;
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, IEmailSender emailSender)
{
UserManager = userManager;
SignInManager = signInManager;
EmailSender = emailSender;
}
// GET: /Account/Login
[HttpGet]
[AllowAnonymous]
public IActionResult Login(string returnUrl = null)
{
ViewBag.ReturnUrl = returnUrl;
return View();
} …Run Code Online (Sandbox Code Playgroud) 在C#中,如果我创建一个没有命名空间的类,那么在尝试实例化类时我会使用什么命名空间?
例如,假设main是......
namespace NamespaceTests
{
class Program
{
static void Main(string[] args)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
...并假设我的无命名空间类是......
public class test
{
public string SayHello()
{
return "Hello World!";
}
}
Run Code Online (Sandbox Code Playgroud)
...并假设我有一个相同名称的另一个类,但具有默认命名空间...
namespace NamespaceTests
{
public class test
{
public string SayHello()
{
return "Hello Moon...";
}
}
}
Run Code Online (Sandbox Code Playgroud)
...我如何修改main以包含无名称空间类的实例,并调用'SayHello'来检索消息"Hello World!"?具体来说,我如何完全限定类'test'的无名称空间实例,特别是考虑到我可能有另一个类也称为'test'但具有命名空间,所以我需要区分...
在ASP.NET Core应用程序中,我可以像这样通过DI注册DbContext
services.AddDbContext<Models.ShellDbContext>(options => options.UseNpgsql(connection));
Run Code Online (Sandbox Code Playgroud)
而知道它的生命周期是什么呢?
从这里https://github.com/aspnet/EntityFramework/blob/f33b76c0a070d08a191d67c09650f52c26e34052/src/Microsoft.EntityFrameworkCore/EntityFrameworkServiceCollectionExtensions.cs#L140看起来它被配置为作用域,这意味着的DbContext实例在每次请求创建.
所以问题的第一部分是:它是真的,如果是,那么它的代价是多少?
第二部分是:如果我创建一个消耗DbContext的服务,并且打算由控制器使用,并且将有一个API来管理DB中的某些实体,它是否应该注册为Scoped?
我想像这样在我的ASP.NET Core WebApi中添加一个处理时中间件
public class ProcessingTimeMiddleware
{
private readonly RequestDelegate _next;
public ProcessingTimeMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var watch = new Stopwatch();
watch.Start();
await _next(context);
context.Response.Headers.Add("X-Processing-Time-Milliseconds", new[] { watch.ElapsedMilliseconds.ToString() });
}
}
Run Code Online (Sandbox Code Playgroud)
但这样做会引发异常
System.InvalidOperationException: Headers are readonly, reponse has already started.
Run Code Online (Sandbox Code Playgroud)
如何在响应中添加标头?
谁会知道我缺少什么,为什么那些asp-controller和asp-action标签对我不起作用.我正在ASP.NET MVC Core中实现一个项目.
这不会触发:
<a asp-controller="App" asp-action="Trips" class="btn btn-lg btn-success">Go to Trips</a>
Run Code Online (Sandbox Code Playgroud)
剃刀工作正常:
@Html.ActionLink("Go to Trips", "Trips", "App", new object { }, new { @class = "btn btn-lg btn-success" })
Run Code Online (Sandbox Code Playgroud)
我是否需要配置一些服务才能正常工作.而且,哪种方式更受欢迎?Razor很受MVC的欢迎,那些asp标签是一种新的,更好的方式吗?
有没有办法在IIS下运行时调试ASP.NET Core Web应用程序?
我在IIS下运行,因为我需要它在端口443上通过SSL提供.
使用时是否有关机功能Microsoft.AspNet.Server.Kestrel?ASP.NET Core(以前的ASP.NET vNext)显然有一个启动序列,但没有提到关闭序列以及如何处理干净关闭.
我最近向我的主机发布了我的ASP.NET核心应用程序.我正在点击HTTP错误500.19.
IIS 8.5说问题是: -
"无法添加'add'类型的重复集合条目,并将唯一键属性'name'设置为'aspNetCore'"
它还突出显示了我的system.webServer配置中的这个关键添加行: -
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"
</handlers>
Run Code Online (Sandbox Code Playgroud)
我不确定该怎么做.它看起来好像有一个重复的实例,所以我尝试重命名这个,但它仍然要求再次添加它?
这是我的web.config: -
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
-->
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
<system.net>
<defaultProxy useDefaultCredentials="true" >
</defaultProxy>
</system.net>
</configuration>
Run Code Online (Sandbox Code Playgroud)