将 StreamReader 从 Silverlight 的异步调用传递到 WCF 服务

发布于 2024-08-13 11:10:39 字数 1549 浏览 4 评论 0原文

我想从 Silverlight 前端将 csv 文件中的记录导入到数据库中。我正在使用 WCF 服务来执行数据库操作。当我传递整个文件路径(硬编码)时,我可以将记录添加到数据库,但是由于Silverlight中的OpenFileDialog不允许获取文件路径(由于安全原因),我尝试使用WCF服务并通过FileInfo 属性或 StreamReader,然后执行操作。但这给了我一个例外。我有以下代码 -

1)传递 StreamReader Page.xaml.vb 文件

Dim service As New ServiceReference1.Service1Client
dlg.ShowDialog()
Dim Reader As System.IO.StreamReader
If dlg.File IsNot Nothing Then
   Reader = dlg.File.OpenText
End If
service.ImportPersonInfoAsync(Reader)

'Service1.svc.vb 文件

<OperationContract()> _
    Public Sub ImportPersonInfo(ByVal Reader As System.IO.StreamReader)
    'Code to add records to DB table
    End Sub

我收到异常 - 远程服务器返回错误:NotFound(在 EndInvoke 方法中)

Public Sub EndImportPersonInfo(ByVal result As System.IAsyncResult) Implements ServiceReference1.Service1.EndImportPersonInfo
    Dim _args((0) - 1) As Object
    MyBase.EndInvoke("ImportPersonInfo", _args, result)
End Sub

2) 传递 FileInfo

Page.xaml.vb 文件

If dlg.File IsNot Nothing Then
   ImportFile = dlg.File
End If
service.ImportPersonInfoAsync(ImportFile)

Service1.svc.vb 文件

Public Sub ImportPersonInfo(ByVal ImportFile As System.IO.FileInfo)
    Dim Reader As System.IO.StreamReader = ImportFile.OpenText
    'Do operation
End Sub

我是在 BeginInvoke 方法中出现异常 - 尝试访问该方法失败:System.IO.FileSystemInfo.get_Attributes()

任何人都可以帮助我/建议解决方案或更好的方法,以使用 Silverlight 将记录从 csv 导入到数据库中。

谢谢!

I want to import records from csv file to the DB from Silverlight front end. I am using WCF service to perform the DB operations. When I pass the entire file path (hard coded), I am able to add records to the DB, but as OpenFileDialog in Silverlight doesn't allow to obtain the file path's (due to security reasons), I tried using WCF service and pass either the FileInfo property or StreamReader and then perform the operations. But its giving me an exception. I have the following code -

1) Passing StreamReader
Page.xaml.vb file

Dim service As New ServiceReference1.Service1Client
dlg.ShowDialog()
Dim Reader As System.IO.StreamReader
If dlg.File IsNot Nothing Then
   Reader = dlg.File.OpenText
End If
service.ImportPersonInfoAsync(Reader)

'Service1.svc.vb file

<OperationContract()> _
    Public Sub ImportPersonInfo(ByVal Reader As System.IO.StreamReader)
    'Code to add records to DB table
    End Sub

I am getting an exception - The remote server returned an error: NotFound (in EndInvoke method)

Public Sub EndImportPersonInfo(ByVal result As System.IAsyncResult) Implements ServiceReference1.Service1.EndImportPersonInfo
    Dim _args((0) - 1) As Object
    MyBase.EndInvoke("ImportPersonInfo", _args, result)
End Sub

2) Passing FileInfo

Page.xaml.vb file

If dlg.File IsNot Nothing Then
   ImportFile = dlg.File
End If
service.ImportPersonInfoAsync(ImportFile)

Service1.svc.vb file

Public Sub ImportPersonInfo(ByVal ImportFile As System.IO.FileInfo)
    Dim Reader As System.IO.StreamReader = ImportFile.OpenText
    'Do operation
End Sub

I am getting an exception in BeginInvoke method - Attempt to access the method failed: System.IO.FileSystemInfo.get_Attributes()

Can anyone please help me out / suggest solution or a better approach to import records from csv into the DB programmatic using Silverlight.

Thanks!

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

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

发布评论

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

评论(1

甜尕妞 2024-08-20 11:10:39

FileInfo 和 StreamReader 都不是可序列化的类,因此您无法真正使用 WCF 将它们直接传递到服务器。一种选择是读取内存中的文件,然后将其传递给服务,如下所示。另一种选择是读取多行(列表).csv 文件并将其传递给服务。

Dim service As New ServiceReference1.Service1
Clientdlg.ShowDialog()
Dim ms As System.IO.MemoryStream
If dlg.File IsNot Nothing Then
   Dim TheFile as Stream = dlg.File.OpenRead()
   Dim Buffer as Byte() = New Byte(10000) { }
   Dim BytesRead as Integer
   Do
      BytesRead = TheFile.Read(Buffer, 0, Buffer.Length)
      ms.Write(Buffer, 0, BytesRead)
   Loop While (BytesRead > 0)
End If
TextEnd Ifservice.ImportPersonInfoAsync(ms.ToArray())

Neither FileInfo nor StreamReader are serializable classes, so you can't really pass them directly to the server with WCF. An option is to read the file in memory, then pass it to the service, something like shown below. Another option is read the .csv file in multiple lines (List) and pass it to the service.

Dim service As New ServiceReference1.Service1
Clientdlg.ShowDialog()
Dim ms As System.IO.MemoryStream
If dlg.File IsNot Nothing Then
   Dim TheFile as Stream = dlg.File.OpenRead()
   Dim Buffer as Byte() = New Byte(10000) { }
   Dim BytesRead as Integer
   Do
      BytesRead = TheFile.Read(Buffer, 0, Buffer.Length)
      ms.Write(Buffer, 0, BytesRead)
   Loop While (BytesRead > 0)
End If
TextEnd Ifservice.ImportPersonInfoAsync(ms.ToArray())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文