C# 在 WCF 启动时运行函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10841635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:29:47  来源:igfitidea点击:

Running a function on WCF start up

c#.netwcfrest

提问by SmashCode

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 implement the cache, my question is strictly about having the function run when the service starts. The service will be RESTful.

我不确定它是否可能,但我希望在 WCF 服务启动后立即运行一个函数来生成初始缓存数据。我现在不担心如何实现缓存,我的问题是在服务启动时运行该函数。该服务将是 RESTful。

The service will eventually be hosted in IIS and is using .Net Framework 4.5

该服务最终将托管在 IIS 中并使用 .Net Framework 4.5

采纳答案by carlosfigueira

What @KirkWoll suggested works, but only if you're in IIS and that's the only AppInitialize static method under App_Code. If you want to do initialization on a per-service basis, if you have a different AppInitialize method or if you're not under IIS, you have these other options:

@KirkWoll 建议的内容有效,但前提是您在 IIS 中并且这是 App_Code 下唯一的 AppInitialize 静态方法。如果您想在每个服务的基础上进行初始化,如果您有不同的 AppInitialize 方法,或者您不在 IIS 下,您还有其他选项:

An example of a custom factory is shown below:

自定义工厂的示例如下所示:

public class MyFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
        host.Opening += new EventHandler(host_Opening);
        return host;
    }

    void host_Opening(object sender, EventArgs e)
    {
        // do initialization here
    }
}

}

}

回答by Kirk Woll

The easiest way is to create an App_Codefolder underneath your WCF project root, create a class (I'll call it Initializerbut it doesn't matter. The important part is the method name) like so:

最简单的方法是App_Code在您的 WCF 项目根目录下创建一个文件夹,创建一个类(我会调用它,Initializer但这并不重要。重要的部分是方法名称),如下所示:

public class Initializer
{
    public static void AppInitialize()
    {
        // This will get called on startup
    } 
}

More information about AppInitializecan be found here.

AppInitialize可以在此处找到有关的更多信息。

回答by Ziggler

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

就我而言,我确实喜欢下面的内容。我有托管 WCF Rest 服务的 Windows 服务项目。我在 Windows 服务项目 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
}