如何立即实例化单例
services.AddSingelton<IMySingleton, MySingleton>()
Run Code Online (Sandbox Code Playgroud)
我现在正在使用这个肮脏的解决方法(GetService在每个请求中调用):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.ApplicationServices.GetService<IFirebaseApp_ForInjection>();
//......
}
Run Code Online (Sandbox Code Playgroud)
我知道我的单例应该在构造函数注入需要时实例化,但我不需要将它注入到任何构造函数中。
我在 HttpInterceptor 的响应中缺少 http 标头。我可以得到一个正文,但不能得到标题。请参阅附加的输出和我的代码。
@Injectable()
export class ApiVersionInterceptor implements HttpInterceptor {
intercept(
req: import("@angular/common/http").HttpRequest<any>,
next: import("@angular/common/http").HttpHandler
): import("rxjs").Observable<import("@angular/common/http").HttpEvent<any>> {
return next.handle(req).pipe(
tap(httpEvent=>{
// Skip request
if(httpEvent.type === 0){
return;
}
console.log("response: ", httpEvent);
})
);
}
}
Run Code Online (Sandbox Code Playgroud)