【发布时间】:2011-12-01 10:59:33
【问题描述】:
我有这个非常简单的 DTO:
[DataContract]
public class DTO
{
[DataMember]
public byte[] Image {get; set; }
}
还有这个非常简单的服务:
[ServiceContract]
public interface IFooService
{
[WebGet(
UriTemplate = "",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
List<DTO> GetDTOs();
}
在我的 global.asax 中,我有:
RouteTable.Routes.Add(new ServiceRoute("foo",
new WebServiceHostFactory(), typeof(FooService)));
现在,当我从浏览器调用它时,我得到一个 JSON 格式的字节数组。目前很好。现在,如何将字节数组转换为图像?
或者,有没有更好的方法来解决这个问题?我尝试将 byte[] 更改为 Stream,但是当我从 Firefox 调用服务时,尽管 HTTP 状态代码为 200,但响应为空。我正在使用 Firebug 和 Fiddler。
我认为这无关紧要,但由于太多信息不会伤害任何不是机器人的人,这里是 web.config:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
最终,我想问题是:如何从 WCF RESTful 服务返回位图,以便在浏览器中运行的 JavaScript 可以将其显示在屏幕上?
【问题讨论】:
-
我无法理解这里投票的逻辑。
标签: c# javascript wcf rest