鉴于以下内容:
SELECT ISNULL('XY' + NULL, 'ABCDEFGHIJ') -- Outputs ABC (Why?)
SELECT COALESCE('XY' + NULL, 'ABCDEFGHIJ') -- Outputs ABCDEFGHIJ
Run Code Online (Sandbox Code Playgroud)
为什么这些陈述会返回不同的结果?
根据此答案,IOptionsMonitor将以单例形式注册在DI容器中,并且能够通过OnChange事件订阅来检测更改。它有一个CurrentValue属性。
另一方面,通过读取每个请求的最后一个选项IOptionsSnapshot被注册为作用域,并且还具有更改检测功能,但是它没有OnChange事件。它有一个Value属性。
例如,将两者都注入到视图中将为我们提供完全相同的行为:
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Options;
using UsingOptionsSample.Models;
using UsingOptionsSample.Services;
namespace UsingOptionsSample.Pages
{
public class MyOptions
{
public MyOptions()
{
// Set default value.
Option1 = "value1_from_ctor";
}
public string Option1 { get; set; }
public int Option2 { get; set; } = 5;
}
public class OptionsTestModel : PageModel
{
private readonly MyOptions _snapshotOptions;
private readonly MyOptions _monitorOptions;
public OptionsTestModel( …Run Code Online (Sandbox Code Playgroud) dependency-injection interface options object-lifetime asp.net-core
鉴于程序:
using System;
using System.IO;
namespace fsw_bug_poc
{
class Program
{
private static FileSystemWatcher _fileSystemWatcher;
static void Main(string[] args)
{
_fileSystemWatcher = new FileSystemWatcher("Watched", "*.*");
_fileSystemWatcher.Changed += Notify;
_fileSystemWatcher.Created += Notify;
_fileSystemWatcher.Deleted += Notify;
_fileSystemWatcher.Renamed += Notify;
_fileSystemWatcher.IncludeSubdirectories = true;
_fileSystemWatcher.EnableRaisingEvents = true;
Console.ReadKey(false);
}
private static void Notify(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"{e.FullPath} {e.ChangeType}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["fsw-bug-poc.csproj", ""]
RUN dotnet restore "fsw-bug-poc.csproj"
COPY . …Run Code Online (Sandbox Code Playgroud) 鉴于以下中间件:
public class RequestDurationMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<RequestDurationMiddleware> _logger;
public RequestDurationMiddleware(RequestDelegate next, ILogger<RequestDurationMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
var watch = Stopwatch.StartNew();
await _next.Invoke(context);
watch.Stop();
_logger.LogTrace("{duration}ms", watch.ElapsedMilliseconds);
}
}
Run Code Online (Sandbox Code Playgroud)
由于管道,它发生在管道结束之前并记录不同的时间:
WebApi.Middlewares.RequestDurationMiddleware 2018-01-10 15:00:16.372 -02:00 [Verbose] 382ms
Microsoft.AspNetCore.Server.Kestrel 2018-01-10 15:00:16.374 -02:00 [Debug] Connection id ""0HLAO9CRJUV0C"" completed keep alive response.
Microsoft.AspNetCore.Hosting.Internal.WebHost 2018-01-10 15:00:16.391 -02:00 [Information] "Request finished in 405.1196ms 400 application/json; charset=utf-8"
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如何从WebHost(示例中为405.1196ms)值捕获实际请求执行时间?我想将此值存储在数据库中或在其他地方使用它.
为 IIS Asp.NET Core 应用程序池设置“无托管代码”.NET CLR 版本有什么意义?
文档说
ASP.NET Core 在单独的进程中运行并管理运行时。ASP.NET Core 不依赖于加载桌面 CLR。将 .NET CLR 版本设置为无托管代码是可选的。
既然它是可选的,那么保留默认的v4.0 有什么缺点?为什么文档明确指示将其设置为“无托管代码”?目前还不清楚这个特定配置是否存在性能问题。
我正在寻找一种方法来调用带有lambda表达式的泛型方法,该表达式在一个项目数组中调用Contains.
在这种情况下,我正在使用Entity Framework Where方法,但该方案可以应用于其他IEnumerables.
我需要通过Reflection调用上面代码的最后一行,所以我可以使用任何类型和任何属性传递给Contains方法.
var context = new TestEntities();
var items = new[] {100, 200, 400, 777}; //IN list (will be tested through Contains)
var type = typeof(MyType);
context.Set(type).Where(e => items.Contains(e.Id)); //**What is equivalent to this line using Reflection?**
Run Code Online (Sandbox Code Playgroud)
在研究中,我注意到我应该使用GetMethod,MakeGenericType和Expression来实现这一点,但我无法弄清楚如何去做.拥有这个样本非常有帮助,因此我可以理解Reflection如何与Lambda和Generic概念一起工作.
基本上,目标是编写一个正确版本的函数,如下所示:
//Return all items from a IEnumerable(target) that has at least one matching Property(propertyName)
//with its value contained in a IEnumerable(possibleValues)
static IEnumerable GetFilteredList(IEnumerable target, string propertyName, IEnumerable searchValues)
{
return target.Where(t => searchValues.Contains(t.propertyName));
//Known the following:
//1) This …Run Code Online (Sandbox Code Playgroud) 我可以使用async/await等所有选项,如OnlyOnRanToCompletion,OnlyOnCanceled,NotOnFaulted等吗?我找不到关于如何使用Tasks获得相同结果的示例,例如:
Task.Factory.StartNew(foo).ContinueWith(bar, TaskContinuationOptions.NotOnRanToCompletion);
Run Code Online (Sandbox Code Playgroud)
我不确定简单的条件或异常处理是否可以管理显式任务中可用的所有延续行为.
cmd窗口中的以下命令
sqlcmd -S. -Usa -Ppass -dmaster -Q "RESTORE DATABASE [MYDATABASE] FROM DISK = 'D:\SQL Server\MYDATABASE.BAK' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10"
Run Code Online (Sandbox Code Playgroud)
显示以下输出:
10 percent processed.
20 percent processed.
30 percent processed.
40 percent processed.
50 percent processed.
60 percent processed.
70 percent processed.
80 percent processed.
90 percent processed.
100 percent processed.
Processed 32320 pages for database 'MYDATABASE', file 'MYDATABASE' on file 1.
Processed 7 pages for database 'MYDATABASE', file 'MYDATABASE_log' on file 1.
Run Code Online (Sandbox Code Playgroud)
但是事实是,仅在整个还原 …
如何设置 lighttpd 以使环境变量可用于生成的进程?
我有一个通过 CGI 作为 URL 调用的可执行文件(例如http://.../cgi-bin/executable.bin)。
可执行文件需要加载库并读取/etc/profileusing中设置的环境变量export FOO=BAR。
当我尝试访问 URL 时,它会生成内部服务器错误 (500),这是由在/etc/profile.
如何创建像下面的示例一样的复合实现,但使用本机 .NET Core DI 容器?
\n\n [TestFixture]\n public class CompositeTests\n {\n [Test]\n public void BuildComposite()\n {\n var container = new UnityContainer();\n container.RegisterType<IFoo, SomeFoo>("first");\n container.RegisterType<IFoo, AnotherFoo>("second");\n container.RegisterType<IFoo, CompositeFoo>();\n\n var instanceOfFoo = container.Resolve<IFoo>();\n\n Assert.IsInstanceOf<CompositeFoo>(instanceOfFoo);\n }\n }\n\n public class CompositeFoo : IFoo\n {\n public CompositeFoo(IFoo[] others)\n {\n Debug.Assert(others != null);\n Debug.Assert(others.Any());\n }\n }\n\n public class AnotherFoo : IFoo {}\n public class SomeFoo : IFoo {}\n public interface IFoo {}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n在这种情况下,复合\xe2\x80\x9c消耗\xe2\x80\x9d子对象的数组,并且在某种意义上\xe2\x80\x9csucks在\xe2\x80\x9d中使用密钥注册的IFoo的每个实现。\这是一个重要的方面:如果您要使用键注册组合,它将尝试实例化自身,从而立即导致 StackOverflowException。
\n
本机 DI 不支持这些命名注册。
\n\n这个例子是从这里提取的
\nc# ×5
asp.net-core ×3
.net-core ×2
linux ×2
t-sql ×2
async-await ×1
cgi ×1
clr ×1
coalesce ×1
composite ×1
docker ×1
generics ×1
iis ×1
interface ×1
isnull ×1
lambda ×1
lighttpd ×1
options ×1
progress ×1
reflection ×1
server ×1
sql-server ×1
sqlcmd ×1
stopwatch ×1
truncation ×1
unmanaged ×1
where ×1