Search code examples
proxyexchange-serverexchangewebservices

Set HttpHeader in ews autogenerated proxy


Im running a exchange synchronisation application with an autogenerated Proxy Class in .NET built with Exchange2010.

Now, i need to set the HttpHeaders:

service.HttpHeaders.Add("X-AnchorMailbox", Mailbox.SMTPAddress);
service.HttpHeaders.Add("X-PreferServerAffinity", "true");

like described here: Maintain affinity in exchange

But it refers to the EWS Managed API and i cannot find this Property in my ExchangeServiceBindingObject. So how can I set this header in my autogenerated proxy?


Solution

  • I would try to use the EWS Managed Api rather than trying to roll your own. If that if that is not an option, you can add httpheaders by overriding the generated GetWebRequest method on ExchangeServiceBinding to get to the headers like so:

    public class ExchangeServiceBindingWithHeaders : EwsProxy.ExchangeServiceBinding
    {
        private NameValueCollection _customHeaders = new NameValueCollection();
    
        public void AddHeaders(string key, string value)
        {
            _customHeaders.Add(key, value);
        }
    
        protected override WebRequest GetWebRequest(Uri uri)
        {
            HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(uri);
            req.Headers.Add(_customHeaders);
    
            return req;
        }
    }
    

    Then you can call the new derived class to add the custom headers:

    ExchangeServiceBindingWithHeaders service = new ExchangeServiceBindingWithHeaders();
    service.RequestServerVersionValue = new RequestServerVersion();
    service.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
    service.Credentials = new NetworkCredential("<username>", "<password>", "<domain>");
    service.Url = @"https://<FQDN>/EWS/Exchange.asmx";
    
    service.AddHeaders("X-AnchorMailbox", "[email protected]");
    service.AddHeaders("X-PreferServerAffinity", "true");