我试图用投影运行聚合但我得到了NotSupportedException: $project or $group does not support <document>.我正在使用mongodb v3.4运行2.4.4版本的驱动程序.
var filter = Builders<T>.Filter.Regex(x=>x.Value,"/test/gi");
var aggregate = collection.Aggregate()
.Match(filter)
.Project(x => new
{
Idx = x.Value.IndexOf("test"),
Result = x
})
.SortBy(x => x.Idx);
Run Code Online (Sandbox Code Playgroud)
我认为IndexOfCP是支持的.
我在这做错了什么?
我有一个基本配置文件,例如.
appsettings.json
{
"Values": {
"Test": ["one", "two"]
}
}
Run Code Online (Sandbox Code Playgroud)
和
appsettings.dev.json
{
"Values": {
"Test": ["three"]
}
}
Run Code Online (Sandbox Code Playgroud)
在转换之后,数组将是
["three", "two"]
Run Code Online (Sandbox Code Playgroud)
如何确保转换后的数组缩小到较少数量的元素,而不是每个元素单独更改?
我正在尝试从新的Asp Net Core 2.0 Web应用程序调用.Net 4.7数据访问库(使用Entity Framework 6).
问题是EF6似乎无法获得DbProviderFactory.我的工作理论是,这应该是在调用程序的app/web.config中提供的.我得到的错误是:
System.TypeLoadException:'无法从程序集'System.Data,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'加载类型'System.Data.Common.DbProviderFactories'.'
为了解决这个问题,我创建了一个DbConfiguration类:
public class MyDbConfiguration : DbConfiguration
{
public MyDbConfiguration()
{
SetProviderFactory("System.Data.SqlClient", System.Data.SqlClient.SqlClientFactory.Instance);
SetProviderServices("System.Data.SqlClient", SqlProviderServices.Instance);
}
}
[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyDbContext : DbContext, IMyDbContext
{
public MyDbContext(string connectionString)
: base(connectionString)
{
}
Run Code Online (Sandbox Code Playgroud)
断点显示它正确执行MyDbConfiguration,但仍然抛出错误.我已经在.Net Core Web应用程序上安装了System.Data.SqlClient和System.Data.Common包.
我没有发现任何明确说明我想要做的事情(一般)是不可能的,所以我正在假设我的DBConfiguration实现有问题.请有人指出我正确的方向吗?
.net entity-framework-6 asp.net-core asp.net-core-2.0 .net-4.7
我正在使用 Json.net 反序列化 Web API 调用接收到的 json 数据。某些字段通常具有 html 编码的字符,例如"或&如何在反序列化期间自动解码此字符?
我得出了两种可能的解决方案:
调用System.Web.HttpUtility.HtmlDecode()属性设置器,如:
public string Title
{
set
{
title = System.Web.HttpUtility.HtmlDecode(value);
}
}
Run Code Online (Sandbox Code Playgroud)编写自定义JsonConverter的呼叫System.Web.HttpUtility.HtmlDecode()的ReadJson()方法:
public class HtmlEncodingConverter : Newtonsoft.Json.JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(String);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return System.Web.HttpUtility.HtmlDecode((string)reader.Value);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteRawValue(System.Web.HttpUtility.HtmlEncode((string)value));
}
}
Run Code Online (Sandbox Code Playgroud)但是是否有任何内置解决方案允许在 …
让我们考虑以下简单程序:
class Program
{
class TestClass
{
~TestClass()
{
Console.WriteLine("~TestClass()");
}
}
static void Main(string[] args)
{
WeakReference weakRef;
{
var obj = new TestClass();
weakRef = new WeakReference(obj);
Console.WriteLine("Leaving the block");
}
Console.WriteLine("GC.Collect()");
GC.Collect();
System.Threading.Thread.Sleep(1000);
Console.WriteLine("weakRef.IsAlive == {0}", weakRef.IsAlive);
Console.WriteLine("Leaving the program");
}
}
Run Code Online (Sandbox Code Playgroud)
在Release模式下构建时,可预测打印:
Leaving the block
GC.Collect()
~TestClass()
weakRef.IsAlive == False
Leaving the program
Run Code Online (Sandbox Code Playgroud)
启动调试版本时(不在调试器下,通常从Windows资源管理器启动),输出会有所不同:
Leaving the block
GC.Collect()
weakRef.IsAlive == True
Leaving the program
~TestClass()
Run Code Online (Sandbox Code Playgroud)
在两个版本的调试器下运行不会更改输出.
我在自定义集合的调试过程中发现了这种奇怪的区别,它保留了对对象的弱引用.
为什么调试可执行文件中的垃圾收集器不会收集明显未被引用的对象?
更新:
如果以其他方法执行对象创建,情况会有所不同:
class Program
{
class TestClass
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Asp.Net Core 2.0.1,EF 2.0.1和MVC 6在多项目解决方案中将Serilog设置为我的记录器.
我已经设置了Serilog,主要是遵循这个博客文章 Set up Serilog post的指导
该帖子中的json存在问题,我已经更正并在此处显示
appsettings.json文件
{
"ApplicationConfiguration": {
"ConnectionStrings": {
"DevelopmentConnection": "Server=(localdb)\\mssqllocaldb;Database=TestingConfigurationNetCoreTwo_Development;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"ApplicationInfo": {
"VersionNumber": "1.0.0",
"Author": "Jimbo",
"ApplicationName": "CustomTemplate",
"CreatedOn": "November 20, 2017"
},
"Serilog": {
"Using": [
"Serilog.Sinks.RollingFile",
"Serilog.Sinks.Async",
"Serilog.Sinks.ApplicationInsights",
"Serilog.Sinks.Console",
"Serilog.Sinks.Seq"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "RollingFile",
"Args": { "pathFormat": "Logs/log-{Date}.log" }
}
]
}
}
],
"Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
"Properties": …Run Code Online (Sandbox Code Playgroud) 我在ASP.NET核心2.0中有过滤器属性,请参阅下面的我的代码片段.这里的问题是我总是得到状态码是200.
即使实际状态代码是500,那么我也得到200.如何获得实际状态代码?
public void OnActionExecuted(ActionExecutedContext context)
{
try
{
var controller = context.Controller as APIServiceBase;
var statusCode = controller.Response.StatusCode;
..
..
}
catch { }
}
Run Code Online (Sandbox Code Playgroud) 在我的.net core 2.0 Web API中,我在模型属性上使用EnumDataType()验证属性。验证失败时,自定义错误消息为空。我不确定为什么会这样-
EnumDataType(typeof(MyEnum), ErrorMessage = "Custom Error Message")
public MyEnum MyEnumProp {get; set;}
Run Code Online (Sandbox Code Playgroud)
我检查了其他具有[Required],[MinLength]的属性,并且所有属性均生成自定义错误消息。难道我做错了什么?还有其他方法吗?
我知道这是一个设计问题,但我正在努力理解这一点,以便以最佳方式使用它。因此,请考虑该问题,以阐明如何使用其最大功能。
为什么它不是设计为基础的KISS同步和异步有方法(StartAsync,StopAsync),据我所知,异步的Web请求的主要好处是让一些空闲线程释放被用来为进一步的请求,但它不能是这样的对于IHostedService因为没有的请求的概念和总有一个运行(或悬浮)线程。
我们有一个带有这个 appsettings.json 的 ASP.NET Core Web 应用程序:
{
"Subscriptions": [
{
"Name": "Production",
"PublishSettings": "<PublishData>SECRET</PublishData>",
"Environments": [
{
"Name": "Prod",
"DeploymentServiceNames": [
"api1",
"api2",
"api3"
]
}
]
},
{
"Name": "Test",
"PublishSettings": "<PublishData>SECRET</PublishData>",
"Environments": [
{
"Name": "Test1",
"DeploymentServiceNames": [
"api1",
"api2"
]
},
{
"Name": "Test2",
"DeploymentServiceNames": [
"api1",
"api2"
]
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
这些PublishSettings值是秘密的,所以我希望在我的本地用户机密文件和我的部署的环境变量中使用这些值。但是,因为Subscriptions是一个数组,我不确定如何。我不是特别想交换整个Subscriptions部分。有没有办法为这样一个数组中的每个项目交换一个属性,也许是通过在强类型订阅模型上定义一个键属性?
asp.net-core ×6
c# ×6
.net-core ×3
.net ×2
json ×2
.net-4.7 ×1
appsettings ×1
asp.net ×1
asynchronous ×1
enums ×1
html-encode ×1
json.net ×1
logging ×1
serilog ×1