开发者

Invoking a method when WCF service is started

I have a WCF service (开发者_如何学编程VS 2010, .Net 4.0) hosted as a Windows service. What I want to do is this: I want a method which is in the service to be executed when the service is started.


I am not sure how you have used Windows Service to host your WCF service(s) but I would expect something like @SSamra described.

Anyway, below the line wherever you do .Open(); to open your service, you could initialize your wcf proxy and call your method.

Say your proxy is FirstWcfProxy, then you can do something like,

var firstWcfProxy = new FirstWcfProxy(); 
// or IFirstWcfService firstWcfProxy = new FirstWcfProxy();

firstWcfProxy.YourMethod();

EDIT: If you want to ensure the method is called as soon as the service is started, initialize your proxy below the line sHost.Open(); and invoke the method there, like I described above


how about

    using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.ServiceModel;

namespace Windows_Service
{
  public partial class WCFWindowsService : ServiceBase
  {
    ServiceHost m_serviceHost;

    protected override void OnStart(string[] args)
    {
      m_serviceHost = new ServiceHost(typeof(FirstWcfService.Service));
      m_serviceHost.Open();
    }

    protected override void OnStop()
    {
      if (m_serviceHost != null)
      {
        m_serviceHost.Close();
      }
      m_serviceHost = null;
    }
  }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜