开发者

Do something when my WCF service started

开发者 https://www.devze.com 2023-03-31 16:28 出处:网络
I want to do somethingjust 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

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 t开发者_运维知识库hread when service started (Is there anything liked Form_Load event in WCF services?)


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消