WCF 服务:transferMode:StreamingResponse 不起作用

发布于 2024-12-08 08:33:12 字数 3983 浏览 0 评论 0原文

我正在尝试创建流式 WCF 服务。我创建了一个我认为应该支持流式响应的服务(作为 VS 中的 WCF 服务应用程序项目)和一个客户端,该客户端只是一个控制台应用程序项目,我在其中添加了该服务作为服务引用。当我将客户端中的传输模式设置为 Buffered 时,代码可以正常工作,但如果我将其更改为 StreamedResponse,我会得到一个只有大约 1500 字节数据的 Stream,而不是使用缓冲传输模式时得到的 33k 数据。

以下是我的代码的重要部分:

Interface

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

实现接口 Web.config 的类

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;
            }
        }

    }
}

在服务中

<?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>

:在客户端中的 app.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>

I'm trying to create a streaming WCF service. I have created a service that I believe should support streamed responses (as a WCF Service Application project in VS) and a client that is just a Console Application project where I have added the service as a service reference. The code works when I set the transfer mode in the client to Buffered, but if I change it to StreamedResponse I get a Stream that only has about 1500 bytes of data instead of the 33k I get when I use the buffered transfer mode.

Here are the vital parts of my code:

Interface

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

The class that implements the interface

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 in service:

<?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>

app.config in client:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞花火° 2024-12-15 08:33:12

File.OpenRead() 是否为您提供了位置重置为零的 Stream?如果不是,则在返回之前SetPosition=0;

Does File.OpenRead() give you a Stream with position reset to zero? If not, SetPosition=0 before returning it;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文