WCF服务TcpNetBinding StreamRequest不接收流

时间:2011-04-26 12:55:48

标签: vb.net wcf visual-studio-2010 .net-4.0 stream

我的第一个问题是温柔的=)

以下是VS2010中的.Net 4,VB.Net。最终目标是通过Tcp绑定从客户端接收(完整)流到IIS托管WCF服务。我面临的问题是该服务无法从提供的流中读取任何字节。现在有了细节......为了简洁,我已经删除了相当数量但是,如果我省略了重要的东西,请告诉我。

服务合同如下:

<ServiceContract(Namespace:="ImageSystem")> _
Public Interface IUploadService
    <OperationContract()> _
    Function UploadFile(ByVal file As ImageUpload) As ImageUpload
End Interface

数据合约 ImageUpload 如下:

<MessageContract()> _
Public Class ImageUpload

#Region " Message Header "

    Private _ImageID As Nullable(Of Long)
    <MessageHeader()> _
    Public Property ImageID() As Nullable(Of Long)
        Get
            Return _ImageID
        End Get
        Set(ByVal value As Nullable(Of Long))
            _ImageID = value
        End Set
    End Property

    '... a few other value type properties

#End Region

#Region " Message Body"
    ' Do not add any more members to the message body or streaming support will be disabled!

    <MessageBodyMember()> _
    Public Data As System.IO.Stream

#End Region

End Class

相关的服务器配置/绑定如下(这些显然只是开发环境设置):

<system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTcpStreamBinding" transferMode="Streamed" maxBufferSize="20971520" maxReceivedMessageSize="20971520"/>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="UploadServiceBehaviour"
        name="ImageSystem.SVC.UploadService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpStreamBinding"
          contract="ImageSystem.SVC.IUploadService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:809/UploadService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="UploadServiceBehaviour">
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

WCF服务是一个服务库,由Web应用程序托管。 Web应用程序项目在我的本地IIS 7.5中运行。 IIS已配置为启用TCP连接,并且应用程序池标识配置了对合同实现的相关权限。 VS2010以管理员身份运行,以便在IIS中启用调试。

为了测试合同实现,我将Windows控制台应用程序设置为(测试)客户端。通过在IIS主机(http://localhost/ImageSystem/UploadService.svc)中向服务添加服务引用来生成客户端代理类。服务引用配置为生成异步方法。

相关的自动生成的客户端配置如下(请注意,我已尝试增加maxBufferPoolSize,maxBufferSize和maxReceivedMessageSize以匹配“20971520”的服务器配置,但无济于事):

[编辑:reliableSessions部分根据Sixto Saez的建议进行评论但无效]

<system.serviceModel>

    <bindings>
        <binding name="NetTcpBinding_IUploadService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
          hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="20971520"
          maxBufferSize="20971520" maxConnections="10" maxReceivedMessageSize="20971520">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <!--<reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />-->
          <security mode="None">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
            <message clientCredentialType="Windows" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

    <client>
      <endpoint address="net.tcp://mycomputername.mydomain/ImageSystem/UploadService.svc"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IUploadService"
        contract="UploadService.Local.IUploadService" name="NetTcpBinding_IUploadService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>

  </system.serviceModel>

客户端使用情况如下:

Public Sub Test()
    Dim serviceClient As UploadService.Local.UploadServiceClient = New UploadService.Local.UploadServiceClient
    AddHandler serviceClient.UploadFileCompleted, AddressOf LocalTestCallback
    Dim ms As MemoryStream = New MemoryStream
    My.Resources.Penguins.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
    serviceClient.ClientCredentials.Windows.ClientCredential.Domain = "MYDOMAIN"
    serviceClient.ClientCredentials.Windows.ClientCredential.UserName = "User"
    serviceClient.ClientCredentials.Windows.ClientCredential.Domain = "Password123"
    serviceClient.UploadFileAsync(Nothing, ..., ms, ms) '"..." is obviously not actually here, other values omitted. "ms" is passed as UserState object in addition to fulfilling the 'Data' parameter
End Sub

如果您想知道(或重要),企鹅图像是样本图片目录中随Windows 7提供的图像。图像为777,835字节(应在相关的请求/缓冲区最大大小范围内)。

我尝试了两种在服务器端读取图像的方法。

方法1:

Public Function UploadFile(ByVal file As ImageUpload) As ImageUpload Implements IUploadService.UploadFile

    Dim uploadBuffer(Helper.Settings.AppSettings(Of Integer)("UploadBufferSize", True) - 1) As Byte
    Dim ms As MemoryStream = New MemoryStream()
    Dim bytesRead As Integer
    Do
        bytesRead = file.Data.Read(uploadBuffer, 0, uploadBuffer.Length)
        ms.Write(uploadBuffer, 0, bytesRead)
    Loop Until bytesRead = 0

End Function

方法2:

Public Function UploadFile(ByVal file As ImageUpload) As ImageUpload Implements IUploadService.UploadFile

    Dim reader As StreamReader = New StreamReader(file.Data)
    Dim imageB64 As String = reader.ReadToEnd
    ms = New MemoryStream(Convert.FromBase64String(imageB64))

End Function

在这两种情况下,ms.Length = 0.更明显,在第二种方法中,imageB64 =“”(空字符串)。

为什么我不从流中收到任何内容?另外,作为一个偷偷摸摸的子问题,为什么生成的代理类不提供接受 ImageUpload 类型的对象的重载?

提前谢谢!!

2 个答案:

答案 0 :(得分:2)

对我来说,你会遇到你提到的问题似乎很奇怪,所以我很好奇并使用你的服务合同整理了一个实现。那个实际上是直接工作的。我实际上并不知道你的情况出了什么问题(这不是很明显),但我想在这里发布一个有效的解决方案,希望这能帮助你解决问题。

不幸的是,由于我多年前放弃了VB,我只能提供C#代码。希望没关系。

服务器Web.config(在IIS中测试,使用net.tcp绑定):

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding transferMode="Streamed" maxReceivedMessageSize="1000000">
          <security mode="None"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="ImageSystem.SVC.UploadService">
        <endpoint address="" binding="netTcpBinding" contract="ImageSystem.SVC.IUploadService">
        </endpoint>
        <endpoint address="mex" kind="mexEndpoint" binding="mexTcpBinding"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

客户端app.config(控制台测试应用):

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding transferMode="Streamed" maxReceivedMessageSize="1000000">
                <security mode="None"/>
            </binding>
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint 
            address="net.tcp://localhost/WcfService1/UploadService.svc" 
            binding="netTcpBinding" 
            contract="ImageServices.IUploadService" 
            name="NetTcpBinding_IUploadService">
        </endpoint>
    </client>
</system.serviceModel>

服务合同&amp;实施

[ServiceContract(Namespace="urn:ImageSystem")]
public interface IUploadService
{
    [OperationContract]
    ImageUpload UploadFile(ImageUpload file);
}

[MessageContract]
public class ImageUpload
{
    [MessageHeader]
    public long? ImageID { get; set; }
    [MessageBodyMember]
    public Stream Data;
}

public class UploadService : IUploadService
{

    public ImageUpload UploadFile(ImageUpload file)
    {
        long length;

        using (var ms = new MemoryStream())
        {
            file.Data.CopyTo(ms);
            length = ms.Length;
        }

        return new ImageUpload { ImageID = length, Data = new MemoryStream() };
    }
}

测试应用:

private static readonly string imgPath = @"C:\Pictures\somepicture.jpg";
private static readonly EventWaitHandle waitHandle = new AutoResetEvent(false);

static void Main()
{
    long? result;

    using (var service = new ImageServices.UploadServiceClient("NetTcpBinding_IUploadService"))
    {
        var image = new ImageServices.ImageUpload();
        using (var imgStream = File.OpenRead(imgPath))
        {
            image.Data = imgStream;
            service.UploadFileCompleted += (sender, e) => 
            { 
                result = e.Result;
                if (e.Data != null) image.Data.Dispose();
                waitHandle.Set();
            };

            service.UploadFileAsync(null, imgStream);

            waitHandle.WaitOne();
        }
    }
}

首先,正如您所看到的,配置文件可以更简单。特别是大BufferSize值不是必需的。然后,关于服务合同,我不清楚为什么Upload操作会收到并返回ImageUpload消息。在我的实现中,我正在返回ImageID参数中的上传文件大小,当然只是为了演示目的。我不知道你的合同背后的原因是什么,以及你真正希望返回的是什么。


实际上,当我知道为什么你的代码可能失败时,正要点击“发送”。在测试客户端中,在致电serviceClient.UploadFileAsync()之前,请将此行添加到您的代码中:ms.Seek(0, SeekOrigin.Begin)

这会将MemoryStream的位置重置回其开头。如果你不这样做,MemoryStream只会从当前位置消耗,这是它的结束 - 这解释了服务端收到的流的长度= 0!

答案 1 :(得分:0)

您可能已经seen the information in this MSDN article但是您应该查看名为Streaming Data的部分及其列出的限制。您的客户端配置显示将ordered属性设置为“true”的reliableSession元素,该流不支持流式传输。

我不知道这是否是您问题的具体原因,但这是一个开始。该文章还很好地布局了流媒体所需的基本配置,因此您应该确保您的配置符合其建议。