Tuesday, March 31, 2009

Attach WCF services to Sitecore context


As you know, any stand-alone .NET resource such as ASPX webform or web service can be associated with a Sitecore context. With this in place, will be able to access all nice stuff from Sitecore.Context such as "ContentDatabase", "Domain", etc.

In order to do that, you can just drop the resource into one of the folders referenced by a site definition in the <sites> section.
For example, if your stand-alone resource is working with the back-end, "modules_shell" would be the best option:

<site name="modules_shell" virtualFolder="/sitecore modules/shell" physicalFolder="/sitecore modules/shell" rootPath="/sitecore/content" startItem="/home" language="en" database="core" domain="sitecore" content="master" enableWorkflow="true" />

As you can see, the "modules_shell" site is referencing the "/sitecore modules/shell" folder, so this would be the location for your resource.

It works great for aspx and asmx resources but fails for a WCF service that you may be using.
The reason for that is the FilterUrlExtensions processor in preprocessRequest pipeline that does not allow SVC extensions through. If you simply adjust the value of the first "allowed extensions"  parameter as it is shown below, your WCF service will be able to access Sitecore context:
<processor type="Sitecore.Pipelines.PreprocessRequest.FilterUrlExtensions, Sitecore.Kernel">
   <param desc="Allowed extensions (comma separated)">aspx, ashx, asmx, svc</param>
   <param desc="Blocked extensions (comma separated)">*</param>
   <param desc="Blocked extensions that stream files (comma separated)">*</param>
   <param desc="Blocked extensions that do not stream files (comma separated)"></param>
</processor>

Note, there are two processors with the same class name in web.config so don't confuse it with the one defined in the httpRequestBegin pipeline.

2 comments:

pauls said...

That solved an issue with WCF service but unfortunately triggered another one. When URL length to my web service exceeds 100 characters I'm getting the following error: "Length cannot be less than zero.
Parameter name: length.
Exception Details: System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length
Stack Trace:

[ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length]
System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) +7495111
Sitecore.Web.RequestUrl.get_ItemPath() +95
Sitecore.Pipelines.HttpRequest.SiteResolver.GetItemPath(HttpRequestArgs args, SiteContext context) +32
Sitecore.Pipelines.HttpRequest.SiteResolver.UpdatePaths(HttpRequestArgs args, SiteContext site) +54
Sitecore.Pipelines.HttpRequest.SiteResolver.Process(HttpRequestArgs args) +57
(Object , Object[] ) +61
Sitecore.Pipelines.PipelineMethod.Invoke(Object[] parameters) +36
Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +137
Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args, String pipelineDomain) +134
Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args) +50
Sitecore.Nexus.Web.HttpModule. (Object sender, EventArgs e) +252
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

"

Hamid said...

Hi Paul,
Did you manage to fix this issue. We are getting it in our web service calls as well.

Cheers,
Hamid