2013-04-04 101 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