EWS托管API SearchMailboxes在生产服务器上使用ServiceXmlDeserializationException生成异常

时间:2016-06-22 09:09:38

标签: c# exchangewebservices ews-managed-api exchange-server-2013

我对EWS托管API存在严重问题,虽然我在github上发布了这个项目的问题,但我没有收到任何反馈,这就是为什么我真的希望这里有人可以指出我正确的方向(上周我与EWS有另一个问题,感谢这个精彩的社区,这个问题已经解决了。)

现在关于我的问题:正如我已经告诉过你的那样,我使用EWS托管API通过简单查询查找一些消息。在只有少量项目/邮箱的测试环境中,搜索工作正常,但是当我将其投入生产时,我遇到了一个非常奇怪的问题(例外):

[ServiceXmlDeserializationException: An element node 't:Mailbox' of the type Element was expected, but node 't:SearchScope' of type Element was found.]
   Microsoft.Exchange.WebServices.Data.EwsXmlReader.InternalReadElement(XmlNamespace xmlNamespace, String localName, XmlNodeType nodeType) +451
   Microsoft.Exchange.WebServices.Data.SearchMailboxesResult.LoadFromXml(EwsServiceXmlReader reader) +464
   Microsoft.Exchange.WebServices.Data.SearchMailboxesResponse.ReadElementsFromXml(EwsServiceXmlReader reader) +47
   Microsoft.Exchange.WebServices.Data.ServiceResponse.LoadFromXml(EwsServiceXmlReader reader, String xmlElementName) +360
   Microsoft.Exchange.WebServices.Data.SearchMailboxesRequest.ParseResponse(EwsServiceXmlReader reader) +230
   Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadResponse(EwsServiceXmlReader ewsXmlReader) +157
   Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.ReadResponseXml(Stream responseStream) +87
   Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.ReadResponse(IEwsHttpWebResponse response) +403
   Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() +53
   XXX.XXX.MailManager.Admin.Controllers.MailingManagerController.Search(MailingManagerMasterModel masterModel) +722
   lambda_method(Closure , ControllerBase , Object[] ) +139
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +209
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +35
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +67
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +72
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +386
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +386
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +30
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +186
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +65
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +44
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +65
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +399
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +137

进行搜索的代码如下:

List<MailboxSearchScope> mailboxSearchScopes = new List<MailboxSearchScope>();
                foreach (SearchableMailbox mailbox in response.SearchableMailboxes)
                {
                    mailboxSearchScopes.Add(new MailboxSearchScope(mailbox.ReferenceId, MailboxSearchLocation.All));
                }

                SearchMailboxesParameters searchMailboxesParameters = new SearchMailboxesParameters();

                List<MailboxQuery> queries = new List<MailboxQuery>()
                    {
                        new MailboxQuery(String.Format("Body:{0} OR Subject:{0}", masterModel.FilteringOptions.Subject), mailboxSearchScopes.ToArray())
                    };

                searchMailboxesParameters.SearchQueries = queries.ToArray();
                searchMailboxesParameters.PerformDeduplication = false;
                searchMailboxesParameters.ResultType = SearchResultType.PreviewOnly;

                ServiceResponseCollection<SearchMailboxesResponse> responseCollection = service.SearchMailboxes(searchMailboxesParameters);

您认为问题在哪里?

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

我建议您启用跟踪https://msdn.microsoft.com/en-us/library/office/dd633676(v=exchg.80).aspx并将您的请求转储到服务器。例如,它可能在您的请求中导致失败,正确格式化的请求应该类似于以下示例。在错误中,您收到它指示MailboxSearchScope的邮箱元素丢失(例如,您不包括要搜索的任何邮箱)。跟踪转储(或fiddler捕获)将验证这一点。

  <?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2013_SP1" />
    </soap:Header>
    <soap:Body>
      <m:SearchMailboxes>
        <m:SearchQueries>
          <t:MailboxQuery>
            <t:Query>Subject:test</t:Query>
            <t:MailboxSearchScopes>
               <t:MailboxSearchScope>
                <t:Mailbox>/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=a29159c5bd8e4f7ab9127r</t:Mailbox>
                <t:SearchScope>PrimaryOnly</t:SearchScope>
                <t:ExtendedAttributes />
              </t:MailboxSearchScope>
            </t:MailboxSearchScopes>
          </t:MailboxQuery>
        </m:SearchQueries>
        <m:ResultType>PreviewOnly</m:ResultType>
        <m:Deduplication>false</m:Deduplication>
        <m:PageSize>1000</m:PageSize>
        <m:PageDirection>Next</m:PageDirection>
      </m:SearchMailboxes>
    </soap:Body>
  </soap:Envelope>

答案 1 :(得分:0)

所以,感谢Glen给出的建议,我能够解决问题 - 我启用了跟踪,并将所有请求和响应记录到xml文件中,这样我就找到了错误。

长话短说 - 不知怎的(不要问为什么,因为我不知道)在可搜索邮箱的枚举中将其作为MailboxSearchScope,我发现其中一些将ReferenceId作为空字符串从这里开始生成的请求无效。

所以,我所做的很简单:

foreach (SearchableMailbox mailbox in response.SearchableMailboxes)
                {
                    if (String.IsNullOrEmpty(mailbox.ReferenceId))
                        continue;

                    mailboxSearchScopes.Add(new MailboxSearchScope(mailbox.ReferenceId, MailboxSearchLocation.All));
                }