c# - WCF - 接收对 http ://xxxxx/Service/的 HTTP 响应时出错

标签 c# wcf entity-framework ef-code-first

我正在我的 WCF 服务中进行此调用:

public User GetStudentRecord(string userName)
    {
        try
        {
            return new DashboardFacade().GetStudentRecord(userName);
        }
        catch (Exception ex)
        {
            ServiceFault fault = new ServiceFault();
            fault.Operation = "GetStudentRecord";
            fault.Reason = "Who knows...";
            fault.Message = ex.Message;
            throw new FaultException<ServiceFault>(fault, fault.Message, new FaultCode(fault.Reason), fault.Operation);
        }
    }

DashboardFacade().GetStudentRecord(..) 定义如下:

public User GetStudentRecord(string userName)
{
    User user = ctx.Users.Where(x => x.ADUserName == userName).SingleOrDefault();
    return user;
}

ctx 是我的 DbContext。我首先使用实体​​框架代码。用户对象如下所示:

public class User
{
    public User()
    {
        //UserDetails = new UserDetail();
    }
    [Key]
    public Guid MasterKey { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string ADUserName { get; set; }
    public string UserType { get; set; }
    public virtual UserDetail UserDetails { get; set; }
    public Nullable<DateTime> LastModifiedDate { get; set; }
}

最后,您可能需要查看 WCF 应用程序上的 web.config:

<?xml version="1.0"?>
    <configuration>
      <connectionStrings>
        <add name="NotMyDBContextDB" connectionString="[omitted]" providerName="System.Data.SqlClient"/>
        <add name="ApplicationContext" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>
      </connectionStrings>
   <system.web>
     <compilation debug="true" targetFramework="4.0"/>
   </system.web>
   <system.serviceModel>
     <behaviors>
       <serviceBehaviors>
          <behavior>
             <serviceMetadata httpGetEnabled="true"/>
             <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>

谁能指出我哪里出错了?

这正是我得到的错误:

An error occurred while receiving the HTTP response to http://localhost:5843/MyService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

Inner Exception:
The underlying connection was closed: An unexpected error occurred on a receive.

Inner Exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Inner Exception:
An existing connection was forcibly closed by the remote host

最佳答案

经过多次咒骂并想到外面的天气多么好,才找到了根本原因。我从 User 对象内的 UserDetails 对象中删除了 virtual 关键字。

现在可以了!

至于这导致问题的原因,我的假设是序列化或 DbContext 问题,但我还不确定,我将不得不深入研究它。

我现在要出去

因此作为引用,如果您在这里结束并且不知道发生了什么,除了您应该查看的所有其他事项(大小、超时等):

Check to see if your object has virtual keyword on it.

关于c# - WCF - 接收对 http ://xxxxx/Service/的 HTTP 响应时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821687/

相关文章:

c# - 我的 Windows 服务从应用程序读取不同的注册表路径

c# - 使用 iTextSharp 将不同尺寸的页面添加到 PDF

c# - 使用 WCF 将数据推送到客户端的服务器推送技术

entity-framework - Azure 移动应用-使用外键 "Bad Request"400 错误发布到表

c# - 无法从数据库 MySqL C# 获取图像

java - 为 unity 3D 创建原生 Flashlight Android Java 插件

c# - 一个 WCF 服务的两个 throttle

wcf - ServiceMetadataBehavior的HttpGetEnabled属性设置为true,HttpGetUrl属性是相对地址,但是没有http baseadd

c# - Oracle Instant Client 和 Entity Framework 的配置问题

c# - 在 EntityFramework 4.0 中处理 DataContext