2017-02-15 151 views
0

我想使WCF服务可以将图像放入流中。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不允许的方法

有什么不对?

+0

如果WebOperationContext.Current为null,您肯定会得到奇怪的答复。 – BugFinder

+0

但我甚至没有进入功能。未达到断点。 – Ksice

回答

0

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

请尝试使用WebHttpBinding来代替,看看那是怎么回事!