Winforms - 服务 - 处置对象错误

时间:2013-05-05 18:48:58

标签: winforms web-services

这是一个奇怪的问题 - 我们有一个winforms应用程序,它调用数据服务。最终用户可以选择当前的数据库,对于其中一个数据库,它有时只会抛出一个错误,所有其他错误无缝地工作。

抛出的错误是

 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

2 个答案:

答案 0 :(得分:0)

在finalize方法中,在调用close()之前检查VService的状态。如果通信对象处于Closing或Closed状态且无法修改,它将抛出ObjectDisposedException异常。

检查CommunicationObject.Close Method

答案 1 :(得分:0)

最后答案是重命名服务

 Dim vNewService As New Service1Client

之后它运行没有问题 - 仍然不确定什么是冲突的,但仍然解决了......