Combined example

This example combines the tips and tricks defined in the following topics:

 

In the small code snippet example below, all these have been combined during DataServiceContext construction. The fact that the DataServiceContext (or the ODataService as it is called in your WorkZone proxy) is a partial class is also leveraged.

Example:


// As this class is partial it must be placed in the same namespace as the rest of the specification in your proxy
public partial class ODataService
{
    public ODataService() : this((ICredentials)null)
    { }
 
    public ODataService(ICredentials credentials) : this(new Uri("http://host/OData/"))
    {
        if (credentials == null)
            credentials = CredentialCache.DefaultCredentials;
        Credentials = credentials;
        Format.UseJson();
    }
 
    public string UseLogApplication { get; set; }
    public string UseLogWindow { get; set; }
    public string UseLogIdentity { get; set; }
    public string OnBehalfOfUser { get; set; }
    public bool AcceptCompression { get; set; }
 
    partial void OnContextCreated()
    {
        IgnoreMissingProperties = true;
        AcceptCompression = true;
        UseLogApplication = "MyDemoProject";
        SendingRequest2 += (sender, args) =>
            {
                if (!args.IsBatchPart)
                {
                    ApplyRequestHeaders(((HttpWebRequestMessage)args.RequestMessage).HttpWebRequest);
                }
            };
    }
 
    public void ApplyRequestHeaders(HttpWebRequest request)
    {
        if (!string.IsNullOrEmpty(UseLogApplication))
            request.Headers["UseLog-Application"] = UseLogApplication;
        if (!string.IsNullOrEmpty(UseLogWindow))
            request.Headers["UseLog-Window"] = UseLogWindow;
        if (!string.IsNullOrEmpty(UseLogIdentity))
            request.Headers["UseLog-Identity"] = UseLogIdentity;
        if (!string.IsNullOrEmpty(OnBehalfOfUser))
            request.Headers["OnBehalfOf"] = string.Concat("User=", OnBehalfOfUser);
        if (AcceptCompression)
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    }
}
				

Kopibilledet mangler