【发布时间】: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