我正在运行由Exchange2010构建的.NET中具有自动生成的代理类的交换同步应用程序。
现在,我需要设置HttpHeaders:
service.HttpHeaders.Add("X-AnchorMailbox", Mailbox.SMTPAddress);
service.HttpHeaders.Add("X-PreferServerAffinity", "true");
就像这里描述的那样: Maintain affinity in exchange
但是它指的是EWS托管API,我无法在我的ExchangeServiceBindingObject
中找到此属性。那么如何在自动生成的代理中设置此标头?
答案 0 :(得分:1)
我会尝试使用EWS Managed Api而不是尝试自己动手。如果那不是一个选择,则可以通过覆盖ExchangeServiceBinding上生成的GetWebRequest方法来添加httpheaders,以使其到达标题,如下所示:
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;
}
}
然后,您可以调用新的派生类以添加自定义标头:
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", "some@emailaddress.com");
service.AddHeaders("X-PreferServerAffinity", "true");