如何设置 StructureMap 来解决 Windows 服务中的依赖关系?

发布于 2024-12-12 05:13:51 字数 1221 浏览 1 评论 0原文

我有一个使用 ASP.NET MVC 1.0 构建的网站。它使用 Structuremap 作为 IOC 容器。 如果我像这样在 Application_Start 上注册 IOC 部分,效果会很好:

ObjectFactory.Initialize(service =>
    {
        service.ForRequestedType<IOrderRepository>()
               .TheDefaultIsConcreteType<OrderRepository>()
               .CacheBy(InstanceScope.PerRequest);
    });

我必须在 Windows 服务中使用相同的后端。

该服务中有一些同时访问 OrderRepository 的计时器,因此线程是这里的一个问题。

我的第一个想法是在服务的构造函数中注册它,如下所示:

public Service1()
{
    ObjectFactory.Initialize(service =>
        {
            service.ForRequestedType<IOrderRepository>()
                   .TheDefaultIsConcreteType<OrderRepository>()
                   .CacheBy(InstanceScope.PerRequest);
        });
}

这是缓存的正确位置和正确参数吗?

阅读Structuremap的文档,我认为最安全的方法是使用缓存的默认设置:

PerRequest - 默认操作。将为每个请求创建一个新实例。

我的印象是 PerRequest 意味着 HttpContext,但那是另一个条目:

HttpContext - 将为每个 HttpContext 创建一个实例。缓存 HttpContext.Items 集合中的实例。

I have a web that was build using ASP.NET MVC 1.0. It uses Structuremap as an IOC container.
The IOC part works nice if I register it on Application_Start like this:

ObjectFactory.Initialize(service =>
    {
        service.ForRequestedType<IOrderRepository>()
               .TheDefaultIsConcreteType<OrderRepository>()
               .CacheBy(InstanceScope.PerRequest);
    });

I have to use the same backend in a Windows Service.

The service has some timers in it that access OrderRepository simultaneously, so threading is an issue here.

My first idea is to register it in the constructor of the service like this:

public Service1()
{
    ObjectFactory.Initialize(service =>
        {
            service.ForRequestedType<IOrderRepository>()
                   .TheDefaultIsConcreteType<OrderRepository>()
                   .CacheBy(InstanceScope.PerRequest);
        });
}

Is this the right place and the right parameter for caching?

Reading the documentation of Structuremap, I think the safest way is to use the default setting for Caching:

PerRequest - The default operation. A new instance will be created for each request.

I had the impression that PerRequest meant HttpContext, but thats another entry:

HttpContext - A single instance will be created for each HttpContext. Caches the instances in the HttpContext.Items collection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2024-12-19 05:13:51

根据本文:http://msdn.microsoft.com/en -us/library/system.serviceprocess.servicebase.aspx

可执行文件调用ServiceBase派生类的构造函数
第一次调用该服务的 Start 时
。 OnStart 命令处理
构造函数执行后立即调用方法。这
第一次服务后,构造函数不会再次执行
已加载,因此需要将执行的处理分开
由 OnStart 执行的构造函数执行。任何资源
可以通过OnStop发布,应该在OnStart中创建。创造
构造函数中的资源阻止它们正确创建
如果 OnStop 释放后再次启动服务
资源。

听起来构造函数是第一次设置结构图的方法。

Per this article: http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.aspx

The executable calls the ServiceBase derived class's constructor the
first time you call Start on the service
. The OnStart command-handling
method is called immediately after the constructor executes. The
constructor is not executed again after the first time the service has
been loaded, so it is necessary to separate the processing performed
by the constructor from that performed by OnStart. Any resources that
can be released by OnStop should be created in OnStart. Creating
resources in the constructor prevents them from being created properly
if the service is started again after OnStop has released the
resources.

It sounds like constructor is the way to go for the first time setup of structure map.

鹿童谣 2024-12-19 05:13:51

老实说,这更像是“我的两分钱”贡献,实际上是试图给出明确的答案,因为我开发 Windows 服务已经有一段时间了,但现在就这样了。

与 ASP .Net MVC 应用程序进行类比,其中容器的配置将在 Global.asax 类的 Application_Start 方法中进行,然后将配置的容器注入自定义控制器工厂;我相信您可以尝试在可执行文件的 Main 函数中配置所有内容,而不是在服务的构造函数中配置所有内容,因为它只会运行一次,然后将配置的容器注入到服务的构造函数中。

我认为通过这样做,您可以引导所有 Composition Root 的内容服务中的应用程序和代码将专注于做它应该做的事情。

就像我说的,已经有一段时间了,我从来没有使用 IoC 容器或 DI 来做这件事。祝你好运!

To be honest this is more of a "my two cents" contribution that actually trying to give a definitive answer since it's been a while since I developed a Windows Service but here it goes.

Making an analogy from an ASP .Net MVC app in which the configuration of the container would take place in the Global.asax class' Application_Start method and then inject the configured container into a custom Controller Factory; I believe you could, instead of configuring everything in the service's constructor, try to do it in the Main function of the executable, since it will only run once, and then inject the configured container in the constructor of the service.

I think that by doing it that way you are bootstrapping everything Composition Root of the application and the code within the service will focus on doing what it is supposed to do.

Like I said it's been a while and I never did it with an IoC container or DI for that matter. Best of lucks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文