Running a function on WCF start up

后端 未结 3 403
北荒
北荒 2020-12-13 18:46

I\'m not sure if its possible, but I\'d like to have a function run as soon as a WCF service is started to generate initial cache data. I\'m not worried now about how to imp

3条回答
  •  被撕碎了的回忆
    2020-12-13 19:03

    In my case I did like below. I have Windows service project that hosted a WCF Rest service. I wrote below code in my windows service project MyService.cs

    protected override void OnStart(string[] args)
    {
        try
          {
            ServiceHost myServiceHost = new ServiceHost(typeof(myservice));
            myServiceHost.Opening += OnServiceHostOpening;
            myServiceHost.Open();
          }
          catch (Exception ex)
          {
             //handle exception
          }
    }
    
    private void OnServiceHostOpening(object sender, EventArgs e)
    {
       //do something
    }
    

提交回复
热议问题