WPF服务调用WCF无法访问已处置的对象

时间:2015-01-18 16:34:05

标签: .net wpf vb.net wcf

我们有一个WPF应用程序,可以从WCF服务返回数据。

下面的代码会间歇性地抛出“无法访问已丢弃的对象”错误 - 通常在一个更新序列成功完成后。

即使关闭服务并在每次通话时开始新服务也无济于事。有什么想法吗?

...谢谢

 Private Sub Reports_BalanceSheet_DoWork(sender As Object, e As DoWorkEventArgs)
    Try
        Dim vUpdate As New UpdateDelegate(AddressOf Reports_BalanceSheet_UpdateDB_Delegate)
        Dim vName As String = ReportName

        vService = New Service1Client

        Dim vID As Integer = 0
        If NewRecord = True Then
            Dim vRows As Integer = vService.ReturnScalarInteger("SELECT COUNT(Form_ID) From Balance_Sheet_Templates", Current_HOA_ID)
            If vRows > 0 Then
                vID = NullInteger(vService.ReturnScalarInteger("SELECT MAX(Form_ID) FROM Balance_Sheet_Templates", Current_HOA_ID))
            End If
            vID += 1
            'Save the form name
            strSQL = "INSERT INTO Balance_Sheet_Templates(Form_ID, Category_Type, Category_ID,  Form_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'1', "
            strSQL += "'1', "
            strSQL += "'" & vName & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2466 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If


        Else

            vID = FormID
            strSQL = "DELETE Balance_Sheet_Templates WHERE Form_ID = " & vID
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2455 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        End If




        Dim Update1() As Object = {"Saving the report defaults... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update1})
        For Each Row As DataRow In ReportDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Form_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'1', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & vName & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2417 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next



        Dim Update2() As Object = {"Saving the revenue categories... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update2})
        For Each Row As DataRow In RevenueDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Key_String) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'3', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & SubmitText(Row("Name")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2434 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next



        Dim Update3() As Object = {"Saving the revenue data... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update3})
        For Each Row As DataRow In RevenueNomDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Cat_Name_ID, Nom_Code, Nom_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'4', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & Row("CatID") & "', "
            strSQL += "'" & Row("NomCode") & "', "
            strSQL += "'" & SubmitText(Row("NomName")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2453 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next



        Dim Update4() As Object = {"Saving the expenses categories... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update4})
        For Each Row As DataRow In ExpensesDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Key_String) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'5', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & SubmitText(Row("Name")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2472 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next



        Dim Update5() As Object = {"Saving the expenses data... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update5})
        For Each Row As DataRow In ExpensesNomDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Cat_Name_ID, Nom_Code, Nom_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'6', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & Row("CatID") & "', "
            strSQL += "'" & Row("NomCode") & "', "
            strSQL += "'" & SubmitText(Row("NomName")) & "') "
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2491 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




        Dim Update6() As Object = {"Saving the assets categories... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update6})
        For Each Row As DataRow In ResIncomeDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Key_String) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'7', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & SubmitText(Row("Name")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2509 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




        Dim Update7() As Object = {"Saving the assets data... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update7})
        For Each Row As DataRow In ResIncNomDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Cat_Name_ID, Nom_Code, Nom_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'8', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & Row("CatID") & "', "
            strSQL += "'" & Row("NomCode") & "', "
            strSQL += "'" & Row("NomName") & "') "
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2529 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




        Dim Update8() As Object = {"Saving the liability categories... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update8})
        For Each Row As DataRow In ResExpensesDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Key_String) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'9', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & SubmitText(Row("Name")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2548 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




        Dim Update9() As Object = {"Saving the liability data... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update9})
        For Each Row As DataRow In ResExpNomDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Cat_Name_ID, Nom_Code, Nom_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'10', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & Row("CatID") & "', "
            strSQL += "'" & Row("NomCode") & "', "
            strSQL += "'" & Row("NomName") & "') "
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2566 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




    Catch ex As Exception
        EmailError(ex, False)
        IsError = True
    Finally
        If Not vService Is Nothing Then
            vService.Close()
            vService = Nothing
        End If
    End Try
End Sub



Private Sub Reports_BalanceSheet_WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
    Try
        If IsError = True Then
            AppBoxError("There was an error updating the record!")
            Exit Sub
        End If
        PageStatusBarLoaded(Reports_BalanceSheet_Grid, "Template was successfully saved...")
        PageStatusBarRightChangeText(Reports_BalanceSheet_Grid, "Template updated...")
        Dim SaveUpdateButton As Button = Reports_BalanceSheet_Grid.FindName("BalanceSheet_SaveUpdateButton")
        If NewRecord = True Then
            SaveUpdateButton.IsEnabled = False
            SaveUpdateButton.Content = ReturnToolBarImageGrey("Record_Insert.png")
        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub



Private Sub Reports_BalanceSheet_UpdateDB_Delegate(ByVal x() As Object)
    Try
        PageStatusBarRightChangeText(Reports_BalanceSheet_Grid, x(0).ToString)
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

错误返回

Cannot access a disposed object.

对象名称:'System.ServiceModel.Channels.HttpsChannelFactory`1 + HttpsRequestChannel [System.ServiceModel.Channels.IRequestChannel]'。

服务器堆栈跟踪:    在System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen()    在System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(消息消息,TimeSpan超时)    在System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan超时)    在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超时)    在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins,Object [] outs,TimeSpan timeout)    在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)    在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

在[0]处重新抛出异常:    在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)    在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 type)    at HOA_Manager_Client_04.ServiceReference1.IService1.InsertDataHOA(String strSQL,String LineNo,String HOAID)    at HOA_Manager_Client_04.ServiceReference1.Service1Client.InsertDataHOA(String strSQL,String LineNo,String HOAID)    at HOA_Manager_Client_04.Reports_BalanceSheet_Page.Reports_BalanceSheet_DoWork(Object sender,DoWorkEventArgs e)

1 个答案:

答案 0 :(得分:0)

整个应用程序vService在每个页面中定义

Private strSQL As String
Private vService As Service1Client

并在需要时创建为New。有些东西显然也在同时使用和关闭,所以我刚刚创建了一个全新的服务

 Private Sub Reports_BalanceSheet_DoWork(sender As Object, e As DoWorkEventArgs)
    Dim vService1 As New Service1Client
    Try
        Dim vUpdate As New UpdateDelegate(AddressOf Reports_BalanceSheet_UpdateDB_Delegate)
        Dim vName As String = ReportName

        ' vService = New Service1Client

并将每个更新实例更改为 - 并且到目前为止似乎正在运行。奇怪,这是整个应用程序中唯一一个抛出此错误的地方