WCF将图像流式传输到Web [英] WCF Stream an image to a Web
问题描述
我正在尝试将图像从WCF返回到Web应用程序,并保持收到此错误消息:响应消息的内容类型image / jpeg与绑定的内容类型不匹配(text / xml; charset = UTF-8)。如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法。
Web应用程序页面调用WCF返回图像:
I'm trying to return an image from WCF to a web application, and kept received this error message: The content type image/jpeg of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.
The web application page calls the WCF to return an image:
srQRCode.QRCodeClient qrCode = new srQRCode.QRCodeClient();
Stream image = qrCode.GetImage();
WCF qrCode.cs
WCF qrCode.cs
[OperationContract]
[WebInvoke(Method = "GET")]
Stream GetImage();
qrCode.svc.cs
qrCode.svc.cs
public Stream GetImage()
{
int width = 300;
int height = 300;
Bitmap bitmap = new Bitmap(width, height);
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
bitmap.SetPixel(i, j, (Math.Abs(i - j) < 2) ? Color.Blue : Color.Yellow);
}
}
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Position = 0;
WebOperationContext.Current.OutgoingRequest.Headers.Add("Slug", "title");
WebOperationContext.Current.OutgoingRequest.Method = "GET";
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return ms;
}
WCF.WebConfig:
WCF.WebConfig:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="TransferService" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None">
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="wcfQRCode.Service1Behavior" name="wcfQRCode.QRCode">
<endpoint address="" binding="basicHttpBinding" contract="wcfQRCode.IQRCode" bindingConfiguration="TransferService" >
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="wcfQRCode.Service1Behavior">
<!-- 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>
<endpointBehaviors>
<behavior name="imageBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
我在这里缺少什么?
What am I missing here?
推荐答案
这 [ ^ ]是研究特定错误消息的最佳方法。
This[^] is the best way to research specific error messages.
为什么不使用图像处理程序类(.ashx文件) )
why not you using image handler class (.ashx file)
我用二维码做了同样的事情,这对我不起作用,如果你这样做,你能把代码寄给我吗,这是我的邮件m.alvear@outlook .com谢谢:D
I'm doing the same thing with QR code and it doesn't work me, can you send me the code if you did it, this is my mail m.alvear@outlook.com thanks :D
这篇关于WCF将图像流式传输到Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!