25. November 2015
Hi guys, I found an interesting challenge: I tried to use Sitecore Analytics from my WCF service but the Analytics stubbornly didn’t want to be activated.
And when I found a solution, it was a wonderful feeling of catharsis. So let me share it with you.
When you want to use Sitecore Analytics from a WCF Service, you will probably face two problems.
Problem 1: Sitecore Analytics needs a Site Context
First of all, Sitecore Analytics works with a Site context. This means that you need to define a Sitecore site for your service. To do this just register a new site in the web.config, as in my example below:
In the config file under the “/Sitecore/sites” add a new node
So the site “my_service” is referenced to the MyServices location: virtualFolder="/MyServices"
It would work for HTTP handlers and webservices, but WCF service is not allowed by Sitecore by default. Now the request will be filtered. To fix this, go to the Web.config, find a pipeline “preprocessRequest” and in “Allowed Extensions” of the FilterUrlExtensions processor add a “svc” extension
Ok, now the Sitecore context should be fine. But if not, please do not despair. You always can use SiteContextSwitcher
The first problem is fixed.
Problem 2: Sitecore Analytics Uses a Session
The second problem is a session. Even after your start Tracking using the method Tracker.StartTracking(), Tracker.Active is still false.
This happens because Sitecore Analytics uses a session. And if your WCF was configured wrong, the session will be disabled.
To fix this, you need to set an Asp.net Compatibility requirements mode to “allow”:
- In an attribute of your service class:
- In the configuration of the WCF:
Now it should work!