【问题标题】:WCF service streaming imagesWCF 服务流图像
【发布时间】:2017-02-15 15:25:27
【问题描述】:

我想做 WCF 服务,可以将图像放入流中。

我在配置中有下一个:

<service name="Images" behaviorConfiguration="ImagesBehavior">
    <endpoint address="http://localhost:5523/Images.svc"
       binding="basicHttpBinding" contract="Images" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:5523/Images" />
      </baseAddresses>
    </host>
</service>

<behavior name="ImagesBehavior">
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceMetadata httpGetEnabled="true" />
</behavior>

和代码:

[OperationContract]
[WebGet(UriTemplate = "GetImage/{imageID}",
    BodyStyle = WebMessageBodyStyle.Bare)]
public Stream GetImage(string imageID)
{
    try
    {

        if (WebOperationContext.Current != null)
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";

        var ms = new MemoryStream(myImage);
        return ms;
    }
    catch (Exception e)
    {
if (WebOperationContext.Current != null)
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        Console.WriteLine("GetImage ERROR:" + e.Message + "\r\n" + e.StackTrace);
        byte[] errorBytes = Encoding.UTF8.GetBytes("ERROR");
        return new MemoryStream(errorBytes);
    }
}

但是当我通过浏览器尝试这个时

http://localhost:5523/Images.svc/GetImage/imagecodeblabla

我有

400 错误请求

http://localhost:5523/Images/GetImage/imagecodeblabla

405 方法不允许

怎么了?

【问题讨论】:

  • 如果 WebOperationContext.Current 为空,你肯定会得到奇怪的响应。
  • 但我什至没有进入功能。未达到断点。

标签: c# wcf stream datacontract


【解决方案1】:

您的服务是 SOAP 还是 REST?您似乎使用的是 REST 语法 (WebGetAttribute),但您的绑定是 BasicHttpBinding (SOAP)。

尝试改用WebHttpBinding,看看效果如何!

【讨论】:

    猜你喜欢
    • 2017-06-10
    • 2012-05-07
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    相关资源
    最近更新 更多