2017-02-15 72 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來代替,看看那是怎麼回事!