我试图了解AsyncLocal应该如何在.Net 4.6中运行.我将一些数据放入AsyncLocal ......但是当ThreadContext更改时,它被设置为null.我正在使用AsyncLocal的全部原因是在等待异步操作时尝试跨线程保留/缓存此值.知道为什么会特别调用它并在上下文发生变化时设置为null吗?关于AsyncLocal的文档非常稀疏......也许我已经把它弄错了.
public class RequestContextProvider : IRequestContextProvider
{
private static readonly AsyncLocal<IRequestContext> _requestContext = new AsyncLocal<IRequestContext>(ValueChangedHandler);
private static void ValueChangedHandler(AsyncLocalValueChangedArgs<IRequestContext> asyncLocalValueChangedArgs)
{
**//This is just here to observe changes...when I await a call that
//causes control to pass to a different thread..this is called
//with a current value of null.**
var previousValue = asyncLocalValueChangedArgs.PreviousValue;
var currentValue = asyncLocalValueChangedArgs.CurrentValue;
var contextChanged = asyncLocalValueChangedArgs.ThreadContextChanged;
}
public void SetContext(IRequestContext requestContext)
{
_requestContext.Value = requestContext;
}
public IRequestContext GetContext()
{
return _requestContext.Value;
} …Run Code Online (Sandbox Code Playgroud) 这很容易获得旧.NET中的所有可用类型(例如某些接口),但我无法在新的CoreCLR中找到如何做到这一点的方法.
我想要做的是拥有像GetRepository这样的函数,它应该查找IRepository的现有实现并返回该类型的新实例.实施将位于不同的项目中.
所以,在.NET中我可以使用这样的东西:
AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())
Run Code Online (Sandbox Code Playgroud)
我现在对CoreCLR唯一的解决方案是:
public T GetRepository<T>()
{
foreach (Type type in typeof(T).GetTypeInfo().Assembly.GetTypes())
if (typeof(T).IsAssignableFrom(type) && type.GetTypeInfo().IsClass)
return (T)Activator.CreateInstance(type);
return default(T);
}
Run Code Online (Sandbox Code Playgroud)
但它只有在接口和实现位于同一个程序集中时才有效(这不是我的情况).
谢谢!
我刚刚开始在VS2015中使用DNX 1.0.0-rc1-update1.我的第一个应用程序是"控制台应用程序(包)"项目.一切正常,除了NLog日志记录.我怀疑是因为NLog.config没有被复制到输出文件夹.如何通过project.json告诉VS将此文件复制到输出文件夹?
我已经尝试添加这样的'资源'变量,但它不起作用:
project.json
...
"resource":"NLog.config",
...
Run Code Online (Sandbox Code Playgroud)
编辑1: 我正在使用dnx451,因此兼容性不是问题.
编辑2: 我将以下内容添加到project.json中
"scripts": {
"postbuild": [
"%project:Directory%/../scripts/copy_resources.bat \\\"%project:Directory%\\\" \\\"%project:Directory%/../artifacts/bin/%project:Name%/%project:Version%/dnx451\\\""
]
}
Run Code Online (Sandbox Code Playgroud)
copy_resources.bat
echo "Running script" >> C:\logs\log.txt
echo %1 >> C:\logs\log.txt
echo %2 >> C:\logs\log.txt
xcopy %1\NLog.config %2 /U /Y
Run Code Online (Sandbox Code Playgroud)
VS的输出窗口中没有任何内容表明脚本实际上已运行.此外,log.txt为空.
如何调试构建过程?
我一直在尝试重新创建ValidateAntiForgeryToken的Ajax版本 - 有许多关于如何为以前版本的MVC执行此操作的博客文章,但是对于最新的MVC 6,没有任何代码是相关的.不过,我要追求的核心原则是验证Cookie和Header __RequestVerificationToken,而不是将Cookie与表单值进行比较.我使用的是MVC 6.0.0-rc1-final,dnx451框架,所有的Microsoft.Extensions库都是1.0.0-rc1-final.
我最初的想法是继承ValidateAntiForgeryTokenAttribute,但是看一下源代码,我需要返回自己的授权过滤器实现来让它看看标题.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ValidateAjaxAntiForgeryTokenAttribute : Attribute, IFilterFactory, IFilterMetadata, IOrderedFilter
{
public int Order { get; set; }
public bool IsReusable => true;
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
return serviceProvider.GetRequiredService<ValidateAjaxAntiforgeryTokenAuthorizationFilter>();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,我随后制作了自己的ValidateAntiforgeryTokenAuthorizationFilter版本
public class ValidateAjaxAntiforgeryTokenAuthorizationFilter : IAsyncAuthorizationFilter, IAntiforgeryPolicy
{
private readonly IAntiforgery _antiforgery;
private readonly ILogger _logger;
public ValidateAjaxAntiforgeryTokenAuthorizationFilter(IAntiforgery antiforgery, ILoggerFactory loggerFactory)
{
if (antiforgery == null)
{
throw new ArgumentNullException(nameof(antiforgery));
} …Run Code Online (Sandbox Code Playgroud) 我想对应用程序的性能进行抽样,但尝试使用性能向导启动新的性能分析会话时,各种工具(如CPU使用率和应用程序时间轴)不可用:
我正在使用Visual Studio 2015 Professional.该应用程序使用的是ASP.NET 5 RC1,并且是从Kestrel(而不是IIS)运行的.
为什么会发生这种情况?
我在使用dnx/k版本1.0.0-beta3设置的ASP.NET 5项目上工作.最近我干净安装了我的操作系统和VS并丢失了beta3软件包,我只安装了beta4.是否可以安装特定版本的dnx?我已经尝试过了:
dnvm install 1.0.0-beta3 等等
通常我们会看到以下目标project.json:
"frameworks": {
"net45": {},
"dnx451": {},
"dnxcore50": { }
}
Run Code Online (Sandbox Code Playgroud)
dnxcore50将是项目代码的唯一可移植版本,并dnx451实际上针对.Net 4.5.1 mscorlib等...
现在,如果我添加另一个被调用的目标dnx50,这将创建一个有效的输出并且工作正常.
dnx451和dnx50之间的区别在于它引用了不同的.Net程序集dll
例如mscorlib.dll:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\mscorlib.dllC:\Windows\Microsoft.NET\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll本质上是.net 4.6版本.现在的问题是:
dnx50例如,为自定义库创建目标是否有意义?基本上以.net 4.6为目标,您需要dnxcore50便携式设备没有的某些功能吗?
或者dnx451目标实际上是足够的,如果我不需要更新的.net版本(4.5.2,4.6)中的特定功能,如果它安装在系统上,那么该目标将使用.net 4.6而且我我只是针对我的项目所需的最低版本?
这是否意味着同时拥有dnx451和dnx50目标实际上会产生相同的输出,对吧?
尝试将迁移添加到位于ASP.NET 5类库中的EF7模型.当运行dnx . ef migration add mymigration失败时会有不同的结果,具体取决于我运行它的项目.
如果我在主项目的文件夹中运行它,它找不到DbContext,这是有道理的,因为DbContext它在共享项目中,而ef命令可能不关心依赖项.
如果我在共享项目的文件夹中运行它,它就无法访问startup.cs中指定的连接字符串.我从这样的问题收集这是它从共享的项目工作,如果你指定要在连接字符串OnConfiguring的方法DbContext,但我真的想保持这个代码从配置独立.
我在EF7存储库中遇到了一些问题日志,提到他们实现了用于指定项目和上下文的命令行选项,但没有示例,我无法通过查看提交历史记录中的源代码来弄清楚如何使用它.
ef-migrations entity-framework-core visual-studio-2015 dnx asp.net-core
据我所知,目标框架dnx451和net451两者都使用桌面.NET Framework 4.5.1.dnx451特别适用于DNX运行时应用程序并支持ASP.NET 5.
如果我们有一个带有ASP.NET 5项目和多个类库的解决方案,那么它们是否都要定位dnx451或只需要定位Web项目dnx451?类库是否只是针对目标net451?
我有一个场景,我使用共享代码库运行UWP客户端应用程序,UWP IOT应用程序和.NET Core应用程序.在.NET Core RC1中,我构建了一个类库(Package),并使用"dotnet5.4"作为该库的基础框架.
使用"生成构建输出"我可以从.NET Core应用程序(控制台)引用创建的nuget包并使用解决方法(从%local%.dnx - >%local%.nuget复制包)UWP应用程序能够参考并使用该包.
现在在RC2中,事情发生了一些变化,我再次能够使用已升级的库(在项目文件中升级的工具,更改为project.json,netstandard1.4(因为1.5根据此不能与UAP10一起使用))完美地使用.NET Core控制台应用程序.
对于UWP我无法添加库,因为我得到了几十个臭名昭着的
"[...] provides a compile-time reference assembly [...] but there is no run-time assembly compatible with [...]"
Run Code Online (Sandbox Code Playgroud)
错误.
经过一些环顾四周,我试图找出问题并发现我甚至无法添加对System.IO.FileSystem.Watcher的引用,原因如下:
System.IO.FileSystem.Watcher 4.0.0-rc2-24027 provides a compile-time reference assembly for System.IO.FileSystem.Watcher on UAP,Version=v10.0, but there is no run-time assembly compatible with win10-arm-aot.
Some packages are not compatible with UAP,Version=v10.0 (win10-x64-aot).
System.IO.FileSystem.Watcher 4.0.0-rc2-24027 provides a compile-time reference assembly for System.IO.FileSystem.Watcher on UAP,Version=v10.0, but there is no run-time assembly compatible with win10-x64. …Run Code Online (Sandbox Code Playgroud) dnx ×10
asp.net-core ×5
c# ×5
.net ×3
ajax ×1
asp.net5 ×1
async-await ×1
coreclr ×1
dnvm ×1
project.json ×1
uwp ×1