以下是我用于通过Silverlight从SharePoint 2010检索当前用户名的代码:
ClientContext clientContext = ClientContext.Current;
if (clientContext == null)
{
SharepointUser = "[Unknown]";
}
else
{
MessageBox.Show("Beginning server query now...");
clientContext.Load(clientContext.Web, s => s.CurrentUser);
clientContext.ExecuteQueryAsync((s, e) =>
{
MessageBox.Show("RESPONSE!");
SharepointUser = clientContext.Web.CurrentUser.LoginName;
MessageBox.Show("Hello, " + SharepointUser + "!");
},
(s, e) =>
{
MessageBox.Show("An error occurred: " + e.ToString());
});
}
问题是请求永远不会回来!成功/错误事件根本不会触发,并且请求似乎正在进入虚空。
有没有人有任何想法?
答案 0 :(得分:1)
MessageBox.Show可能是问题的原因。我遇到了同样的问题,通过调试我发现ExecuteQueryAsync回调中的MessageBox.Show调用抛出异常“无效的跨线程访问”。我需要像这样包装调用才能使它工作:
Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show("Request Suceeded"));
答案 1 :(得分:0)
试试这个: -
ClientContext clientContext = ClientContext.Current;
if (clientContext == null)
{
SharepointUser = "[Unknown]";
}
else
{
try
{
MessageBox.Show("Beginning server query now...");
clientContext.Load(clientContext.Web, s => s.CurrentUser);
clientContext.ExecuteQueryAsync((s, e) =>
{
MessageBox.Show("RESPONSE!");
SharepointUser = clientContext.Web.CurrentUser.LoginName;
MessageBox.Show("Hello, " + SharepointUser + "!");
},
(s, e) =>
{
MessageBox.Show("An error occurred: " + e.ToString());
});
}
catch (Exception err)
{
MessageBox.Show("Synchronous error occurred: " + err.ToString());
}
}
如果尝试启动异步操作失败,则通常会同步抛出错误,而不是将其留给异步回调。