从WCF web服务返回图像
本文关键字:返回 图像 服务 web WCF | 更新日期: 2023-09-27 18:29:43
我已经创建了我的第一个WCF Web服务,我正试图根据一些传递的参数从中返回一个映像。我得到错误:
响应消息的内容类型image/jpeg与绑定的内容类型不匹配(text/xml;charset=utf-8)。如果使用自定义编码器,请确保IsContentTypeSupported方法已正确实现
我需要做些什么来解决这个问题?
调用该服务的我的网站的web.config文件具有以下配置:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IRestImageService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:59473/RestImageService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRestImageService"
contract="RestImageService.IRestImageService" name="BasicHttpBinding_IRestImageService" />
</client>
web服务web.config如下所示:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
服务合同:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Image/{type}/{typeid}/{imageid}/{size}/{extension}",
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
Stream Image(string type, string typeid, string imageid, string size = "lrg", string extension = "jpg");
我是WCF的新手,所以任何帮助/建议都将不胜感激!
更新在执行蒂姆的建议后,我得到了一个新的错误:
在localhost:59473/RestImageService.svc上没有可以接受该消息的侦听端点。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参见InnerException(如果存在)
我不知道如何配置web服务来解决此问题。有什么建议吗?
这就是我访问web服务的方式:
RestImageServiceClient client = new RestImageServiceClient();
client.Image(WSC.Common.BO.User.User.ImageFolder.Buyer, "27085", "BuyerPhoto", "LRG", "jpg");
我希望能够设置我的图像的src标签到web服务url一旦我让它工作。
basicHttpBinding
是SOAP(1.1版)。要启用基于REST的服务,我认为(我自己还没有做太多)需要使用webHttpBinding
。
我会试试这样的。在服务的配置文件中,进行以下更改:
<protocolMapping>
<add binding="webHttpBinding" scheme="http"/>
</protocolMapping>
此应为http
调用将默认绑定配置为webHttpBinding
。
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
上面的代码应该为webHttp
添加一个默认的端点行为。
最后,在您的客户端配置文件中,进行以下更改:
<client>
<endpoint address="http://localhost:59473/RestImageService.svc"
binding="webHttpBinding"
contract="RestImageService.IRestImageService"
name="WebHttpBinding_IRestImageService" />
</client>
我不能肯定这会起作用,但我已经对WCF(在SOAP方面)做了很多工作,所以我认为这至少会让你朝着正确的方向前进。
编辑
RestImageServiceClient
是一个SOAP客户端。要使用REST服务,您需要使用HTTP API。以下是[WCF REST Service with JSON]中的一个示例http://www.codeproject.com/Articles/327420/WCF-REST-Service-with-JSON):
WebClient client = new WebClient();
byte[] data = client.DownloadData("http://localhost:11523/Service1.svc/GetData");
Stream stream = new MemoryStream(data);
DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));
string result = obj.ReadObject(stream).ToString();
我建议在谷歌上搜索"WCF REST JSON Client exmaple"——你会得到很多点击和一些不同的方法。
另外需要注意的是,可以使用SOAP客户端进行SOAP调用,只要您在服务上公开了SOAP端点即可。