如何通过浏览器WCF Rest流URI

时间:2012-07-31 03:53:55

标签: wcf streaming

我有一个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>

1 个答案:

答案 0 :(得分:0)

这是否在您的本地IIS上?

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