【发布时间】:2011-04-26 12:55:47
【问题描述】:
我的第一个问题,所以要温柔=)
以下是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”的服务器配置,但无济于事):
[编辑:根据 Sixto Saez 的建议,reliableSessions 部分已注释掉,但无济于事]
<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 字节(应在相关请求/缓冲区的最大大小范围内)。
我尝试了两种方法来读取服务器端的图像。
方法一:
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
方法二:
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类型对象的重载?
提前谢谢你!!
【问题讨论】:
-
有什么办法可以解决这个问题吗?只有 26 次观看,还没有答案。 =(
标签: vb.net wcf visual-studio-2010 .net-4.0 stream