Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a function on WCF start up

Tags:

rest

c#

.net

wcf

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.

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

like image 258
SmashCode Avatar asked May 31 '12 21:05

SmashCode


People also ask

How do I run a WCF service?

To open WCF Test Client, open Developer Command Prompt for Visual Studio and execute WcfTestClient.exe. Select Add Service from the File menu. Type http://localhost:8080/hello into the address box and click OK. Make sure the service is running or else this step fails.

What are 3 basic WCF configurations required for hosting a WCF service?

There are three types of hosting environments for WCF services: IIS, WAS, and self-hosting. The term “self-hosting” refers to any application that provides its own code to initialize the hosting environment. This includes console, Windows Forms, WPF, and managed Windows services.

How do I host a WCF service in a managed Windows service?

Open your Visual Studio if you are using Windows Vista or Windows 7, then open Visual Studio in Administrator mode and create a new project of type Windows Service like in the following diagram. Add a reference to your WCF service library from Project Add Reference Browse Select your WCF service .

What is the entry point of WCF service?

In order to consume a WCF service, a client application must first obtain or generate a proxy class. We also need a configuration file to specify things such as the binding of the service, the address of the service, and the contract. To generate these two files, we can use the svcutil.exe tool from the command line.


2 Answers

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

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

More information about AppInitialize can be found here.

like image 192
Kirk Woll Avatar answered Oct 05 '22 02:10

Kirk Woll


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:

  • 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.

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     } } 

}

like image 23
carlosfigueira Avatar answered Oct 05 '22 00:10

carlosfigueira