WCF应用程序启动事件
首次启动WCF服务时获得通知的最佳方式是什么?
是否有类似于ASP.NET应用程序的Global.asax中的Application_Start方法?
那么,这可能有点棘手,因为调用WCF服务的首选方式是“每个呼叫”的基础上,例如,你真的没有什么是“开始”,然后只是挂起。
如果您在IIS或WAS中托pipe服务,甚至可以“按需加载”服务主机 – 当消息到达时,主机将被实例化并处理请求。
如果你自己托pipe,你可以有一个控制台或Winforms应用程序 – 所以你可以挂在那里知道什么时候开始。 如果你有一个Windows服务来托pipe你的服务主机,你很可能会覆盖ServiceBase类的OnStart和OnStop方法 – >挂钩到那里。
问题在于:你究竟想要完成什么? 只要logging或类似的东西,或者你想有东西build立在记忆中坚持?
渣子
既然它只是一个类,你可以使用一个静态的构造函数,这个构造函数将在第一次被使用的时候被调用。
public Service : IContract { public Service(){ // regular constructor } static Service(){ // Only called first time it's used. } }
我已经使用这个链接,有多种解决scheme: http : //blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx
您可以随时将global.asax文件手动添加到WCF服务应用程序,就像它在IIS上托pipe并与ASP.NETpipe道集成一样:
<%@ Application Codebehind="Global.asax.cs" Inherits="WcfApplication" Language="C#" %> public class WcfApplication : HttpApplication { protected void Application_Start() { } }
如果您有一个自托pipe的WCF服务,您可以将一个事件添加到该服务的打开,并且在这个事件中您可以分配一个静态variables,就像这个post:
//Static Variables in a WCF Service public class Post2331848 { [ServiceContract] public interface ITest { [OperationContract] string GetString(); } public class Service : ITest { public static string TheString; public string GetString() { return TheString; } } static void host_Opening(object sender, EventArgs e) { Service.TheString = "This is the original string"; } public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); //This is the magic line! host.Opening += new EventHandler(host_Opening); host.Open(); Console.WriteLine("Host opened"); Console.ReadLine(); host.Close(); } }
(来自http://www.eggheadcafe.com/community/aspnet/18/10162637/help-in-maintain-global-variable-in-wcf.aspx )
祝你好运!
Imports System.ServiceModel Imports System.ServiceModel.Description Public Class MyServiceHost Inherits Attribute Implements IServiceBehavior Public Sub AddBindingParameters(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase, endpoints As System.Collections.ObjectModel.Collection(Of System.ServiceModel.Description.ServiceEndpoint), bindingParameters As System.ServiceModel.Channels.BindingParameterCollection) Implements System.ServiceModel.Description.IServiceBehavior.AddBindingParameters End Sub Public Sub ApplyDispatchBehavior(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase) Implements System.ServiceModel.Description.IServiceBehavior.ApplyDispatchBehavior AddHandler serviceHostBase.Opened, AddressOf serviceHostBase_Opened AddHandler serviceHostBase.Closed, AddressOf serviceHostBase_Closed End Sub Public Sub Validate(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase) Implements System.ServiceModel.Description.IServiceBehavior.Validate End Sub #Region "Event Handlers" Private Sub serviceHostBase_Opened(ByVal sender As Object, ByVal e As EventArgs) End Sub Private Sub serviceHostBase_Closed(ByVal sender As Object, ByVal e As EventArgs) End Sub #End Region
Windows Communication Foundation(WCF)中用于托pipe服务的标准ServiceHost API是WCF体系结构中的一个可扩展性点。 用户可以从ServiceHost中派生自己的主机类,通常覆盖OnOpening,以便在打开服务之前使用ServiceDescription强制添加默认端点或修改行为。
http://msdn.microsoft.com/en-us/library/aa702697%28v=vs.110%29.aspx
有一个名为WebActivator的nuget包,我发现对IIS托pipe有用。
https://www.nuget.org/packages/WebActivatorEx/
您将一些程序集属性添加到您的WCF项目。
[assembly: WebActivatorEx.PreApplicationStartMethod ( typeof(MyActivator), "Start") ] public static class MyActivator { public static void Start() { // do stuff here } }