WCF Initialization Code

I created a WCF service that reads from a database and sends the results. For performance reasons, I would like to cache the tables when the service starts (what happens on Windows OnStart). But there is no such thing as constructors in WCF (right?), So it's best to come up with the Init () function and call it like this:

protected override void OnStart(string[] args) { mServiceHost = new ServiceHost(typeof(DLSService.DLSService), new Uri("http://localhost:8000/DLS")); mServiceHost.AddServiceEndpoint(typeof(DLSService.IDLSService), new BasicHttpBinding(), "ServicesHost"); ((DLSService.DLSService)mServiceHost.SingletonInstance).Init(); mServiceHost.Open(); } 

But using SingletonInstance and casting for the right type doesn't seem to me all. Are there any more elegant ways to achieve a constructor, like functionality in WCF?

+4
source share
4 answers

The recommended recommendation is to use an activation model for each call in WCF and completely disable services.

This means: every time the client makes a request, an instance of the service implementation class will be created on the server side, the requested service call will be executed, and then the service class will be destroyed again.

Therefore, placing the initialization code in the constructor of the service implementation class will be a very bad idea - it will be executed for each individual request.

What you can do is to have some kind of logic (either in your service class, or some kind of support code, for example, some kind of admin interface) that will load the tables you want to cache into a permanent cache, for example . something like an AppFabric cache. After several requests are processed for processing service instances, this shared cache can be used to improve performance.

+3
source

This can be solved using the memoization library, for example MbCache . We do exactly what you are looking for; when the application starts, we call every maintenance operation that we want to cache, and MbCache caches the result for consecutive calls (i.e., without database feedback to get the results) until the cache expires.

MbCache really has a fair amount of complexity, but once it works, it works very well and handles all the caching logic for us.

+3
source

You can use the IInstanceProvider interface to create your service; read this article for more information. Here is a sample code:

 public class CustomInstanceProvider:IInstanceProvider { public object GetInstance(InstanceContext instanceContext) { return GetInstance(instanceContext, null); } public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message) { return new DLSService.DLSService(); } public void ReleaseInstance(InstanceContext instanceContext, object instance) { } } var mServiceHost = new ServiceHost(typeof(DLSService.DLSService), new Uri("http://localhost:8000/DLS")); mServiceHost.AddServiceEndpoint(typeof(DLSService.IDLSService), new BasicHttpBinding(), "ServicesHost"); foreach (var channelDispatcher in mServiceHost.ChannelDispatchers.OfType<ChannelDispatcher>()) { foreach (var endpointDispatcher in channelDispatcher.Endpoints) { endpointDispatcher.DispatchRuntime.InstanceProvider = new CustomInstanceProvider(); } } mServiceHost.Open(); 
+1
source

With framework 4.5, you can use the configuration function in the implementation code of your service:

http://msdn.microsoft.com/en-us/library/hh205277(v=vs.110).aspx .

0
source

Source: https://habr.com/ru/post/1490639/


All Articles