从 WCF 调用 StreamWriter
我有一个在我的控制台应用程序中运行良好的功能。但当我将它移至我的 WCF 时,它每次都会崩溃。无法弄清楚我做错了什么。
这是操作合同:
<OperationContract()> _
Function SendLetterToBatch(ByVal FinishedLetter As Letter) As Boolean
'Sends a completed letter to the batch for XStream, returns true/false.
这是 svc:
Imports System.Collections 进口系统 导入系统数据 导入 System.Data.SqlClient 导入 System.IO
Public Function SendLetterToBatch(ByVal FinishedLetter As Letter) As Boolean Implements ILetterWriter.SendLetterToBatch
Dim CurDateTime As DateTime = DateTime.Now
Dim Format As String = "yyyyMMdd HHmmss"
Dim FileName As String
'Create the text file name. Date / Time (yyyyMMdd HHmmss) to precede claim / policy number
FileName = CurDateTime.ToString(Format) & FinishedLetter.ClaimOrPolicyNo.ToString
'Remove Me -- temporary text file location
FileName = "D:\" & FileName.ToString & ".txt"
'Write the letter to the text file
Using writer As StreamWriter = New StreamWriter(FileName)
writer.Write("01")
writer.Write("02" & FinishedLetter.Body.ToString)
writer.Write("03")
End Using
'Function completed fine, return true
SendLetterToBatch = True
End Function
最后,调用 WCF 的控制台应用程序:
Dim objLetter As New Letter
objLetter._ClaimOrPolicyNo = 99999
objLetter._CompID = 23
objLetter._StateID = 12
objLetter._Body = "Dear Sir, you have not had much luck winning the lottery. We hope we can help. Next time, please choose 6/7/8/9/1 BONUS 2 on your ticket. Thank you, Us."
Console.Write(client.SendLetterToBatch(objLetter))
我不明白。有人有什么想法吗? WCF不支持streamwriter吗?任何想法将不胜感激!
谢谢, 贾森
I have a function which works great in my console app. But when I move it over to my WCF, it bombs out every time. Can't figure out what I'm doing wrong.
Here's the operation contract:
<OperationContract()> _
Function SendLetterToBatch(ByVal FinishedLetter As Letter) As Boolean
'Sends a completed letter to the batch for XStream, returns true/false.
Here's the svc:
Imports System.Collections
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Function SendLetterToBatch(ByVal FinishedLetter As Letter) As Boolean Implements ILetterWriter.SendLetterToBatch
Dim CurDateTime As DateTime = DateTime.Now
Dim Format As String = "yyyyMMdd HHmmss"
Dim FileName As String
'Create the text file name. Date / Time (yyyyMMdd HHmmss) to precede claim / policy number
FileName = CurDateTime.ToString(Format) & FinishedLetter.ClaimOrPolicyNo.ToString
'Remove Me -- temporary text file location
FileName = "D:\" & FileName.ToString & ".txt"
'Write the letter to the text file
Using writer As StreamWriter = New StreamWriter(FileName)
writer.Write("01")
writer.Write("02" & FinishedLetter.Body.ToString)
writer.Write("03")
End Using
'Function completed fine, return true
SendLetterToBatch = True
End Function
And finally, the console app calling the WCF:
Dim objLetter As New Letter
objLetter._ClaimOrPolicyNo = 99999
objLetter._CompID = 23
objLetter._StateID = 12
objLetter._Body = "Dear Sir, you have not had much luck winning the lottery. We hope we can help. Next time, please choose 6/7/8/9/1 BONUS 2 on your ticket. Thank you, Us."
Console.Write(client.SendLetterToBatch(objLetter))
I can't figure it out. Anybody have any ideas? Does WCF not support streamwriter? Any ideas would be greatly appreciated!
Thanks,
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果代码在从控制台运行时有效,我猜想它与作为 svc 执行时的上下文有关。您是否通过 IIS 运行 svc?哪个帐户?该帐户是否对您尝试创建文件的路径具有写访问权限?
If the code works when you run it from console, I would guess that it has to do with the context when it is executed as a svc. Are you running your svc via IIS? which account? does that account has write-access to the path where you are trying to create a file?