locked
Adding custom headers to a data service context RRS feed

  • Question

  • Hi All,

    With regards to ADO.NET data services is it possible to attach custom headers onto the context? For instance I've set the Credentials on the context and now I would like to send additional user information through to the service. There doesn't seem to be any HttpContext or HttpWebRequest exposed to be able to set this on.

    I was able to send this through using my own HttpWebRequest (as included below) but I would like to do it on the context that is generated from the service reference so that I can use intellisense and LINQ on the client.

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(inputUri);
                webRequest.Method = "GET";
                webRequest.Accept = "application/xml";
                webRequest.ContentType = "application/xml";
                webRequest.Headers.Add("UserDetails", "Some additional details");
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                WebResponse webResponse = webRequest.GetResponse();
                Stream responseStream = webResponse.GetResponseStream();
                StreamReader responseStreamReader =  new StreamReader(responseStream);
                return responseStreamReader.ReadToEnd();

    Thank you in advance!

    - James
    Sunday, October 19, 2008 11:41 PM

Answers

  • You can attach custom headers to the HttpWebRequest via the DataServiceContext.SendingRequest event.

     

    context.SendingRequest += delegate(object sender, SendingRequestEventArgs args)

    {

     ((HttpWebRequest)args.Request).Headers.Add("UserDetails", "Some additional details");

    };

     

    Monday, October 20, 2008 3:29 PM
    Moderator

All replies

  • You can attach custom headers to the HttpWebRequest via the DataServiceContext.SendingRequest event.

     

    context.SendingRequest += delegate(object sender, SendingRequestEventArgs args)

    {

     ((HttpWebRequest)args.Request).Headers.Add("UserDetails", "Some additional details");

    };

     

    Monday, October 20, 2008 3:29 PM
    Moderator
  • Hi Mark,

    Thank you very much that's exactly what I was looking for!

    - James
    Tuesday, October 21, 2008 1:00 AM