Ado.Net Data Services – Are you experienced?
I am in the process of deploying my first project using Ado.net Data Services using an Entity Framework Context and I thought I would share some of my experience.
1) Authentication
Data services comes with the ability to apply credentials on the context, but if you’re wanting to roll-your-own method of passing credentials (such as a cookie) you’re not out of luck. The client ObjectContext supports a “SendingRequest” that is fired every time an HttpWebRequest object is created. In the args object you can get access to the web request. The code looks something like this:
private static void Context_SendingRequest(object sender, SendingRequestEventArgs e)
{
if (e != null && (e.Request as HttpWebRequest != null))
{
var Request = e.Request as HttpWebRequest;
Request.CookieContainer = new CookieContainer();
Request.CookieContainer.Add(new Cookie("token", "value") { Domain = Request.RequestUri.Host });
}
}
You can then use the cookie you in the service by overriding the “OnStartProcessingRequest” method.
1: protected override void OnStartProcessingRequest(ProcessRequestArgs args)
2: {
3: try
4: {
5: var Cookie = HttpContext.Current.Request.Cookies["token"];
6:
7: var Ticket = FormsAuthentication.Decrypt(Cookie.Value);
8:
9: if (Ticket != null && Ticket.Expired)
10: {
11: throw new DataServiceException("");
12: }
13:
14:
15: }
16: catch (Exception)
17: {
18:
19: throw new DataServiceException("Invalid Login");
20: }
21:
22: base.OnStartProcessingRequest(args);
23: }
2) .SVC mapping in IIS
For the .svc mime type mapping in IIS you’ll need to make sure the “POST”, “PUT’” and “DELETE” methods are supported.
3) Object Context and connection string
After having trouble creating an object context using an .edmx file directly in an asp.net web site I moved it do a class library and everything seems fine now. Also, it’s very important to make sure your connection string is formatted correctly. When you create and ObjectContext using .edmx it will create a connection string in the .config file that corresponds to the class name of the object context. Additionally, it is very important the connection string “metadata” section be formatted as “metadata=res://*/Context.csdl|res://*/Context.ssdl|res://*/Context.msl;”. And this is where it gets to be interesting. When you create the .edmx file a custom Object Context is created, but also portions of the .edmx file are embedded as resources in the compiled assembly. The “res://*/” paths, which are pipe separated (“|”), need to correspond to these resource names in the compiled assembly. If you don’t don’t know the names you can always use RedGate Reflector to find them. Thanks to Danny Simmons at Microsoft for pointing the right me in the right direction.
4) Turn on debugging output if needed.
There are two things you can do to get at error messages in the service output if necessary.
- Apply the ServiceBehavior attribute to the service. When you do this you want to include (“IncludeExceptionDetailInFaults = true”).
- In the “InitalizeService” method include “config.UseVerboseErrors = true”.
This will allow you to get at the exception details. Make sure you turn off when in production.
Thanks for this – great post. I’m struggling a bit with a data services project myself at the mo. These tips will help.
James Munro
March 31, 2009 at 10:39 am