如何通过浏览器访问WCF Rest流URI

如何通过浏览器访问WCF Rest流URI,wcf,streaming,Wcf,Streaming,我有一个WCF Rest服务,它返回托管在IIS中的流。这可以通过.net客户端应用程序访问,但不能通过web浏览器访问 我在web.config中有以下内容: <bindings> <basicHttpBinding> <binding name="StreamedHttp" transferMode="StreamedResponse" maxReceivedMessageSize="67108864"> </

我有一个WCF Rest服务,它返回托管在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. ---&gt; 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>
System.ServiceModel.ProtocolException,System.ServiceModel,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089
从网络接收的XML有问题。有关详细信息,请参阅内部异常。
位于System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext上下文,操作回调)
位于System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult结果)
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandlerRequest()中
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()中
位于System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(对象状态)
在System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32错误,UInt32字节读取,NativeOverlapped*NativeOverlapped)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32错误代码,UInt32个字节,本机重叠*pOVERLAP)
System.ServiceModel.ProtocolException:从网络接收的XML存在问题。有关详细信息,请参阅内部异常。--System.Xml.XmlException:无法读取消息体,因为它是空的。
---内部异常堆栈跟踪的结束---
System.Xml.XmlException,System.Xml,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089
无法读取邮件正文,因为它是空的。
位于System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext上下文,操作回调)
位于System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult结果)
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandlerRequest()中
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()中
位于System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(对象状态)
在System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32错误,UInt32字节读取,NativeOverlapped*NativeOverlapped)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32错误代码,UInt32个字节,本机重叠*pOVERLAP)
System.Xml.XmlException:无法读取消息体,因为它是空的。

这是否在本地IIS上


每当我看到该错误时,都会通过在本地IIS web服务器上运行来解决。

如果您试图从web浏览器访问响应,则需要设置一个端点,该端点将公开一个支持REST的webHttpBinding。这是有道理的,但现在它一起变成了一个不同的问题。我需要通过webHttpBinding发布一个接口,并发布另一个用于流式传输大型二进制数据的接口。如果使用basicHttpbinding,则无法通过浏览器进行测试。如果使用webHttpBinding,则无法使用MTOM编码。设置2个不同的端点,并使用所需的绑定覆盖所有Scenariosthank,但这是不允许的。下面是错误消息的一个片段:“如果两个端点想要共享同一个ListenUri,它们还必须共享同一个绑定对象实例。”您需要区分每个端点的地址。发布您配置条目的外观可能会有所帮助。