Silverlight의 Async Call에서 Streamreader를 WCF 서비스로 전달합니다.
-
11-09-2019 - |
문제
CSV 파일에서 Silverlight 프론트 엔드에서 DB로 레코드를 가져오고 싶습니다. WCF 서비스를 사용하여 DB 작업을 수행하고 있습니다. 전체 파일 경로 (하드 코딩)를 전달하면 DB에 레코드를 추가 할 수 있지만 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) PasseInfo를 통과합니다
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 메소드에서 예외가 발생하고 있습니다.
누구든지 Silverlight를 사용하여 CSV에서 DB 프로그래밍 방식으로 레코드를 가져 오는 솔루션 또는 더 나은 접근 방식을 도와 줄 수 있습니까?
감사!
해결책
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())