NullReferenceException when calling Sitecore Database.Templates[] inside WCF service

c#, sitecore, wcf

Solution

You have no Sitecore Context from your WCF service and so `Sitecore.Context.Database` is null.

You can either add `svc` to the list of allowed extensions or get the data via `Sitecore.Configuration.Factory.GetDatabase("master")`

- Attach WCF services to Sitecore context

- Access Sitecore data with WCF

Problem

I'm new to Sitecore development, so my apologies if this question is basic. I've created both an agent (scheduled task) and a WCF service and have them both added to my SiteCore project. The agent calls the same code the WCF service code calls. The issue I am having is that when I call `Database.Template[ID template]` passing in the ID container of the corresponding template for an item I want to process, it works inside the agent task, but not inside the WCF service. Any call to `Database.Template[ID id]` inside the WCF service is giving me a `NullReferenceException`, however the exact same call inside the agent gathers up the template properly. Is this due to some structural reason dependent on the calling application being a webpage instead of a webservice? Edit #1: I have svc as an allowed extension, and I have tried both ``` Sitecore.Configuration.Factory.GetDatabase("master") ``` and ``` var siteContext = Sitecore.Configuration.Factory.GetSite("websiteMaster"); siteContext.Database.Template[]; ``` both which return `NullReferenceException`. I also can successfully obtain an Item, using `GetItem`, however when I try to access the Template property of that item, I get a `NullReferenceException`.

Original source

Related problems