【发布时间】:2014-05-02 11:38:54
【问题描述】:
我正在构建一个解决方案,旨在通过 WCF 代理客户端使用 REST 服务、检索 XML 数据并将其插入 SQL 表中。
我的解决方案有 4 个项目:
- 一个用于托管所有内容的控制台应用程序
- 一个 WCF 客户端类,用于连接到 REST 服务并将 XML 数据反序列化为对象
- 用于解析反序列化对象和写入 SQL 表的类。
- 一个 WCF 服务类,托管在控制台中,实现一个引发事件的操作。
什么效果好:
- 实例化我的 WCF 客户端类并直接从控制台使用它,
- 实例化我的 WCF 客户端类并在手动触发我的 WCF 服务事件时使用它
什么不起作用:
- 实例化我的 WCF 客户端类并在我的 WCF 服务通过 HTTP 方法调用引发其事件时使用它 => 引发错误:文件意外结束
我查看了详细的跟踪日志,它说发送 HTTP 消息失败。
任何线索可以从哪里来?
这是我的 WCF 服务实现:
<ServiceBehavior(InstanceContextMode:=ServiceModel.InstanceContextMode.Single _
, IncludeExceptionDetailInFaults:=True)> _
Public Class RemoteService
Implements IRemoteService
Public Event getGroups As MyHostEventHandler
Public Sub DoWork() Implements IRemoteService.DoWork
RaiseEvent getGroups()
End Sub
End Class
Public Delegate Sub MyHostEventHandler()
这是我的控制台应用程序的代码:
Sub getGroups()
' Instantiate WCF client
Dim proxy As EE2014_DataSolution.EERestAPI = New EE2014_DataSolution.EERestAPI()
' Call WCF REST method getGroups2014()
Dim response_groups As groups = proxy.getGroups2014()
' Instantiate SQL writer class
Dim sql As SqlDataWriter.SqlDataWriter = New SqlDataWriter.SqlDataWriter()
' Pass deserialized object to SQL writter class
Dim numRowsWritten As Integer = sql.WriteGroups(response_groups)
Console.WriteLine(numRowsWritten & " rows updated")
End Sub
Sub Main()
' Instantiate WCF service
Dim host As ServiceHost = New ServiceHost(New RemoteService.RemoteService())
' Handle event (THIS WILL FAIL)
AddHandler CType(host.SingletonInstance, RemoteService.RemoteService).getGroups, AddressOf getGroups
' Start WCF service
host.Open()
Console.WriteLine("RemoteService started at " & Now)
' Wait for WCF messages
Console.WriteLine("Press any key to send HTTP request manually.")
Console.ReadLine()
' Get groups directly from console (THIS WILL WORK)
getGroups()
' Wait for WCF messages
Console.WriteLine("Press any key to send HTTP request manually via event.")
Console.ReadLine()
' Raise the WCF service event manually (THIS WILL WORK)
CType(host.SingletonInstance, RemoteService.RemoteService).DoWork()
' Exit
Console.WriteLine("Press any key to exit.")
Console.ReadLine()
End Sub
【问题讨论】: