Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a slightly different approach for your problem:</p> <ol> <li>Use <a href="http://msdn.microsoft.com/en-us/windowsserver/ee695849" rel="nofollow" title="Windows Server AppFabric">Windows Server AppFabric</a> for service auto-start</li> <li>Use WCF infrastructure to execute custom startup code</li> </ol> <p>Re 1: The <a href="http://msdn.microsoft.com/en-us/library/ee677260.aspx" rel="nofollow">Appfabric AutoStart feature</a> should just work out of the box (provided you're not using MVC's ServiceRoute to register your services, they MUST be specified either in the Web.config's <code>serviceActivations</code> section or using physical <code>*.svc</code> files.</p> <p>Re 2: To inject custom startup code into the WCF pipeline you could use an attribute like this:</p> <pre><code>using System; using System.ServiceModel; using System.ServiceModel.Description; namespace WCF.Extensions { /// &lt;summary&gt; /// Allows to specify a static activation method to be called one the ServiceHost for this service has been opened. /// &lt;/summary&gt; [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)] public class ServiceActivatorAttribute : Attribute, IServiceBehavior { /// &lt;summary&gt; /// Initializes a new instance of the ServiceActivatorAttribute class. /// &lt;/summary&gt; public ServiceActivatorAttribute(Type activatorType, string methodToCall) { if (activatorType == null) throw new ArgumentNullException("activatorType"); if (String.IsNullOrEmpty(methodToCall)) throw new ArgumentNullException("methodToCall"); ActivatorType = activatorType; MethodToCall = methodToCall; } /// &lt;summary&gt; /// The class containing the activation method. /// &lt;/summary&gt; public Type ActivatorType { get; private set; } /// &lt;summary&gt; /// The name of the activation method. Must be 'public static void' and with no parameters. /// &lt;/summary&gt; public string MethodToCall { get; private set; } private System.Reflection.MethodInfo activationMethod; #region IServiceBehavior void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection&lt;ServiceEndpoint&gt; endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) { } void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { serviceHostBase.Opened += (sender, e) =&gt; { this.activationMethod.Invoke(null, null); }; } void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { // Validation: can get method var method = ActivatorType.GetMethod(name: MethodToCall, bindingAttr: System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public, callConvention: System.Reflection.CallingConventions.Standard, types: Type.EmptyTypes, binder: null, modifiers: null); if (method == null) throw new ServiceActivationException("The specified activation method does not exist or does not have a valid signature (must be public static)."); this.activationMethod = method; } #endregion } } </code></pre> <p>..which can be used like this:</p> <pre><code>public static class ServiceActivation { public static void OnServiceActivated() { // Your startup code here } } [ServiceActivator(typeof(ServiceActivation), "OnServiceActivated")] public class YourService : IYourServiceContract { } </code></pre> <p>That's the exact approach we've been using for quite a while and on a large number of services. The extra benefit of using a WCF <code>ServiceBehavior</code> for custom startup code (as opposed to relying on the IIS infrastructure) is that it works in any hosting environment (incl. self-hosted) and can be more easily tested.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload