2012-04-06 90 views
1

我寫了一個WCF服務,從我的SQL Server數據庫中讀取數據。它工作正常,當調用返回的所有字符串,而是一種方法,如果我調用返回它提供有關超時和太多的數據,這是沒有意義的,以我的一些錯誤崩潰的int的方法...爲什麼我的WCF服務不會返回一個整數?

這裏是我的web服務代碼:

public List<Track> getTrack() 
{ 
    List<Track> trackList = new List<Track>(); 

    SqlConnection dbConn = connectToDb(); 

    string _selectQuery = string.Format("SELECT Date, Track, KeyID FROM hdData ORDER BY Track"); 

    try 
    { 
     dbConn.Open(); 
     SqlCommand cmd = new SqlCommand(_selectQuery, dbConn); 
     SqlDataReader reader = cmd.ExecuteReader(); 

     while (reader.Read()) 
     { 
      Track Dat = new Track(); 
      Dat.Date = (string)reader[0]; 
      Dat.TrackName = (string)reader[1]; 
      Dat.KeyId = (int)reader[2]; 
      trackList.Add(Dat); 
     } 

     dbConn.Close(); 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine(e.ToString()); 
    } 

    return trackList; 
    } 

如果我拿出KeyId場正​​常工作...... KeyId在數據庫中被定義爲類型int,是一個自動遞增的字段。

我甚至嘗試它強制轉換爲varchar但同樣的結果...

我在做什麼錯?

問候, 迪安

確切的錯誤和跟蹤類如下:

好的,確切的錯誤是:

的最大消息大小配額用於傳入消息(65536)具有已超過 。要增加配額,請在適當的綁定元素上使用MaxReceivedMessageSize 屬性。

服務器堆棧跟蹤:
在System.ServiceModel.Channels.HttpInput.ThrowMaxReceivedMessageSizeExceeded()
在System.ServiceModel.Channels.HttpInput.GetMessageBuffer()
在System.ServiceModel.Channels.HttpInput.ReadBufferedMessage(流 的inputStream)
在System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(例外& requestException)
在System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(時間跨度超時)
在System.ServiceModel.Channels.RequestChannel.Request(消息消息,時間跨度 超時)
在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息 消息,時間跨度超時)
在System.ServiceModel.Channels.ServiceChannel .CALL(字符串動作, 布爾單向,ProxyOperationRuntime操作,對象[]插件, 對象[]奏,時間跨度超時)
在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage 包括methodCall,ProxyOperationRuntime操作)
在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)[0]時

異常重新拋出:
在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即時聊天 reqMsg,即時聊天retMsg)
在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke (MessageData & msgData,Int32 type)
at IService1.getTrack()
at Service1Client。getTrack()

內部異常:
已超過傳入消息的最大消息大小配額 (65536)。要增加配額,請在適當的綁定元素上使用MaxReceivedMessageSize屬性。

和軌道類:

[DataContract] 
public class Track 
{ 
    string _Date, _TrackName; 
    int _KeyId; 

    [DataMember] 
    public string Date 
    { 
     get { return _Date; } 
     set { _Date = value; } 
    } 

    [DataMember] 
    public string TrackName 
    { 
     get { return _TrackName; } 
     set { _TrackName = value; } 
    } 

    [DataMember] 
    public int KeyId 
    { 
     get { return _KeyId; } 
     set { _KeyId = value; } 
    } 

} 
+3

你能不能給我們確切的錯誤?還有Track類。你也應該關閉你的讀者。 – Fabske 2012-04-06 07:43:30

+0

也許如果你使用MsSql CE數據庫你的KeyId不是一個int,而是一個GUID。然後在嘗試轉換爲int時出現錯誤。 – SynerCoder 2012-04-06 07:51:47

+1

啓用[wcf tracing](http://mkdot.net/mknetug/b/dejan/archive/2008/10/23/wcf-tracing-and-message-logging.aspx)查看詳細的錯誤消息並向我們顯示錯誤。 – Reniuz 2012-04-06 07:57:37

回答

2

當你說:「如果我拿出keyid的領域」你的意思是從田徑類中刪除?如果是這樣,您所返回的Track列表的大小可能接近您的端點綁定MaxReceivedMessageSize(65536)?如果是這種情況,那麼通過從Track類中移除_KeyId int來減小該List的大小可能會將總體返回的數據大小減小到低於此限制。
嘗試在終點綁定中增加此限制。您可能需要爲服務器和客戶端執行此操作。例如:

maxBufferPoolSize="10000000" maxBufferSize="10000000" maxReceivedMessageSize="10000000"> 
    <readerQuotas maxDepth="32" 
     maxStringContentLength="10000000" maxArrayLength="10000000" 
     maxBytesPerRead="10000000" maxNameTableCharCount="10000000" /> 
+0

中增加'MaxReceivedMessageSize'我在web.config中增加了maxRecievedMessageSize。現在的問題是,當我將它發佈到我的虛擬主機(www.winhost.com)時似乎不起作用。 我會假設網絡主機會使用我將發佈的任何web.config文件? – 2012-04-06 09:44:19

+0

那是另一個問題。我不能真正幫助你,因爲我只有在贏得服務託管沒有IIS託管的WCF服務器的經驗。作爲一個單獨的問題,你可能會更好。 – Ricibob 2012-04-06 09:55:37

+0

好的感謝Ricibob,我會先發送給winhost團隊,看看他們能否回答。 – 2012-04-06 09:59:49

1

基本上,您需要增加消息的大小。要做到這一點,您需要創建一個綁定配置用於您正在使用的任何綁定 - 我在此使用basicHttpBinding作爲示例。所以,你需要定義綁定配置(包括您的服務器上,並在客戶端的配置),以及您的服務以及你的客戶需要引用綁定配置:

<system.serviceModel> 
    <bindings> 
    <!-- use whichever binding you're using here! --> 
    <basicHttpBinding> 
     <!-- define a binding configuration with a name --> 
     <binding name="LargeData" 
       maxBufferSize="999999" maxReceivedMessageSize="999999" /> 
    </basicHttpBinding> 
    </bindings> 
    <services> 
    <service name="Namespace.YourServiceClass"> 
     <!-- your endpoints - both on the server as well as on the client - need 
      to make reference to that defined binding configuration --> 
     <endpoint name="test" 
       address="...." 
       binding="basicHttpBinding" 
       bindingConfiguration="LargeData" 
       contract="Namespace.IYourServiceContract" /> 
    </service> 
    </services> 
</system.serviceModel> 

,並在客戶端,你將有這個:

<client name="...."> 
     <endpoint name="test" 
      address="...." 
      binding="basicHttpBinding" 
      bindingConfiguration="LargeData" 
      contract="Namespace.IYourServiceContract" /> 
    </client> 
0

我在等待配額錯誤,這就是爲什麼我問你確切的錯誤信息。

請仔細閱讀此鏈接:http://www.aspnet101.com/2010/08/wcf-performance-best-practices/ 尤其是配額部分(但我認爲整篇文章將幫助您更好地瞭解WCF)。

然後,不要忘了,你也必須更改這些設置(配額)在客戶端(即刷新Web引用一旦更新了服務器)

相關問題