WCF从客户端向服务器发送int [] [英] WCF sending int[] from client to server
问题描述
我遇到了这个问题:
我有一个WCF服务,它公开了此操作合同:
I have a WCF service, that exposes this operation contract:
[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个字符的字符串.
Where max is an array of 2000 int values, and mp3Files[i] is a string of 200 chars.
Web.config具有此功能:
Web.config has this:
<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具有:
And app.config has:
<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",则会出现错误
When you right click on service reference, go to configure service reference, there is an option called Collection type. If I set the collection type to "Syste.Array" I get the error
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"时,错误神奇地消失了.
When I change it to "System.Collection.Generic.List", the error magically disappears.
我的问题是:为什么?
第二个问题:
为什么有效?
client.Index(max.Take(100).ToArray(), mp3Files[i]);
使用相同的上下文(app.config,web.config和与上述相同的代码,并选择System.Array).
using the same context (app.config, web.config, and the same code as above, with the System.Array selected).
谢谢,安德烈·尼古(Andrei Neagu)
Thanks, Andrei Neagu
PS:之所以发布此信息,是因为我花了2天的时间来尝试更改绑定设置,更改消息大小,更改readerQuotas和其他内容的值,而当时我不得不做这种愚蠢的选择.所以我很生气,我想至少从中学到一些东西.
PS: I post this because I spent 2 days on trying to change settings for bindings, change the message size, change values from readerQuotas and other stuff, when all I had to do what this stupid selection. So I am quite pissed and I wanna at least learn something from this.
推荐答案
再进行一些挖掘之后,这就是我所做的更改:
After some more digging, this is what I changed:
app.config
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生成的默认服务将忽略它.
and for the server, look at the fact that there is no name for the basicHttpBinding section. I think that if this section has a name, the default service generated by WCF will ignore it.
<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>
对于其他值,我在末尾添加了0(乘以10).我认为增加他们可以让您转移更多活动.但最重要的是,basicHttpBinding没有名称.如果有名字,我认为它被忽略了.
And for the other values, I added an 0 at the end (multiplied by 10). Increasing them I think will let you transfer event more. But the most important, the basicHttpBinding has no name. If it had a name, I think it was ignored.
我不会将其设置为已回答,因为问题是为什么System.Array会引发异常而System.Generic.List不会.
I will not set this as answered, because the question was why System.Array throws exception and System.Generic.List does not.
这篇关于WCF从客户端向服务器发送int []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!