我通过在SharePoint 2010(表单身份验证)中使用Lists.asmx来获取listitem数据。
代码是这样的。
private void GetItems(string listname)
{
ListsService.ListsSoapClient client = new ListsService.ListsSoapClient();
appset = new AppSettings();
client.CookieContainer = appset.CookieSetting;
client.GetListItemsAsync(listname, null, null, null, "10", null, null);
client.GetListItemsCompleted += new EventHandler<ListsService.GetListItemsCompletedEventArgs>(client_GetListItemsCompleted);
}
void client_GetListItemsCompleted(object sender, ListsService.GetListItemsCompletedEventArgs e)
{
listBox1.ItemsSource = from element in e.Result.Descendants(XName.Get("row", "#RowsetSchema"))
select new Lists
{
Title = (string)element.Attribute("ows_LinkTitle")
};
}
当发生表单身份验证超时时,将引发未处理的CommunicationException。堆栈跟踪就在这里。
at System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest request,HttpWebResponse response,WebException responseException,HttpChannelFactory factory) 在System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest请求,HttpWebResponse响应,HttpChannelFactory工厂,WebException responseException) 在System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.ProcessResponse(HttpWebResponse response,WebException responseException) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(IAsyncResult result) 在System.Net.Browser.ClientHttpWebRequest。&lt;&gt; c_ DisplayClassa.b _8(Object state2) 在System.Threading.ThreadPool.WorkItem.doWork(Object o) 在System.Threading.Timer.ring()
即使我使用try~catch,也无法处理CommunicationException。 所以,请让我知道如何处理CommunicationException。
答案 0 :(得分:0)
您应该在调用GetListItemsAsync之前注册GetListItemsCompleted事件。另外,在try-catch块中包装GetListItemsAsync。
任何错误都将作为GetListItemsAsync调用的异常抛出,或者通过GetListItemsCompletedEventArgs报告为错误。