我有一个 WCF Rest Service,它返回一个托管在 IIS 中的流。这可以通过 .net 客户端应用程序访问,但不能通过 Web 浏览器访问。
我在 web.config 中有以下内容:
<bindings>
<basicHttpBinding>
<binding name="StreamedHttp" transferMode="StreamedResponse" maxReceivedMessageSize="67108864">
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="SvcMetaData" name="Services.StreamService">
<endpoint name="stream" binding="basicHttpBinding" bindingConfiguration="StreamedHttp" contract="Services.IStreamDetails" />
<endpoint name="wsdl" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
界面如下:
[ServiceContract]
public interface IStreamDetails
{
[OperationContract]
[WebGet(UriTemplate = "Items/{itemID}")]
Stream GetDetails(string itemID);
}
该服务的实现如下:
public Stream GetItem (string ItemID)
{
string filePath= ConfigurationManager.AppSettings["ImagePath"];
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return File.OpenRead(filePath);
}
我可以使用以下方式从.Net客户端访问它:
ItemService.ItemDetailsClient client = new ItemDetailsClient();
var v = client.GetDetails("test");
v.CopyTo(new FileStream(@".\temp.doc", FileMode.Create, FileAccess.Write));
当我从网络浏览器调用它时,我得到一个空白网页。WCF 跟踪日志显示以下异常:
<ExceptionType>System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>There is a problem with the XML that was received from the network. See inner exception for more details.</Message>
<StackTrace>
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
<ExceptionString>System.ServiceModel.ProtocolException: There is a problem with the XML that was received from the network. See inner exception for more details. ---> System.Xml.XmlException: The body of the message cannot be read because it is empty.
--- End of inner exception stack trace ---</ExceptionString>
<InnerException>
<ExceptionType>System.Xml.XmlException, System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>The body of the message cannot be read because it is empty.</Message>
<StackTrace>
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
<ExceptionString>System.Xml.XmlException: The body of the message cannot be read because it is empty.</ExceptionString>
</InnerException>