Winforms-服务-已处理对象错误
这是一个奇怪的问题——我们有一个winforms应用程序,它调用数据服务。最终用户可以选择当前数据库,对于其中的一个数据库,它有时会对其中一个数据库抛出此错误,而所有其他数据库都可以无缝工作 抛出的错误是Winforms-服务-已处理对象错误,winforms,web-services,Winforms,Web Services,这是一个奇怪的问题——我们有一个winforms应用程序,它调用数据服务。最终用户可以选择当前数据库,对于其中的一个数据库,它有时会对其中一个数据库抛出此错误,而所有其他数据库都可以无缝工作 抛出的错误是 Cannot access a disposed object. Object name: 'System.ServiceModel.Channels.ServiceChannel'. 堆栈跟踪是 Server stack trace: at System.ServiceModel.C
Cannot access a disposed object.
Object name: 'System.ServiceModel.Channels.ServiceChannel'.
堆栈跟踪是
Server stack trace:
at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen()
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at HOA_Manager_Client_03.ServiceReference1.IService1.InsertDataHOA(String strSQL, String LineNo, String HOAID)
谢谢
编辑-这就是它的应用方式
Private Sub Accounting_Utilities_Prepayments_MovePrepaymentsToUnallocated(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim DGV As CustomControl.DGV = RFC(MainForm, "Prepayments_DGV")
Dim TotalRows As Integer = 0
Dim TotalBalance As Decimal = 0
For Each Row As DataGridViewRow In DGV.SelectedRows
Dim vBalance As Decimal = Row.Cells("Balance").Value
TotalBalance += vBalance
TotalRows += 1
Next
If TotalRows = 0 Then
TaskDialog.Show(MainForm, AppBoxWarning("Validation", "You have not selected any records to transfer from!", "Validation"))
Exit Sub
End If
Dim vConfirm As String = "You have selected " & TotalRows & " total records, and a total balance of $" & Format(TotalBalance, "###,##0.00") & " to transfer back!" & Environment.NewLine
vConfirm += "Once transferred the prepayment account will be deleted!" & Environment.NewLine
vConfirm += "Proceed with the transfer?"
If Not TaskDialog.Show(MainForm, AppBoxQuestion("Confirmation", vConfirm, "Proceed Confirmation")) = eTaskDialogResult.Yes Then
Exit Sub
End If
vService = New Service1Client
MainForm.Cursor = Cursors.WaitCursor
For Each Row As DataGridViewRow In DGV.SelectedRows
Dim vBalance As Decimal = Row.Cells("Balance").Value
Dim CreditName As String = Row.Cells("Creditor").Value
Dim PrepaymentID As Integer = Row.Cells("ID").Value
MainSS.Text = "Transfering balance for " & CreditName & "... Please wait..."
Application.DoEvents()
'Nominal Ledger in and out for audit trail
If vService Is Nothing Then
vService = New Service1Client
End If
strSQL = "INSERT INTO A_Nominal (Type, Ref, Details, Debit, Nominal_Code, Item_Date) VALUES ("
strSQL += "'JD', "
strSQL += "'Transfer', "
strSQL += "'Transfer to Debtors Control Account', "
strSQL += "'" & vBalance & "', "
strSQL += "'1103', "
strSQL += "'" & Format(Today, "yyyy-MM-dd") & "')"
If vService.InsertDataHOA(strSQL, "MainTabs_3 51002", Current_HOA_ID) = False Then
TaskDialog.Show(MainForm, AppBoxError("Error", "There was an error updating the records", "Update Error"))
Exit Sub
End If
strSQL = "INSERT INTO A_Nominal (Type, Ref, Details, Credit, Nominal_Code, Item_Date) VALUES ("
strSQL += "'JC', "
strSQL += "'Transfer', "
strSQL += "'Transfer from Prepayments', "
strSQL += "'" & vBalance & "', "
strSQL += "'1100', "
strSQL += "'" & Format(Today, "yyyy-MM-dd") & "')"
If vService.InsertDataHOA(strSQL, "MainTabs_3 51015", Current_HOA_ID) = False Then
TaskDialog.Show(MainForm, AppBoxError("Error", "There was an error updating the records!", "Update Error"))
Exit Sub
End If
'Control account in and out
strSQL = "INSERT INTO A_Control (Control_ID, C_Description, Debit) VALUES ("
strSQL += "'1103', "
strSQL += "'Transfer to Debtors Control Account', "
strSQL += "'" & vBalance & "')"
If vService.InsertDataHOA(strSQL, "MainTabs_3 51029", Current_HOA_ID) = False Then
TaskDialog.Show(MainForm, AppBoxError("Error", "There was an error updating the records!", "Update Error"))
Exit Sub
End If
strSQL = "INSERT INTO A_Control (Control_ID, C_Description, Credit) VALUES ("
strSQL += "'1100', "
strSQL += "'Transfer from Prepayments', "
strSQL += "'" & vBalance & "')"
If vService.InsertDataHOA(strSQL, "MainTabs_3 51038", Current_HOA_ID) = False Then
TaskDialog.Show(MainForm, AppBoxError("Error", "There was an error updating the records!", "Update Error"))
Exit Sub
End If
strSQL = "SELECT Customer_ID FROM A_Prepayments WHERE Prepayment_ID = " & PrepaymentID
Dim CustomerID As Integer = vService.ReturnScalarInteger(strSQL, Current_HOA_ID)
'Move into Sales Ledger
strSQL = "INSERT INTO A_Sales_Ledger (Credit, Paid, S_Description, Document_Date, Customer_ID, Type) VALUES ("
strSQL += "'" & vBalance & "', "
strSQL += "'N', "
strSQL += "'Transfer from prepayments', "
strSQL += "'" & Format(Today, "yyyy-MM-dd") & "', "
strSQL += "'" & CustomerID & "', "
strSQL += "'PT')"
If vService.InsertDataHOA(strSQL, "MainTabs_3 51053", Current_HOA_ID) = False Then
TaskDialog.Show(MainForm, AppBoxError("Error", "There was an error updating the records!", "Update Error"))
Exit Sub
End If
'Delete the prepayment record
strSQL = "DELETE A_Prepayments WHERE Prepayment_ID = " & PrepaymentID
If vService.InsertDataHOA(strSQL, "MainTabs 51063", Current_HOA_ID) = False Then
TaskDialog.Show(MainForm, AppBoxError("Error", "There was an error updating the records!", "Update Error"))
Exit Sub
End If
Next
MainSS.Text = "Data successfully transferred..."
Accounting_Utilities_Prepayments_LoadData()
Catch ex As Exception
EmailError(ex)
Finally
MainForm.Cursor = Cursors.Default
If Not vService Is Nothing Then
vService.Close()
vService = Nothing
End If
End Try
End Sub
在finalize方法中,在调用close()之前检查VService的状态。如果通信对象处于关闭或关闭状态且无法修改,它将引发ObjectDisposedException异常
检查最后,答案是重命名服务
Dim vNewService As New Service1Client
之后,它运行时没有出现问题-仍然不确定与之冲突的是什么,但还是解决了…看起来有什么东西持有对该对象的引用。检查是否有任何静态对象包含对您的服务的引用。我不想显得粗俗(我的辩护是,现在是星期天),但不能调用ServiceModel的多个事件吗?您没有在此处清楚列出您的场景。此外,您的问题并没有明确说明您如何使用您的服务。不管怎样,我做了一个假设,并把它作为一个注释添加了进来。下面是它的使用方式——除了一个后端数据库之外,它的工作没有任何问题——这就是让我感到厌烦的事情。在再次尝试关闭它之前,你能检查一下服务是否已经关闭吗。这可能会有所不同。很高兴你已经解决了这个问题。我注意到您经常使用内联sql语句。它可能导致Sql注入攻击。我希望您已经考虑过这种情况。您是否在Finalize中将vService.Close()更改为vNewService.Close()?