小编Ali*_*ghi的帖子

.NET Core 中 IDisposable 类的依赖注入生命周期

我想知道,请求完成后处理所有 IDisposable 对象的最佳方法是什么。

  • AddTransient<T>- 添加每次请求时再次创建的类型。
  • AddScoped<T>- 添加为请求范围保留的类型。
  • AddSingleton<T>- 在第一次请求时添加类型并保留它。

因此,单例可能不是一个好的选择,因为它会在应用程序被击落后进行处理。但范围和瞬态是不错的选择。我有一个存储库,我想与我的数据库创建连接,如下所示:

public class Dapperr : IDapper  
    {  
        private readonly IConfiguration _config;  
        private string Connectionstring = "DefaultConnection";  
  
        public  Dapperr(IConfiguration config)  
        {  
            _config = config;  
        }  
        public void Dispose()  
        {  
             
        }  
  
        
        public T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.Text)  
        {  
            using IDbConnection db = new SqlConnection(_config.GetConnectionString(Connectionstring));  
            return db.Query<T>(sp, parms, commandType: commandType).FirstOrDefault();  
        }  
  
        public List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)  
        {  
            using IDbConnection db = new …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection dapper .net-core asp.net-core

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

值后的“r”无效。应为“,”、“}”或“]”

我正在尝试使用以下结构将字符串反序列化为类:

  // this is my json
    {
      "MethodName":"PRC_SET_COMMISSION_STATIC",
      "Input":"{"reqType":"U","sapId":17000100,"offerType":5,"commissionRate":4,"accountNo":null,"fromDate":"2022-05-29T00:00:00","toDate":"2029-05-29T00:00:00","userId":"13601360"}"}
Run Code Online (Sandbox Code Playgroud)

这是我想要获得以下值的课程:

public class ProcessRequestRequestModelCl :AuthenticationModelCl
    {
        public string MethodName { get; set; }
        public string Input { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

所以我为实现这一目标所做的事情是这样的:

ProcessRequestRequestModelCl RequestModel = System.Text.Json.JsonSerializer.Deserialize<ProcessRequestRequestModelCl>(ParameterStr);
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:“r”在值后无效。预计 ','、'}' 或 ']' 问题与 有关"Input":"{"reqType":"U","sapId":17000100,"offerType":5,"commissionRate":4,"accountNo":null,"fromDate":"2022-05-29T00:00:00","toDate":"2029-05-29T00:00:00","userId":"13601360"}"。我想知道如何将 json 作为字符串传递给输入值。

c# json deserialization json-deserialization .net-core

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