Thursday, February 7, 2013

SharePoint : Accessing WCF service configuration from a timer job


I recently required to creat a timer job that calls a WCF service and process data. There are a couple of ways to achieve this.
1. For this timer job needs access to the service client proxy for the service located somewhere else. So the configuration generated by svcutil.exe needs to go into config file of the timer which is owstimer.exe. And this file (owstimer.exe.config) is located in either \14\bin or \15\bin depending on the SharePoint version. So we can put our required service configuration in this file so that timer service instance can access this data. Since it is a SharePoint asset, I would want to stay away from it not to risk loosing my configuration data.
2. Otherway of doing this is, create the service client proxy on the fly based on the config data that we store somewhere else. I my case, I wanted to stored just the location of the service in web.config file of the web application instance in which this timer is meant to run. In case if there are multiple web applications that need this kind of timer, we need to update every web application. (for what I am doing).
So I created a serviceURL entry like this in web.config of the web application I needed this timer to run in.

   


Next thing I need to do is, read this value in my timer job, create the service client proxy on the fly and call the methods required. Here is the method I have for it.
    private Service1Client configureServiceProxy()
        {
            ////Read the property bag for service url
            //string serviceURL = string.Empty;
            //if (this.Properties.ContainsKey("serviceURL"))
            //    serviceURL = this.Properties["serviceURL"].ToString();

            //if (serviceURL == null)
            //    return null;

            //Access web configuration of the web application to read the serviceURL
            SPWebApplication webApp = this.Parent as SPWebApplication;
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApp.Name);
            string appValue = config.AppSettings.Settings["serviceURL"].Value;

            //construct service client based on the url
            WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);
            EndpointAddress endpointAddress = new EndpointAddress(appValue);

            Service1Client client = new Service1Client(binding, endpointAddress);
            return client;
        }

The above class is getting the serviceURL value from web application (parent to the timer), creating the service client proxy and returning the client. So this client object can now be used to call the methods on the service.
PS: As mentioned in this post serviceURL could also be stored in the timer job's property bad which could be set using PowerShell.
To say all this is complete, I just have one more task to do, which is creating some PS script to create the above serviceURL in web applications' config file. This can be found here.

No comments:

Post a Comment