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
Running a function on WCF start up
提问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 下,您还有其他选项:
- If using .NET Framework 4.5, and under IIS: You can use the service configuration method which will be called when the service is running. More info at http://msdn.microsoft.com/en-us/library/hh205277(v=vs.110).aspx.
- If you're self-hosting your service, you control when the service starts (the call to
ServiceHost.Open(), so you can initialize it there - If you're under IIS, and not on 4.5, you can use a service host factory and a custom service host to be called when the service host is being opened. At that point you can do your initialization. You can find more about service host factories at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx.
- 如果使用 .NET Framework 4.5,并且在 IIS 下:您可以使用服务运行时将调用的服务配置方法。更多信息请访问http://msdn.microsoft.com/en-us/library/hh205277(v=vs.110).aspx。
- 如果您是自托管服务,则可以控制服务何时启动(调用
ServiceHost.Open(),因此您可以在那里初始化它 - 如果您使用的是 IIS,而不是 4.5,则可以使用服务主机工厂和自定义服务主机,以便在打开服务主机时调用。此时您可以进行初始化。您可以在http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx找到有关服务主机工厂的更多信息。
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
}

