WCF - 接收到http:// xxxxx / Service /的HTTP响应时发生错误

时间:2013-04-04 21:05:30

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

在你投票之前,我知道有一些重复的问题,比如这个,但我尝试过并没有成功找到解决方案。可能是我错过了一些东西,但我对WCF来说还是比较新的,所以有些东西让我头脑发热了。

我正在我的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。我正在使用Entity Framework Code First。 User对象如下所示:

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

7 个答案:

答案 0 :(得分:10)

在经过大量宣誓之后找到根本原因,并想到外面的天气有多好。我从User对象内的UserDetails对象中删除了虚拟关键字。

现在有效!

至于为什么这会引起问题,我的假设是序列化或DbContext问题,但我不得不更多地考虑它,不确定。

我现在要出去了。

所以作为参考,如果你在这里结束并且不知道发生了什么,在你应该看的所有其他事情中(大小,超时等):

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

答案 1 :(得分:7)

我遇到了这个问题,在我的情况下,问题是WCF服务返回的是一个只有getter而没有setter的属性的类。我试图阻止接收器修改属性。要解决这个问题,请看...

WCF Services and Object Constructors

答案 2 :(得分:1)

我遇到了同样的错误。

在我的情况下,我有一个名为OEM的int列的表。 在模型层中,我有一个类(DTO),该列由Enum表示。 表中有一行,OEM列中的值无效。 当我尝试使用LINQ提供所有数据时,VisualStudio未捕获到错误。 当WCF尝试检索消息时,会引发该错误。

答案 3 :(得分:0)

当您尝试访问返回的User对象的某个字段时,是否会发生异常?

如果是这样,在调用GetStudentRecord()之后,您的服务似乎正在关闭您的ctx对象。你在哪里创建ctx以及如何打开/关闭它?

(由于50个代表要求我无法将其放在评论中......所以它在这里作为答案而已)

答案 4 :(得分:0)

这些方法都不适合我。我所做的是在从WCF测试客户端运行时调试服务,我能够找到我的问题。这与服务库中缺少app.config中的连接字符串有关。

如果有人尝试过所有其他方法但仍然无法找到问题,那就发帖了。

答案 5 :(得分:0)

我有相同的异常,但以上答案均未提供解决方案。 我通过在web.config中定义跟踪来找出错误:

<system.diagnostics>
<sources>
  <source propagateActivity="true" name="System.ServiceModel" switchValue="Information, ActivityTracing">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
      <add initializeData="C:/temp/tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="traceListener">
        <filter type="" />
      </add>
    </listeners>
  </source>
</sources>
</system.diagnostics>

我在本地运行网站,触发了一些失败的调用,并打开了跟踪日志,选择了失败的条目,在该条目中我看到了这个异常:

不能在LINQ to Entities查询中构造实体或复杂类型“ Person”。

在查询之外构造人员可以解决此问题,而不是:

 var persons = (from p in db.Persons
                  select new Person
                  {
                      Id = p.Id,
                      Name = p.Name,
                      LastName = p.LastName,
                      Email = p.Email
                  }
              ).AsEnumerable();

  return persons;

我必须做:

var persons = (from p in db.Persons
                  select new 
                  {
                      Id = p.Id,
                      Name = p.Name,
                      LastName = p.LastName,
                      Email = p.Email
                  }
              ).AsEnumerable()
                //construct the complex type outside of DB  (to prevent the exception that model cannot be constructed in linq to entities)
                .Select(item =>

                    new Person
                    {
                        Id = item.Id,
                        Name = item.Name,
                        LastName = item.LastName,
                        Email = item.Email
                    }).ToList(); 

            return persons;

如果异常消息更全面,我可以节省很多时间,从本主题概述的所有不同原因来看,我想人们会同意我的看法。

无论如何,这就是我找出问题的方式,希望这可以帮助其他人

答案 6 :(得分:0)

也许您需要禁用延迟加载和代理创建,如下所示:

  db.LazyLoadingEnabled = false;
  db.ProxyCreationEnabled = false;

执行此操作即可正常工作。