如何在wcf中部署客户端上下文(sharepoint online)?

时间:2013-10-31 08:55:39

标签: c# wcf sharepoint client-object-model

我使用IIS中托管的WCF从sharepoint在线获取数据。我决定使用this示例处理我的所有错误。在我的代码中,我使用的是需要以正确方式处理的ClientContext类

  ClientContext _context = new ClientContext(_url);
  var list = _context.Web.Lists.GetByTitle(ListName);
  ListItem item = list.GetItemById(id);
  _context.Load(item, i => i[Description], i => i[Picture], i => i[Title], i => i.Id);
  _context.ExecuteQuery();
  _context.Dispose();

但是如果我在_context.ExecuteQuery()中得到异常;我去了HandleError方法

public bool HandleError(Exception error)
{
    return true;
}

问题是:如何在这种情况下处理客户端上下文?

1 个答案:

答案 0 :(得分:2)

走这条路:

using(ClientContext _context = new ClientContext(_url))
{
    var list = _context.Web.Lists.GetByTitle(ListName);
    ListItem item = list.GetItemById(id);
    _context.Load(item, i => i[Description], i => i[Picture], i => i[Title], i => i.Id);
    _context.ExecuteQuery();
}

try
{  
  ClientContext _context = new ClientContext(_url);
  var list = _context.Web.Lists.GetByTitle(ListName);
  ListItem item = list.GetItemById(id);
  _context.Load(item, i => i[Description], i => i[Picture], i => i[Title], i => i.Id);
  _context.ExecuteQuery();
}
finally
{  
  _context.Dispose();
}