IIS でホストされているストリームを返す WCF Rest Service があります。これには .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));
これを Web ブラウザーから呼び出すと、空白の Web ページが表示されます。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>