30 Apr

401 Unauthorized error on MERGE operations IIS 7 with WCF Data Service

I was getting this error when using WCF Data Services and trying to save an update to an object.  After quite a bit of googling, I found a work around that I didn’t like.  Allow the users write access to the .svc file.

I have a hard time believing that anyone would accept “allow the user write access to the file” as an answer in web situation.  In my case I was able to use that as a temporary fix because the website in question was internal and behind a firewall, but I knew there had to be a better answer.

UsePostTunneling

Post tunneling allows you to use your REST service with just the Post and Get verbs, and puts a little something extra in the header so that the service knows to treat the Post as a Merge, Put or Delete verb.  For WCF Data Services, all you need to do is set the UsePostTunneling property on the Context to true.

Here’s sample code from anonymized from my dataservice factory:

public static MyDataSvc.MyDataEntities GetDataSvc()
{
    Uri SvcUri = new Uri(string.Format("http://myserver.com/", "dataservice.svc"));
    var context = new MyDataSvc.MyDataEntities (SvcUri);
    context.UsePostTunneling = true;
    return context;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>