当我的 WCF 服务启动时执行某些操作

发布于 2024-12-01 22:56:26 字数 133 浏览 0 评论 0原文

我想在 WCF 服务启动后立即执行一些操作。怎样才能做到呢?

事实上,我应该每 10 分钟更新一次服务的一些变量。所以我把更新代码放在一个线程中。但我不知道服务启动时如何启动这个线程(WCF服务中有类似Form_Load事件的东西吗?)

I want to do something just after my WCF service started. How can do it?

In fact,I should update some variable of my service every 10 minutes. So I put my update code in a thread. But I dont know how start this thread when service started (Is there anything liked Form_Load event in WCF services?)

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

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

发布评论

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

评论(4

寻找我们的幸福 2024-12-08 22:56:26

WCF 服务中通常没有任何部分“只是挂在内存中”准备执行某些操作...WCF 不是 ASP.NET!

在 IIS 中托管时的默认设置是这样的:

  • IIS 侦听特定端口/URL 上的请求 - 内存中的任何位置都没有 WCF 服务的任何痕迹

  • 当第一个请求到来时,IIS 将启动一个 ServiceHost - 一个可以“托管”服务的类

  • 然后,该服务主机将查看已传入的请求,并根据目标 URL 做出决定要实例化哪个服务类来处理此请求。然后创建服务类(您的服务实现),并调用并执行该服务类上的适当方法,一旦完成,服务类就会被释放

所以基本上,您可以连接到以下两点:

  1. 您可以创建自己的自定义 ServiceHost 类,该类在实例化时执行某些操作

  2. 您可以添加一些“初始化”代码每个服务类方法来处理您的需求

There is typically no parts of your WCF service that is "just hanging around" in memory ready to do something.... WCF is NOT ASP.NET !

The default setup when hosting in IIS is this:

  • IIS listens on a specific port/URL for a request - there's not a single trace of your WCF service anywhere in memory

  • when a first request comes in, IIS will spin up a ServiceHost - a class that can "host" a service

  • this service host will then look at the request has come in and depending on the target URL, it will decide which service class to instantiate to handle this request. The service class (your service implementation) is then created and the appropriate method on that service class is called and executed, and once that's completed, the service class is disposed

So basically, there are two points where you can hook into:

  1. you could create your own custom ServiceHost class that will do something when it gets instantiated

  2. you can add some "initialization" code to each of your service class methods to handle your needs

归属感 2024-12-08 22:56:26

保持线程在服务器上运行是很困难的。一旦最后一个会话终止,应用程序就会关闭。一些托管提供商还会按计划回收应用程序池,这会消除保持线程运行的任何机会。

除此之外,WCF 服务实际上并未运行。它们的行为就像由请求触发的网页。添加初始化代码的明智位置是 Global.asax 中的 Application_Start 中。当应用程序启动时(发出第一个请求),这将被调用一次。

如果您希望对服务的每个请求执行某些操作,您也可以在 Global.asax 中挂钩 Application_BeginRequest 事件。

It's difficult to keep a thread running on a server. As soon as the last session terminates the application shuts down. Some hosting providers also recycle the app pool on a schedule which kills any chance of keeping a thread running.

That aside, WCF Services don't actually run. They act like web pages triggered by a request. The sensible place to add init code would be in your Application_Start in Global.asax. This would get called once when the application starts (the first request is made).

If you would rather do something on each request to your services, you could hook the Application_BeginRequest event also in Global.asax.

失与倦" 2024-12-08 22:56:26

您可以创建一个实例化服务,它将在服务启动时调用构造函数:

[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class InstanceService
{
    private int _intValue;

    public InstanceService()
    {
        _intValue = 456;
    }

    [OperationContract]
    public int GetData()
    {
        return _intValue;
    }
}

在此服务上调用 GetData() 将返回一个值为 456 的整数。

You can create an instanced service, which will call the constructor upon the start of your service:

[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class InstanceService
{
    private int _intValue;

    public InstanceService()
    {
        _intValue = 456;
    }

    [OperationContract]
    public int GetData()
    {
        return _intValue;
    }
}

Invoking GetData() on this service will return an integer with a value of 456.

新雨望断虹 2024-12-08 22:56:26

如果您能够控制如何托管它,则可以实现这一目标。如果可以,请将 WCF 服务托管为 Windows 服务或临时可执行文件。然后你就可以轻松实现你想要的。如果您绑定到 IIS 托管,则必须按照其他人的建议进行操作并根据请求进行处理。

如果不需要 IIS,请阅读自托管 wcf。

This can be achieved if you are able to control how to host this. If you can, host your WCF service as a Windows service or an ad hoc executable. Then you can achieve what you want with ease. If you are bound to IIS hosting you must do as others have suggested and handle it per request.

Read up on self hosting wcf if IIS is not required.

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