0

我有一个使用 WCF 的简单服务器/客户端设置。我的问题是,由于连接服务器意外关闭,任何期望返回值的方法都会产生异常

服务器端的配置文件

客户端的配置文件

所有数据类都在外部项目内MSO.ErrorSystem.Shared.Datatypes 调用该方法的代码:

using (var client = Util.GetSearchServiceClient())
{
  ViewBag.Results = client.SearchReports(searchTerm, page, 50);
}

Util.GetSearchServiceClient()

public static SearchServiceClient GetSearchServiceClient()
{
  SearchServiceClient client = new SearchServiceClient();
    client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
  return client;
}

服务器端调用的方法:

 public IEnumerable<Shared.Datatypes.Report> SearchReports(string pSearchterm, int pPageNum, int pPageSize)
    {
        using (var session = DatabaseManager.Instance.DocumentStore.OpenSession())
        {
            return session.Query<Report>().Where(r => Regex.IsMatch(r.Title, pSearchterm) || Regex.IsMatch(r.Text, pSearchterm))
                                            .OrderBy(r => r.Open).OrderBy(r => r.LastEdited).Skip(pPageNum * pPageSize).Take(pPageSize);
        }
    }

编辑:有问题的例外

4

1 回答 1

0

您应该显式打开连接。

http://blogs.msdn.com/b/wenlong/archive/2007/10/26/best-practice-always-open-wcf-client-proxy-explicitly-when-it-is-shared.aspx

尝试:

using (var client = new SearchServiceClient())
{
  client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
  client.Open();
  ViewBag.Results = client.SearchReports(searchTerm, page, 50);
  client.Close();
}
于 2013-08-13T19:40:53.283 回答