0

这是一个奇怪的 - 我们有一个调用数据服务的 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
4

2 回答 2

0

最后的答案是重命名服务

 Dim vNewService As New Service1Client

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

于 2013-05-06T11:43:02.247 回答
0

In the finalise method check the state of the VService before calling close(). It will throw ObjectDisposedException exception if the communication object is in a Closing or Closed state and cannot be modified.

check CommunicationObject.Close Method

于 2013-05-05T20:13:25.553 回答