【发布时间】:2012-05-22 21:21:19
【问题描述】:
我遇到了这个问题:
我有一个 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)。
谢谢, 安德烈·内古
PS:我发布此内容是因为我花了 2 天时间尝试更改绑定设置、更改消息大小、更改 readerQuotas 和其他内容的值,而我必须做这个愚蠢的选择。所以我很生气,我至少想从中学到一些东西。
【问题讨论】:
-
尝试在 Fiddler 中查看在两种情况下(数组和列表)发送到服务器的请求,看看它们之间是否存在差异。
-
最好的故障排除方法是在服务上打开 WCF 跟踪。您将看到引发的真正异常,而不是“错误请求”。 msdn.microsoft.com/en-us/library/aa702726.aspx。这有点耗时,但值得。
-
另外,尝试将
<serviceDebug includeExceptionDetailInFaults="false"/>设置为true,这可能会给您更好的错误响应 -
尝试使用 Fiddler,但客户端目前是一个控制台应用程序,它没有显示任何信息。显示浏览器活动。
-
即使我为调试模式设置了这些值,错误仍然是400。没有返回其他信息。
标签: c# .net wcf visual-studio-2010