相关疑难解决方法(0)

实体框架:已经有一个与此命令关联的开放DataReader

我正在使用实体框架,偶尔我会得到这个错误.

EntityCommandExecutionException
{"There is already an open DataReader associated with this Command which must be closed first."}
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands...
Run Code Online (Sandbox Code Playgroud)

即使我没有做任何手动连接管理.

这个错误间歇性地发生.

触发错误的代码(为了便于阅读而缩短):

        if (critera.FromDate > x) {
            t= _tEntitites.T.Where(predicate).ToList();
        }
        else {
            t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList());
        }
Run Code Online (Sandbox Code Playgroud)

使用Dispose模式以便每次都打开新连接.

using (_tEntitites = new TEntities(GetEntityConnection())) {

    if (critera.FromDate > x) {
        t= _tEntitites.T.Where(predicate).ToList();
    }
    else {
        t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList());
    }

}
Run Code Online (Sandbox Code Playgroud)

仍有问题

如果连接已经打开,EF为什么不重用连接?

linq entity-framework sql-server-2008

279
推荐指数
6
解决办法
15万
查看次数

.net core 3 在 AddJsonOptions 中没有 ReferenceLoopHandling

我的 csproject 文件表明: <TargetFramework>netcoreapp3.0</TargetFramework>

在我的启动中,我使用以下内容:

 services.AddMvc(x => x.Filters.AddService<TransactionFilter>())
        .AddJsonOptions(options => options.JsonSerializerOptions... )
Run Code Online (Sandbox Code Playgroud)

但是, ReferenceLoopHandling 内部不可用options.JsonSerializerOptions

在此处输入图片说明

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentNHibernate" Version="2.1.2" />
    <PackageReference Include="FullContact.Contacts.API" Version="1.0.3" />
    <PackageReference Include="Google.Cloud.Storage.V1" Version="2.3.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Cors" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.5.0" />
    <PackageReference Include="MySql.Data" Version="8.0.17" />
    <PackageReference Include="piplclient" Version="5.0.9" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.5.0" />
  </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

json asp.net-core

25
推荐指数
3
解决办法
3万
查看次数

如何在Asp.net Core Web Api中使用Newtonsoft.Json作为默认值?

我是ASP.Net Web Api Core的新手.我一直在使用ASP.Net MVC过去的几年里,我一直都写了ActionFilter,并用于JSON.NetSerializing数据导入JSON.所以,通过这种方式,我取代了微软JavaScript Serializer(比它慢JSON.Net)JSON.Net(声称速度提高了400%).

如何在ASP.Net Web Api Core中完成所有这些操作?在哪里更改默认格式化程序

注意:如果您有任何疑问,请随时询问.

谢谢

serialization json json.net asp.net-web-api asp.net-core

24
推荐指数
3
解决办法
2万
查看次数

ASP.NET Core:不允许进行同步操作。调用WriteAsync或将AllowSynchronousIO设置为true

ASP.NET核心服务器,AllowSynchronousIO设置为false

        new WebHostBuilder()
        .UseKestrel(options =>
        {
            options.AllowSynchronousIO = false;
        })
Run Code Online (Sandbox Code Playgroud)

在动作中,它输出一个JsonResult

    public async Task<IActionResult> SanityCheck()
    {
        Dictionary<string, string> dic = await GetDic();

        return this.Json(dic);
    }
Run Code Online (Sandbox Code Playgroud)

它以一个异常结束

System.InvalidOperationException:不允许进行同步操作。调用WriteAsync或将AllowSynchronousIO设置为true。

我不能用返回JsonResult AllowSynchronousIO=false吗?

最好的祝福

asp.net-mvc

16
推荐指数
6
解决办法
5335
查看次数

如何在.Net Core Web API中停止自引用循环?

我有一些问题,我猜这些问题与使用.NET Core Web API和Entity Framework Core进行自引用有关.我添加时,我的Web API开始窒息.包括一些导航属性.

我发现旧的Web API中似乎是一个解决方案,但我不知道如何为.NET Core Web API实现相同的功能(我还处于早期学习阶段).

较旧的解决方案是将其粘贴在Global.asax的Application_Start()中:

 GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
Run Code Online (Sandbox Code Playgroud)

我怀疑这是在StartUp的ConfigureService()方法中处理的,但除此之外我不太了解.

或者有更合适的方法来处理这个问题吗?

c# entity-framework-core asp.net-core-webapi

15
推荐指数
3
解决办法
9346
查看次数

ASP.NET Core 3.0 中的 JsonOutputFormatter

在asp.net core 2.2中,我曾经有以下内容,

  var jsonSettings = new JsonSerializerSettings
  {
    ContractResolver = new SubstituteNullWithEmptyStringContractResolver()
  };

services.AddMvc(options =>
{
        options.OutputFormatters.RemoveType<JsonOutputFormatter>();
        options.OutputFormatters.Add(new ResponseJsonOutputFormatter(jsonSettings,ArrayPool<char>.Shared));
}

public class ResponseJsonOutputFormatter : JsonOutputFormatter
{
 // Stuff in here
}
Run Code Online (Sandbox Code Playgroud)

但是在 3.0 中使用:

services.AddControllersWithViews(options =>
Run Code Online (Sandbox Code Playgroud)

并且类型JsonOutputFormatter不再可用。

当前建议的全局自定义 json 响应的方法是什么?

我尝试使用IOutputFormatter但是当我将它设置AddControllersWithViews为 OutputFormatters时它似乎没有连接,所以不确定是否有额外的步骤?

具有新端点路由的中间件是一种选择吗?或者有没有更好的方法来实现这一目标?

c# json asp.net-core-3.0

12
推荐指数
1
解决办法
3552
查看次数

使用枚举数组反序列化 json

使用枚举:

namespace AppGlobals
{
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum BoardSymbols
    {
        [EnumMember(Value = "X")]
        First = 'X',
        [EnumMember(Value = "O")]
        Second = 'O',
        [EnumMember(Value = "?")]
        EMPTY = '?'
    }
}
Run Code Online (Sandbox Code Playgroud)

我想为我的 api 定义一个模型:

using System;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace Assignment_1
{
    public class MyRequest
    {
//...
        [Required]
        [MinLength(9)]
        [MaxLength(9)]
        [JsonProperty("changeTypes", ItemConverterType = typeof(JsonStringEnumConverter))]
        public AppGlobals.BoardSymbols[] GameBoard { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

哪里GameBoard应该序列化为 JSON 作为字符串数组,其名称由EnumMember属性指定。这种方法改编自Deserialize json character as enumeration。然而,它不起作用。如果我将枚举更改为:

    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum …
Run Code Online (Sandbox Code Playgroud)

c# asp.net json.net asp.net-core-webapi asp.net-core-3.1

12
推荐指数
1
解决办法
1万
查看次数

如何解决 AddJsonOptions 不包含 SerializerSettings - .NET 的定义

希望有人可以帮助我,我一直在寻找,但一直无法找到解决方案。也可能是一些基本的东西,我只是找不到解决方案。

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddJsonOptions(opt =>
                {
                    opt.SerializerSettings.ReferenceLoopHandLing = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                });
Run Code Online (Sandbox Code Playgroud)

此代码试图解决引用循环的问题。我正在学习使用 .NET 和 Angular CLI 构建 web 应用程序的教程。在教程中,它显示了循环错误,我也在我的代码中找到了它。尝试遵循解决方案(上面的代码)时,我在 SerializerSettings 上遇到错误,说 JsonOptions 不包含此类定义。

我试过:

添加 [Obsolete](visual studio 建议),

安装 Microsoft.AspNetCore.MVC.Formatters.Json nuget 包(VS 在添加时通知它什么都不做),(在文档和这里 com StackOverflow 上看到这个解决方案)

对于 Newtonsoft Json.Net 文档尝试了 ReferenceLoopHandling(我可能没有正确使用它,所以如果有人觉得这是出路,请告诉我)

提前致谢,

.net c# serialization angular

8
推荐指数
1
解决办法
4986
查看次数

参考循环处理忽略在Asp.Net Core 3.0 Preview 3上不起作用

我一直在用这个头撞墙,试图找出为什么它不起作用。我至今无法找到任何有关为什么它不起作用的信息,所以我在这里问。

我有一个在Asp.Net Core 3.0 Preview 3上运行的控制台应用程序。

在这个项目中,我遇到了一个Json循环问题,我知道可以通过将“启动”中的“参考循环处理”设置为“忽略”来解决。但是,我只能在.AddJsonOptions()中找到有关设置它的信息,Asp.Net Core 3.0中似乎没有。

我去了如何从2.1迁移到3.0的文档,发现了这一点

即使在相应地更改了我的代码之后

services.AddMvc()
     .AddNewtonsoftJson(
          options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }
      );
Run Code Online (Sandbox Code Playgroud)

我仍然收到一条错误消息:“检测到类型为[[模型名称]]的属性'[插入类名称]'的自引用循环”。

我还能在哪里设置Json忽略循环引用?

或者我该怎么做才能使这项工作?

谢谢高级

c# json.net asp.net-core

7
推荐指数
1
解决办法
1901
查看次数