繁体   English   中英

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

[英]WCF - An error occurred while receiving the HTTP response to http://xxxxx/Service/

我正在我的 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.

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

WCF 服务和对象构造函数

我有同样的错误。

在我的例子中,我有一个名为 OEM 的 int 列的表。 在模型层中,我有一个类(DTO),该列由枚举表示。 表中有一行 OEM 列中的值无效。 当我尝试使用 LINQ 获取所有数据时,出现了 VisualStudio 未捕获的错误。 当 WCF 尝试检索消息时引发该错误。

我有同样的异常,但上面的答案都没有给我一个解决方案。 我通过在 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;

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

无论如何,这就是我解决问题的方式,希望这会帮助其他人

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

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

(由于有50个代表的要求,因此我无法在评论中加注...因此,这里是作为答复)

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

如果有人尝试了所有其他方法但仍然无法找到问题,请发布。

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

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

这样做你的 wcf 服务应该可以工作。

我在 Web 服务方面遇到了更糟糕的情况。

我返回了一个带有Enum 属性的简单 POCO 对象。

属性中返回的数值不在Enum values集合中,因此,我得到了那个误导性错误。

这不是我第一次遇到与 .NET 中的 Enum 值相关的问题......

简而言之,请确保您在 WebService 中的响应不会这样做:

enum exampleEnum
    {
        A = 1
    }

    class webService
    {
        public object foo() // Suppose WebService method
        {
            // Bad, make sure the Enum value is in the list defined!
            return new { age = 12, A = (exampleEnum)2 };
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM