当客户端期望数据时,WCF关闭连接意外

时间:2013-08-13 18:43:38

标签: c# wcf connection

我使用WCF进行简单的服务器/客户端设置。我的问题是,由于意外关闭的连接服务器

,任何期望返回值的方法都会在异常中产生

Conifguration file on server side

Configuratoin file on client side

所有数据类都位于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);
        }
    }

编辑:Exception in question

1 个答案:

答案 0 :(得分: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();
}