0
我有一個WCF休息服務,它返回在IIS中託管的流。這可以通過.net客戶端應用程序訪問,但不能通過網絡瀏覽器訪問。如何WCF通過瀏覽器休息流URI
我在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>
如果您試圖從Web瀏覽器訪問響應,您需要設置一個端點來公開支持REST的webHttpBinding。 – Rajesh 2012-07-31 10:09:58
這很有道理,但現在它變成了一個不同的問題。我需要通過webHttpBinding和另一個用於流式傳輸大型二進制數據的接口來發佈一個接口。如果我使用basicHttpbinding,那麼我無法通過瀏覽器進行測試。如果我使用webHttpBinding,那麼我不能使用MTOM編碼。 – VV75 2012-07-31 13:58:56
使用所需的綁定來設置2個不同的端點以覆蓋所有場景 – Rajesh 2012-07-31 14:39:21