"System.ServiceModel.CommunicationException" when trying to pass a bitmap through WCF
I'm trying to pass a Bitmap through WCF, but it throws
System.ServiceModel.CommunicationException was unhandled by user code
Message=The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9650000'.
An开发者_如何学Pythony idea what might be causing this?
Thanks!
The cause is that the Bitmap
type is a .NET specific type (with lots of Win32 specifics inside it, too) which isn't serializable into XML. You can't really pass it back as a parameter of a method - what you might be able to do is stream it back on a parameter of type Stream
and using streamed transfer in WCF.
Or another way (if your bitmaps aren't too big) would be to convert the Bitmap
into a string, using Base64 encoding, send it back as a string, and then re-encode it on the client side.
Assuming that you're hosting your WCF service in IIS (you didn't mention any details).
Try bumping up some numbers in both the client and server config files (remember the settings must match).
<binding name="Binding_ISomeervice" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="1000"
maxBufferPoolSize="524288" maxBufferSize="2147483647" maxConnections="100"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binding>
Also enable debug in the server's web config file:
<system.web>
<compilation debug="true" />
</system.web>
If you can, put try/catch (with logging) around your service call implementation, this will tell you if you're implementation is throwing or the hosting environment is throwing.
Heh, that is one of the more irritating messages from WCF. My guess is that the message size is too big. The most reliable way I've found to figure this stuff out is to use WCF tracing as described at http://msdn.microsoft.com/en-us/library/ms733025.aspx and SvcTraceViewer.exe.
My answer has two parts: advice for this situation, and advice on better error-reporting and tracing for the future.
Since you are dealing with bitmaps, have you considered using WCF's raw programming model? This blog post details how to use the programming model on the service side, and this follow-up blog post details how to use it on the client side. I've seen it used quite a bit for file upload and image transfer scenarios, so it might help your situation as well!
CommunicationException is genericized and does not reveal the underlying exception. In the future, when reporting errors, I'd recommend turning on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, and then report the nested exception you get in Detail.
For example:
<behaviors>
<serviceBehaviors>
<behavior
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
As for turning on tracing, below is my advice, which I have pasted in a few other answers already.
Enable tracing on the service side, generate tracing logs, and analyze with SvcTraceViewer. To do this, follow the instructions at this MSDN article on using the service trace viewer.
Use Fiddler to monitor the wire traffic on both the client side and the service side.
Generally, once you do this, you should plenty of more info on what's going funky at the service side and can diagnose the issue pretty quickly.
精彩评论