简单的注入器和 Cookie
Simple Injector and Cookies
如何使用 cookie 的数据作为参数为每个请求的生命范围创建实例?
例如:
container.Register<ISampleRepository>(() =>
new SampleRepository(
container.GetInstance<ApplicationDbContext>(),
request.Cookie["Token"]));
正如 NightOwl888 在他的评论中所说:
Cookies are runtime data that are user specific (and already tied to the request). Dependency Injection is something that happens 1 time at application startup for all users in the composition root. It makes absolutely no sense to have a user's cookie as an input to your application configuration. Perhaps it would be better if you describe what it is you are trying to achieve with your cookie.
您遇到的问题以及解决此问题的正确方法已在 blog post 中进行了完整描述。
如何使用 cookie 的数据作为参数为每个请求的生命范围创建实例?
例如:
container.Register<ISampleRepository>(() =>
new SampleRepository(
container.GetInstance<ApplicationDbContext>(),
request.Cookie["Token"]));
正如 NightOwl888 在他的评论中所说:
Cookies are runtime data that are user specific (and already tied to the request). Dependency Injection is something that happens 1 time at application startup for all users in the composition root. It makes absolutely no sense to have a user's cookie as an input to your application configuration. Perhaps it would be better if you describe what it is you are trying to achieve with your cookie.
您遇到的问题以及解决此问题的正确方法已在 blog post 中进行了完整描述。