控制器从服务中无法获取ILogger和SugDbContext的时候排除异常
This commit is contained in:
		
							parent
							
								
									8a8160bf9e
								
							
						
					
					
						commit
						41c41878e1
					
				@ -39,11 +39,14 @@ namespace Falcon.SugarApi.ApiDefinistions
 | 
				
			|||||||
                return $":{con}:{ac}";
 | 
					                return $":{con}:{ac}";
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// 构造控制器基类
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="service"></param>
 | 
				
			||||||
        protected ApiControllerBase(IServiceProvider service) {
 | 
					        protected ApiControllerBase(IServiceProvider service) {
 | 
				
			||||||
            this.Services = service;
 | 
					            this.Services = service;
 | 
				
			||||||
            this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger;
 | 
					            this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger ?? throw new NullReferenceException("ILogger");
 | 
				
			||||||
            this.SugarDb = service.GetService<SugarDbContext>();
 | 
					            this.SugarDb = service.GetService<SugarDbContext>() ?? throw new NullReferenceException("SugarDbContext");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
@ -77,7 +80,7 @@ namespace Falcon.SugarApi.ApiDefinistions
 | 
				
			|||||||
        /// <typeparam name="T">对象的类型</typeparam>
 | 
					        /// <typeparam name="T">对象的类型</typeparam>
 | 
				
			||||||
        /// <param name="json">json字符串</param>
 | 
					        /// <param name="json">json字符串</param>
 | 
				
			||||||
        /// <returns>对象实例</returns>
 | 
					        /// <returns>对象实例</returns>
 | 
				
			||||||
        protected T JsonDeserialize<T>(string json) {
 | 
					        protected T? JsonDeserialize<T>(string json) where T: class {
 | 
				
			||||||
            return JsonSerializer.Deserialize<T>(json);
 | 
					            return JsonSerializer.Deserialize<T>(json);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user