小编Zac*_*cci的帖子

.NET Core 应用程序中的 ConfigurationManager

我在应用程序调试/启动时收到以下错误:

System.TypeInitializationException: 'System.Data.Entity.Internal.AppConfig' 的类型初始值设定项引发异常。

FileNotFoundException:无法加载文件或程序集“System.Configuration.ConfigurationManager,Version=4.0.2.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”。该系统找不到指定的文件。

使用以下代码段DatabaeContext

private CamelotViewsStandardContext db = new CamelotViewsStandardContext();
Run Code Online (Sandbox Code Playgroud)

该项目位于 .NET Core 3.0 上,并System.Configuration.ConfigurationManager安装了NuGet 包。它适用于 .NET Core 2.2 上的先前版本,所以不知道为什么它不适用于 3.0?

创建数据库上下文的参考背后的代码使用的是实体框架,应该在 3.0 中支持?

c# configuration configurationmanager .net-core-3.0

6
推荐指数
2
解决办法
4351
查看次数

IActionContextAccessor 为空

我正在为我的 .NET Core 应用程序设置一个自定义中间件来记录异常错误,并在 Startup.cs 中使用以下内容来注册我的上下文:

   services.AddHttpContextAccessor();
   services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
Run Code Online (Sandbox Code Playgroud)

在我的配置中,我添加了以下内容以使用自定义中间件:

   app.UseMiddleware<CustomMiddleware>();
Run Code Online (Sandbox Code Playgroud)

我的自定义中间件类如下:

 public class CustomMiddleware
 {
    private readonly RequestDelegate next;

    public CustomMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            this.BeginInvoke(context);
            await this.next.Invoke(context);
            this.EndInvoke(context);
        }
        catch(Exception ex)
        {
            //Capture the exception.
            string hostName = Environment.MachineName;
            string url = StringFunctions.getCurrentUrl(context);
            string userName = string.Empty;
            string controllerName = string.Empty;
            string actionName = string.Empty;

            //THIS IS NULL BELOW.
            IActionContextAccessor contextAccessor = context.RequestServices.GetService(typeof(IActionContextAccessor)) as IActionContextAccessor;
            RouteData routeData = …
Run Code Online (Sandbox Code Playgroud)

.net c# middleware actioncontext asp.net-core

6
推荐指数
1
解决办法
4073
查看次数

C# WebRequest 发送表单数据大小限制

我正在尝试在 WebRequest 中发送表单数据。

该函数工作正常并按预期返回“成功”响应流。

但是,如果“data”变量的长度超过 30,000 个字符,我会遇到 HTTP 500 错误:

Message "The remote server returned an error: (500) Internal Server Error." 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这是我发送数据的“测试”函数:

 public IActionResult Test()
 {
     var data = "[0].meetingDate=2019-07-17&[0].courseId=9& 
                        [0].raceNumber=1&[0].horseCode=000000500523";

     //THIS MAKES THE REQUEST ERROR BY INCREASING THE SIZE OF THE DATA WHICH THEN GENERATES A ERROR
     for( int i = 0; i < 400; i++)
     {
         data = data + "[0].meetingDate=2019-07-17&[0].courseId=9&[0].raceNumber=1&[0].horseCode=000000500523";
     }
     //////////////////////////

     WebRequest request = WebRequest.Create("https://localhost:44374/HorseRacingApi/Prices/GetPriceForEntries");
     request.Method = "POST";

     if (!string.IsNullOrEmpty(data))
     {
         byte[] dataBytes = Encoding.ASCII.GetBytes(data);

         request.ContentType …
Run Code Online (Sandbox Code Playgroud)

c# size http webrequest

3
推荐指数
1
解决办法
3814
查看次数

C#Json反序列化为通用对象

我有一个具有自己的对象类型的函数:

public RaceJson GetLatestRace()
{
     string filter = "example";
     List<RaceJson> currentRace = await gsaClient.SendCustomRequest<List<RaceJson>>("races?$filter=" + filter);
     return currentRace.FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

我想使用它们都将使用的通用函数。我希望它是通用的,并将对象反序列化为我要发送的类型。我目前有:

    public async Task<T> SendCustomRequest<T>(string odataFilter)
    {
        string response = await SendRequestAsync(odataFilter, true);

        if( !string.IsNullOrEmpty(response))
        {
            T converted = JsonConvert.DeserializeObject<T>(response);
            return converted;
        }

        return default;
    }
Run Code Online (Sandbox Code Playgroud)

尝试反序列化列表时出现错误:

JsonSerializationException:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [tf_gsa_client.Models.HorseRacing.RaceJson]',因为该类型需要JSON数组(例如[1,2,3])以正确反序列化。要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型。数组或列表),可以从JSON对象反序列化。还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。

谢谢。

c# serialization json types object

0
推荐指数
1
解决办法
74
查看次数