2013-04-04 107 views
7

在你冷静下来之前,我知道有这样的重复问题,但我尝试过并没有成功找到解决方案他们。可能是我错过了一些东西,但对于WCF我还是比较新的,所以有些东西有点过头了。WCF - 接收HTTP响应时发生错误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 
+0

凡被抛出的错误?在调试器中运行您的服务。 – dthorpe 2013-04-04 21:21:50

+0

刚刚添加了我收到的错误和内部异常消息。我可以在调试器中运行它,它可以让我逐步了解上面所有的代码。一旦我在实际服务中离开GetStudentRecord()调用,几秒钟后我会收到错误。 – ledgeJumper 2013-04-04 21:30:13

回答

8

找到病根了很多脏话和思考后,如何很好的天气在外面。我从User对象内部的UserDetails对象中删除virtual关键字。

现在,它的作品!

至于为什么这造成了一个问题,我的假设是序列化或的DbContext的问题,但我将不得不寻找更多的进去,不知道。

我现在出去了。

所以供参考,如果你在这里结束了,而且不知道是怎么回事,在所有其他的事情你应该看看(大小,超时等)的方法

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

我花了整整一天找到这个答案。谢谢! – 2014-03-12 16:56:25

+0

你是天使。我也浪费了整整一天的时间。谢谢你的提示。 – stakx 2017-06-30 14:11:40

0

是否当您试图访问用户对象的某些字段中返回,您的例外发生的呢?

如果是这样,看起来您的ctx对象在调用GetStudentRecord()后被您的服务关闭。你在哪里创建ctx,你如何打开/关闭它?

(由于50代表要求我不能把这个评论...所以它在这里作为一个答案,而不是)

+0

上下文正在该方法所在的类中创建。它位于一个单独的类库中,而不是wcf服务本身。所以当类初始化时创建了上下文。我在DashboardFacade类中的其他两个方法都在做基本上是一回事,但返回不同的对象,而不是用户对象的列表。所以我不知道这与情境有关。 – ledgeJumper 2013-04-04 22:13:14

+0

我和上面的_dthorpe_有一样的问题:抛出的异常在哪里?您是在GetStudentRecord()的catch中登陆还是来自其他区域的异常? – TomEddie 2013-04-04 22:23:59

+0

唯一的例外是在其他地方扔,不是我在看任何代码。它通过我以上发布的所有方法完成。 – ledgeJumper 2013-04-04 22:46:09

0

无为我工作。我所做的是在从WCF测试客户端运行时调试服务,并且能够找到我的问题。这是与服务库丢失app.config中的连接字符串。

只是发表如果有人已经尝试了所有的其他方法,仍然无法找到问题。

1

我有同样的错误。

在我来说,我有所谓的OEM int列的表。 在模型层我有一个类(DTO)与由枚举表示该列。 有其在OEM科拉姆值无效​​表中的一行。 当我试图将使用LINQ的所有数据,有一个不是由VisualStudio中捕获的错误。当WCF试图检索消息 提出了这个错误。

4

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

WCF Services and Object Constructors

+0

救了我!实际的错误是由只读属性引起的,该属性抛出了一个空引用异常......但是这一切都隐藏在内部。 WCF疯狂! – Schalk 2015-04-16 05:28:12