2012-05-22 27 views
2

我遇到此問題:WCF發送INT []從客戶端到服務器

我有一個WCF服務,暴露出這種經營合同:

[OperationContract] 
void Index(int[] song, string fileName); 

在我的客戶,我有:

AudioDetectionServiceReference.AudioDetectionServiceClient client = new AudioDetectionServiceReference.AudioDetectionServiceClient(); 
client.Index(max.ToArray(), mp3Files[i]); 

其中,max是一個包含2000個int值的數組,mp3Files [i]是一個200個字符的字符串。

Web.config中有這樣的:

<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

而且具有的app.config:

<configuration> 
     <system.serviceModel> 

      <bindings> 
       <basicHttpBinding> 
        <binding name="BasicHttpBinding_IAudioDetectionService" closeTimeout="00:01:00" 
         openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
         allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
         maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
         messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
         useDefaultWebProxy="true"> 
         <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
          maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
         <security mode="None"> 
          <transport clientCredentialType="None" proxyCredentialType="None" 
           realm="" /> 
          <message clientCredentialType="UserName" algorithmSuite="Default" /> 
         </security> 
        </binding> 
       </basicHttpBinding> 
      </bindings> 
      <client> 
       <endpoint address="http://localhost:15863/AudioDetectionService.svc" 
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAudioDetectionService" 
        contract="AudioDetectionServiceReference.IAudioDetectionService" 
        name="BasicHttpBinding_IAudioDetectionService" /> 
      </client> 
     </system.serviceModel> 
    <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> 

現在到了最棘手的部分:

當你右擊服務參考,請配置服務引用,有一個選項稱爲集合類型。如果我設置的集合類型爲「Syste.Array」我得到的錯誤

The remote server returned an unexpected response: (400) Bad Request. 

隨着堆棧跟蹤:

Server stack trace: 
     at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException, ChannelBinding channelBinding) 
     at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) 
     at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) 
     at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) 
     at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) 
     at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) 

    Exception rethrown at [0]: 
     at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 
     at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 
     at SqlTest.AudioDetectionServiceReference.IAudioDetectionService.Index(Int32[] song, String fileName) 
     at SqlTest.AudioDetectionServiceReference.AudioDetectionServiceClient.Index(Int32[] song, String fileName) in blablabla\Service References\AudioDetectionServiceReference\Reference.cs:line 53 
     at SqlTest.Program.Main(String[] args) in blablabla\Program.cs:line 46 

當我將其更改爲「System.Collection.Generic.List」該錯誤神奇地消失。

我的問題是:爲什麼?

第二個問題:

爲什麼工作的呢?

client.Index(max.Take(100).ToArray(), mp3Files[i]); 

使用相同的上下文(app.config,web.config和與上面相同的代碼,選中System.Array)。

感謝, 安德烈Neagu

PS:我張貼這一點,因爲我花了2天,試圖從readerQuotas和其他的東西改變設置綁定,更改消息大小,變化值,當我不得不這樣做這個愚蠢的選擇是什麼。所以我非常生氣,至少我想從中學到一些東西。

+2

嘗試查看在Fiddler中發送到服務器的這兩種情況(數組和列表)中的請求,以查看它們之間是否存在差異。 – carlosfigueira

+0

最好的解決問題的方法是打開WCF服務跟蹤。你會看到拋出的真正異常,而不是「壞請求」。 http://msdn.microsoft.com/en-us/library/aa702726.aspx。它有點耗時,但值得。 – ErnieL

+0

另外,嘗試將''設置爲'true',這可能會給您更好的錯誤響應 – MichelZ

回答

2

一些更多的挖掘之後,這是我改變了:

的app.config

<bindings> 
    <basicHttpBinding> 
     <binding name="BasicHttpBinding_IAudioDetectionService" closeTimeout="00:01:00" 
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferSize="655360" maxBufferPoolSize="5242880" maxReceivedMessageSize="655360" 
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
      useDefaultWebProxy="true"> 
      <readerQuotas maxDepth="320" maxStringContentLength="81920" maxArrayLength="163840" 
       maxBytesPerRead="40960" maxNameTableCharCount="163840" /> 
      <security mode="None"> 
       <transport clientCredentialType="None" proxyCredentialType="None" 
        realm="" /> 
       <message clientCredentialType="UserName" algorithmSuite="Default" /> 
      </security> 
     </binding> 
    </basicHttpBinding> 
</bindings> 
<client> 
    <endpoint address="http://localhost:15863/AudioDetectionService.svc" 
     binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAudioDetectionService" 
     contract="AudioDetectionServiceReference.IAudioDetectionService" 
     name="BasicHttpBinding_IAudioDetectionService" /> 
</client> 

和服務器,看看事實,那就是爲basicHttpBinding的部分沒有名字。我認爲如果這部分有一個名字,WCF生成的默認服務將忽略它。

<bindings> 
    <basicHttpBinding> 
    <binding closeTimeout="00:01:00" 
     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
     allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
     maxBufferSize="655360" maxBufferPoolSize="5242880" maxReceivedMessageSize="655360" 
     messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
     useDefaultWebProxy="true"> 
     <readerQuotas maxDepth="320" maxStringContentLength="81920" maxArrayLength="163840" 
      maxBytesPerRead="40960" maxNameTableCharCount="163840" /> 
     <security mode="None"> 
     <transport clientCredentialType="None" proxyCredentialType="None" 
      realm="" /> 
     <message clientCredentialType="UserName" algorithmSuite="Default" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

而對於其它值,我末(乘以10)加入0。增加他們我認爲會讓你更多的轉移事件。但最重要的是,基本的HttpBinding沒有名字。如果它有一個名字,我認爲它被忽略了。

我不會將此設置爲已回答,因爲問題是爲什麼System.Array拋出異常並且System.Generic.List沒有。

相關問題