关于 asp.net mvc:Simple Injector and Cookies

Simple Injector and Cookies

如何使用 cookie 的数据作为参数为每个请求的生命范围创建实例?

例如:

1
2
3
4
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.

这篇博文详细描述了您使用正确方法解决此问题时遇到的问题。