2011-10-04 63 views
0

我正尝试创建一个流式WCF服务。我已经创建了一个服务,我认为它应该支持流式响应(作为VS中的WCF服务应用程序项目),而客户端只是一个控制台应用程序项目,我已经将服务添加为服务参考。当我将客户端的传输模式设置为Buffered时,该代码有效,但如果将其更改为StreamedResponse,则会获得只有1500字节数据的Stream,而不是我使用缓冲传输模式时获得的33k数据。WCF服务:transferMode:StreamingResponse不起作用

这里是我的代码的要害部位:

接口

namespace Microsoft.Samples.Stream 
{ 
    [ServiceContract(Namespace = "http://Microsoft.Samples.Stream")] 
    public interface IStreamingSample 
    { 
     [OperationContract] 
     System.IO.Stream GetStream(string data); 
    } 
} 

实现接口的服务

namespace Microsoft.Samples.Stream 
{ 
    public class StreamingService : IStreamingSample 
    { 
     public System.IO.Stream GetStream(string data) 
     { 
      try 
      { 
       FileStream imageFile = File.OpenRead("e:/image.jpg"); 
       return imageFile; 
      } 
      catch (IOException ex) 
      { 
       throw ex; 
      } 
     } 

    } 
} 

Web.config中的类:

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="Microsoft.Samples.Stream.StreamingService"> 
     <endpoint address="ep1" binding="basicHttpBinding" contract="Microsoft.Samples.Stream.IStreamingSample"/> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    <bindings> 
     <basicHttpBinding> 
     <binding maxReceivedMessageSize="67108864" transferMode="StreamedResponse" /> 
     </basicHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

a在客户端pp.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_IStreamingSample" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" transferMode="StreamedResponse" 
        useDefaultWebProxy="true"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <security mode="None"> 
         <transport clientCredentialType="None" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="UserName" algorithmSuite="Default" /> 
        </security> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:56881/StreamingService.svc/ep1" 
       binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IStreamingSample" 
       contract="ServiceReference1.IStreamingSample" name="BasicHttpBinding_IStreamingSample" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

回答

0

是否File.OpenRead()给你一个位置重置为零的流?如果不是,则在返回之前SetPosition = 0;

+0

是的,位置设置为零。 – Nocklas