ObjectDataSource Call WCF service throw cannot access dispose object

0

I encounter the program throw cannot access dispose object when deploy program at IIS in Window Server 2012.

according to the code, The code can be run normally in my PC Development and Production but there is problem when I deploy to test server the symptom is I can run program finely in the first time after deploy then when I re-enter the page it throws cannot access disposal object.

I also check the service using soap to send request repeatedly but the problem not found and now after testing in difference PC and server it can run as well.

I'm not sure what I have to do more about config or it about server spec. becuase the others that can run have ram more than 8GB but in test server that found the problem only has 4GB of ram or I have to edit my code?

code section that bind datasource to service

        #region Employeeprofile
        this.EmployeeProfileDataSource.DataMember = "GetWF_30EmployeeProfile";
        this.EmployeeProfileDataSource.DataSource = typeof(THP.Welfare.Reporting.ReportManager.EmployeeProfilesReportManager);
        this.EmployeeProfileDataSource.Name = "EmployeeProfileDataSource";
        this.EmployeeProfileDataSource.Parameters.AddRange(new Telerik.Reporting.ObjectDataSourceParameter[] {
        new Telerik.Reporting.ObjectDataSourceParameter("employeeNo", typeof(string), empNo)});
        report.DataSource = this.EmployeeProfileDataSource;
        #endregion

        #region Spouse
        this.SpouseDataSource.DataMember = "GetWF_30SummaryMarriageFromBeforeApprove";
        this.SpouseDataSource.DataSource = typeof(THP.Welfare.Reporting.ReportManager.EmployeeProfilesReportManager);
        this.SpouseDataSource.Name = "SpouseDataSource";
        this.SpouseDataSource.Parameters.AddRange(new Telerik.Reporting.ObjectDataSourceParameter[] {
        new Telerik.Reporting.ObjectDataSourceParameter("employeeNo", typeof(string), empNo)});

then EmployeeProfilesReportManager initiate service and call

    public EmployeeProfilesReportManager()
    {
        _service =Autofac.Integration.Mvc.AutofacDependencyResolver.Current.GetService<IService.IReportEmployeeProfileService>();
        _logExceptionService = Autofac.Integration.Mvc.AutofacDependencyResolver.Current.GetService<IService.ILogException>();
    }

in service layer it implements IDisposable

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

public class ReportEmployeeProfileService : IReportEmployeeProfileService, IDisposable, ICommunicationObject { }

and there are code handle Dispose

    protected virtual void Dispose(bool disposing)
    {
        if (disposed){
            return;
        }
        if (disposing)
        {
            handle.Dispose();
        }
        disposed = true;
    }

    public void Abort()
    {
        _dbContext.Database.Connection.Close();
        _dbContext.Dispose();
        Dispose(true);
    }

    public void Close()
    {
        _dbContext.Database.Connection.Close();
        _dbContext.Dispose();
        Dispose(true);
    }

I expect the server that has problem can run normally like others server. thank you.

after using diagnostic tool I've got this message

DetailID = 18
    Count:    1
    Type:     System.ObjectDisposedException
    Message:  Cannot access a disposed object.
    Stack:    
        [HelperMethodFrame]
        System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrImmutable()
        System.ServiceModel.Channels.CommunicationObject.Open(System.TimeSpan)
        System.ServiceModel.Channels.ServiceChannel+CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(System.ServiceModel.Channels.ServiceChannel, System.TimeSpan)
        System.ServiceModel.Channels.ServiceChannel+CallOnceManager.CallOnce(System.TimeSpan, CallOnceManager)
        System.ServiceModel.Channels.ServiceChannel.EnsureOpened(System.TimeSpan)
        System.ServiceModel.Channels.ServiceChannel.Call(System.String, Boolean, System.ServiceModel.Dispatcher.ProxyOperationRuntime, System.Object[], System.Object[], System.TimeSpan)
        System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(System.Runtime.Remoting.Messaging.IMethodCallMessage, System.ServiceModel.Dispatcher.ProxyOperationRuntime)
        System.ServiceModel.Channels.ServiceChannelProxy.Invoke(System.Runtime.Remoting.Messaging.IMessage)
        System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(System.Runtime.Remoting.Proxies.MessageData ByRef, Int32)
        [TPMethodFrame]
        THP.Welfare.Reporting.ReportManager.EmployeeProfilesReportManager.GetWF_30EmployeeProfile(System.String)
        [DebuggerU2MCatchHandlerFrame]
        [HelperMethodFrame_PROTECTOBJ]
        System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
        System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
        Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].TryResolveDataSourceInstance(Boolean, System.Object ByRef, System.String ByRef)
        Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].ResolveDataSource(Boolean)
        Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].ResolveDataSource()
        Telerik.Reporting.Processing.Data.ObjectDataEnumerable.GetEnumerator()
        Telerik.Reporting.Processing.Data.ResultSetAdapter.Fill(Telerik.Reporting.Processing.Data.ResultSet, System.Collections.Generic.IEnumerable`1<Telerik.Reporting.Processing.IDataObject>)
        Telerik.Reporting.Processing.Data.MultidimentionalDataProvider.Execute(Telerik.Reporting.Processing.Data.MultidimensionalQuery)
        Telerik.Reporting.Processing.Report.ResolveData()
        Telerik.Reporting.Processing.Report.ProcessItemCore()
        Telerik.Reporting.Processing.Report.ProcessItem()
        Telerik.Reporting.Processing.ReportItemBase.ProcessElement()
        Telerik.Reporting.Processing.ProcessingElement.Process(Telerik.Reporting.Processing.Data.IDataMember)
        Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Telerik.Reporting.ReportSource, Telerik.Reporting.Processing.IRenderingContext)
        Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Telerik.Reporting.ReportSource, System.Collections.Hashtable, Telerik.Reporting.Processing.IRenderingContext)
        Telerik.Reporting.Services.Engine.ReportRenderer.Render(Telerik.Reporting.Services.Engine.ReportRendererArgs)
        Telerik.Reporting.Services.Engine.Rendering.RenderWithCulture(Telerik.Reporting.Services.Engine.ReportRendererArgs)
        Telerik.Reporting.Services.Engine.Rendering.ThreadFunc(System.Object)
        System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
        System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
        System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
        System.Threading.ThreadHelper.ThreadStart(System.Object)
        [GCFrame]
        [DebuggerU2MCatchHandlerFrame]
        [ContextTransitionFrame]
        [DebuggerU2MCatchHandlerFrame]


DetailID = 19
    Count:    1
    Type:     System.ObjectDisposedException
    Message:  Cannot access a disposed object.
    Stack:    
        [HelperMethodFrame]
        System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage, System.Runtime.Remoting.Messaging.IMessage)
        System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(System.Runtime.Remoting.Proxies.MessageData ByRef, Int32)
        [TPMethodFrame]
        THP.Welfare.Reporting.ReportManager.EmployeeProfilesReportManager.GetWF_30EmployeeProfile(System.String)
        [DebuggerU2MCatchHandlerFrame]
        [HelperMethodFrame_PROTECTOBJ]
        System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
        System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
        Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].TryResolveDataSourceInstance(Boolean, System.Object ByRef, System.String ByRef)
        Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].ResolveDataSource(Boolean)
        Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].ResolveDataSource()
        Telerik.Reporting.Processing.Data.ObjectDataEnumerable.GetEnumerator()
        Telerik.Reporting.Processing.Data.ResultSetAdapter.Fill(Telerik.Reporting.Processing.Data.ResultSet, System.Collections.Generic.IEnumerable`1<Telerik.Reporting.Processing.IDataObject>)
        Telerik.Reporting.Processing.Data.MultidimentionalDataProvider.Execute(Telerik.Reporting.Processing.Data.MultidimensionalQuery)
        Telerik.Reporting.Processing.Report.ResolveData()
        Telerik.Reporting.Processing.Report.ProcessItemCore()
        Telerik.Reporting.Processing.Report.ProcessItem()
        Telerik.Reporting.Processing.ReportItemBase.ProcessElement()
        Telerik.Reporting.Processing.ProcessingElement.Process(Telerik.Reporting.Processing.Data.IDataMember)
        Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Telerik.Reporting.ReportSource, Telerik.Reporting.Processing.IRenderingContext)
        Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Telerik.Reporting.ReportSource, System.Collections.Hashtable, Telerik.Reporting.Processing.IRenderingContext)
        Telerik.Reporting.Services.Engine.ReportRenderer.Render(Telerik.Reporting.Services.Engine.ReportRendererArgs)
        Telerik.Reporting.Services.Engine.Rendering.RenderWithCulture(Telerik.Reporting.Services.Engine.ReportRendererArgs)
        Telerik.Reporting.Services.Engine.Rendering.ThreadFunc(System.Object)
        System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
        System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
        System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
        System.Threading.ThreadHelper.ThreadStart(System.Object)
        [GCFrame]
        [DebuggerU2MCatchHandlerFrame]
        [ContextTransitionFrame]
        [DebuggerU2MCatchHandlerFrame]

and this is from analysis tool

Thread 24 - System ID 3404



Entry point   clr!LogHelp_LogAssert+18208 
Create time   5/9/2562 19:21:15 
Time spent in user mode   0 Days 00:00:00.015 
Time spent in kernel mode   0 Days 00:00:00.000 


This thread is not fully resolved and may or may not be a problem. Further analysis of these threads may be required.

The thread has evidence of .net exceptions on the stack. Check the Previous .NET Exceptions Report (Exceptions in all .NET Thread Stacks) to view more details of the associated exception  





.NET Call Stack




[[HelperMethodFrame]] 
System_ServiceModel_ni!System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrImmutable()+15a 
System_ServiceModel_ni!System.ServiceModel.Channels.CommunicationObject.Open(System.TimeSpan)+b8 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannel+CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(System.ServiceModel.Channels.ServiceChannel, System.TimeSpan)+12 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannel+CallOnceManager.CallOnce(System.TimeSpan, CallOnceManager)+10d 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannel.EnsureOpened(System.TimeSpan)+28 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannel.Call(System.String, Boolean, System.ServiceModel.Dispatcher.ProxyOperationRuntime, System.Object[], System.Object[], System.TimeSpan)+1af 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(System.Runtime.Remoting.Messaging.IMethodCallMessage, System.ServiceModel.Dispatcher.ProxyOperationRuntime)+61 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannelProxy.Invoke(System.Runtime.Remoting.Messaging.IMessage)+90 
mscorlib_ni!System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(System.Runtime.Remoting.Proxies.MessageData ByRef, Int32)+114 
[[TPMethodFrame] (THP.Welfare.IService.IReportEmployeeProfileService.GetWF_30EmployeeProfiles)] THP.Welfare.IService.IReportEmployeeProfileService.GetWF_30EmployeeProfiles(System.String) 
THP.Welfare.Reporting.ReportManager.EmployeeProfilesReportManager.GetWF_30EmployeeProfile(System.String)+59 
[[DebuggerU2MCatchHandlerFrame]] 
[[HelperMethodFrame_PROTECTOBJ] (System.RuntimeMethodHandle.InvokeMethod)] System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean) 
mscorlib_ni!System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])+80 
mscorlib_ni!System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)+92 
Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].TryResolveDataSourceInstance(Boolean, System.Object ByRef, System.String ByRef)+2ec 
Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].ResolveDataSource(Boolean)+2c 
Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].ResolveDataSource()+23 
Telerik.Reporting.Processing.Data.ObjectDataEnumerable.GetEnumerator()+1d 
Telerik.Reporting.Processing.Data.ResultSetAdapter.Fill(Telerik.Reporting.Processing.Data.ResultSet, System.Collections.Generic.IEnumerable`1)+249 
Telerik.Reporting.Processing.Data.MultidimentionalDataProvider.Execute(Telerik.Reporting.Processing.Data.MultidimensionalQuery)+af 
Telerik.Reporting.Processing.Report.ResolveData()+256 
Telerik.Reporting.Processing.Report.ProcessItemCore()+68 
Telerik.Reporting.Processing.Report.ProcessItem()+a0 
Telerik.Reporting.Processing.ReportItemBase.ProcessElement()+27 
Telerik.Reporting.Processing.ProcessingElement.Process(Telerik.Reporting.Processing.Data.IDataMember)+c4 
Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Telerik.Reporting.ReportSource, Telerik.Reporting.Processing.IRenderingContext)+590 
Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Telerik.Reporting.ReportSource, System.Collections.Hashtable, Telerik.Reporting.Processing.IRenderingContext)+1d0 
Telerik.Reporting.Services.Engine.ReportRenderer.Render(Telerik.Reporting.Services.Engine.ReportRendererArgs)+bc 
Telerik.Reporting.Services.Engine.Rendering.RenderWithCulture(Telerik.Reporting.Services.Engine.ReportRendererArgs)+51 
Telerik.Reporting.Services.Engine.Rendering.ThreadFunc(System.Object)+5a 
mscorlib_ni!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)+160 
mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)+17 
mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)+52 
mscorlib_ni!System.Threading.ThreadHelper.ThreadStart(System.Object)+5d 
[[GCFrame]] 
[[DebuggerU2MCatchHandlerFrame]] 
[[ContextTransitionFrame]] 
[[DebuggerU2MCatchHandlerFrame]] 

Full Call Stack




KERNELBASE!RaiseException+6c 
clr!LogHelp_LogAssert+16565 
clr!LogHelp_LogAssert+16363 
[[HelperMethodFrame]] 
System_ServiceModel_ni!System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrImmutable()+15a 
System_ServiceModel_ni!System.ServiceModel.Channels.CommunicationObject.Open(System.TimeSpan)+b8 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannel+CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(System.ServiceModel.Channels.ServiceChannel, System.TimeSpan)+12 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannel+CallOnceManager.CallOnce(System.TimeSpan, CallOnceManager)+10d 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannel.EnsureOpened(System.TimeSpan)+28 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannel.Call(System.String, Boolean, System.ServiceModel.Dispatcher.ProxyOperationRuntime, System.Object[], System.Object[], System.TimeSpan)+1af 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(System.Runtime.Remoting.Messaging.IMethodCallMessage, System.ServiceModel.Dispatcher.ProxyOperationRuntime)+61 
System_ServiceModel_ni!System.ServiceModel.Channels.ServiceChannelProxy.Invoke(System.Runtime.Remoting.Messaging.IMessage)+90 
mscorlib_ni!System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(System.Runtime.Remoting.Proxies.MessageData ByRef, Int32)+114 
clr!GetMetaDataInternalInterface+1e30e 
clr!GetMetaDataPublicInterfaceFromInternal+2a8f1 
clr!PreBindAssemblyEx+1b12d 
[[TPMethodFrame] (THP.Welfare.IService.IReportEmployeeProfileService.GetWF_30EmployeeProfiles)] THP.Welfare.IService.IReportEmployeeProfileService.GetWF_30EmployeeProfiles(System.String) 
clr!GetMetaDataInternalInterface+1e251 
THP.Welfare.Reporting.ReportManager.EmployeeProfilesReportManager.GetWF_30EmployeeProfile(System.String)+59 
clr+3753 
clr+361c 
clr!GetMetaDataInternalInterface+31766 
clr!GetMetaDataInternalInterface+31550 
[[DebuggerU2MCatchHandlerFrame]] 
[[HelperMethodFrame_PROTECTOBJ] (System.RuntimeMethodHandle.InvokeMethod)] System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean) 
mscorlib_ni!System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])+80 
mscorlib_ni!System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)+92 
Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].TryResolveDataSourceInstance(Boolean, System.Object ByRef, System.String ByRef)+2ec 
Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].ResolveDataSource(Boolean)+2c 
Telerik.Reporting.Processing.Data.ObjectDataProviderBase`1[[System.__Canon, mscorlib]].ResolveDataSource()+23 
Telerik.Reporting.Processing.Data.ObjectDataEnumerable.GetEnumerator()+1d 
Telerik.Reporting.Processing.Data.ResultSetAdapter.Fill(Telerik.Reporting.Processing.Data.ResultSet, System.Collections.Generic.IEnumerable`1<Telerik.Reporting.Processing.IDataObject>)+249 
Telerik.Reporting.Processing.Data.MultidimentionalDataProvider.Execute(Telerik.Reporting.Processing.Data.MultidimensionalQuery)+af 
Telerik.Reporting.Processing.Report.ResolveData()+256 
Telerik.Reporting.Processing.Report.ProcessItemCore()+68 
Telerik.Reporting.Processing.Report.ProcessItem()+a0 
Telerik.Reporting.Processing.ReportItemBase.ProcessElement()+27 
Telerik.Reporting.Processing.ProcessingElement.Process(Telerik.Reporting.Processing.Data.IDataMember)+c4 
Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Telerik.Reporting.ReportSource, Telerik.Reporting.Processing.IRenderingContext)+590 
Telerik.Reporting.Processing.ReportProcessor.ProcessReport(Telerik.Reporting.ReportSource, System.Collections.Hashtable, Telerik.Reporting.Processing.IRenderingContext)+1d0 
Telerik.Reporting.Services.Engine.ReportRenderer.Render(Telerik.Reporting.Services.Engine.ReportRendererArgs)+bc 
Telerik.Reporting.Services.Engine.Rendering.RenderWithCulture(Telerik.Reporting.Services.Engine.ReportRendererArgs)+51 
Telerik.Reporting.Services.Engine.Rendering.ThreadFunc(System.Object)+5a 
mscorlib_ni!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)+160 
mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)+17 
mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)+52 
mscorlib_ni!System.Threading.ThreadHelper.ThreadStart(System.Object)+5d 
clr+3753 
clr+361c 
clr+44d3 
clr!TranslateSecurityAttributes+11f684 
[[GCFrame]] 
clr+6810 
clr+67c0 
clr+6705 
[[DebuggerU2MCatchHandlerFrame]] 
clr+71b0 
clr+725e 
[[ContextTransitionFrame]] 
clr+7193 
clr+67c0 
clr+6705 
[[DebuggerU2MCatchHandlerFrame]] 
clr+6947 
clr!TranslateSecurityAttributes+11f29f 
clr!LogHelp_LogAssert+1827e 
kernel32!BaseThreadInitThunk+22 
ntdll!RtlUserThreadStart+34 



Exception Information


In w3wp__ThaihealthWelfare__PID__2376__Date__09_05_2019__Time_07_21_19PM__859__First Chance System.ObjectDisposedException.dmp the assembly instruction at KERNELBASE!RaiseException in C:\Windows\System32\KERNELBASE.dll from Microsoft Corporation has caused a CLR Exception on thread 24 with the following error information:

    Type:   System.ObjectDisposedException 
    Message:   Cannot access a disposed object. 

This exception originated from System_ServiceModel_ni!System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrImmutable(). 



Module Information 

Image Name: C:\Windows\assembly\NativeImages_v4.0.30319_64\System.ServiceModel\2d239193fcf663b211916bd928cfaeb1\System.ServiceModel.ni.dll   Symbol Type:  None 
Base address: 0x00000000`00005a4d   Time Stamp:  Fri Nov 06 10:20:22 2015  
Checksum: 0x00000000`00000000   Comments:  Flavor=Retail 
COM DLL: False   Company Name:  Microsoft Corporation 
ISAPIExtension: False   File Description:  System.ServiceModel.dll 
ISAPIFilter: False   File Version:  4.6.1055.0 built by: NETFXREL2 
Managed DLL: True   Internal Name:  System.ServiceModel.dll 
VB DLL: False   Legal Copyright:  ? Microsoft Corporation. All rights reserved. 
Loaded Image Name:  System.ServiceModel.ni.dll   Legal Trademarks:   
Mapped Image Name:     Original filename:  System.ServiceModel.dll 
Module name:  System_ServiceModel_ni   Private Build:  DDBLD400 
Single Threaded:  False   Product Name:  Microsoft? .NET Framework 
Module Size:  23.41 MBytes   Product Version:  4.6.1055.0 
Symbol File Name:  System.ServiceModel.ni.dll   Special Build:  & 

after following the answer now I can solve the problem with edit my code as

    #region Field
    private readonly Owned<IService.IReportEmployeeProfileService> _service;
    private readonly Owned<IService.ILogException> _logExceptionService;
    #endregion

    #region Constructor
    public EmployeeProfilesReportManager()
    {
        _service = Autofac.Integration.Mvc.AutofacDependencyResolver.Current.GetService<Owned<IService.IReportEmployeeProfileService>>();
        _logExceptionService = Autofac.Integration.Mvc.AutofacDependencyResolver.Current.GetService<Owned<IService.ILogException>>();


    }
    #endregion

and in all method that I call service like this

    public List<SummaryMarriageInfoBeforeApproveModel> GetSummaryMarriageFromBeforeApprove(string employeeNo)
    {
        var result = _service.Value.GetSummaryMarriageFromBeforeApprove(employeeNo);
        _service.Dispose();
        _logExceptionService.Dispose();
        return result;
    }
c#
iis
autofac
telerik-reporting
asked on Stack Overflow Sep 4, 2019 by Weanich Sanchol • edited Sep 9, 2019 by Weanich Sanchol

1 Answer

1

Minor disclaimer: I know Autofac, but I've never used the Telerik reporting services engine. But I can guess what's happening.

It appears you have a threading/race condition going on.

In the comments, you noted [formatted for readability]:

in in startup.cs have create channel as singleton

builder.Register(c => new ChannelFactory<T>("DefaultService")).SingleInstance();

builder.Register(c => c.Resolve<ChannelFactory<T>>().CreateChannel()).UseWcfSafeRelease();

I assume the <T> is short for what you actually are using.

I also see this snippet:

    public EmployeeProfilesReportManager()
    {
        _service =Autofac.Integration.Mvc.AutofacDependencyResolver.Current.GetService<IService.IReportEmployeeProfileService>();
        _logExceptionService = Autofac.Integration.Mvc.AutofacDependencyResolver.Current.GetService<IService.ILogException>();
    }

The report manager is using service location on the MVC dependency resolver to get some stuff. The important thing to note about the MVC dependency resolver is that it's tied to the current request. When the current request ends, the request lifetime scope is disposed along with all the disposable objects that got resolved in that scope. That includes things like WCF service clients.

Finally, the stack trace for the error that got posted, at the bottom, looks like:

        Telerik.Reporting.Services.Engine.Rendering.RenderWithCulture(Telerik.Reporting.Services.Engine.ReportRendererArgs)
        Telerik.Reporting.Services.Engine.Rendering.ThreadFunc(System.Object)
        System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
        System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
        System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
        System.Threading.ThreadHelper.ThreadStart(System.Object)

There's a ThreadStart call there, so this rendering engine thing appears to be running on a different thread, something that is potentially launched from a controller but maybe isn't tied to the request lifetime.

What I'm guessing happens is:

  • MVC request starts
  • Autofac request lifetime starts (Autofac dependency resolver request lifetime)
  • Reporting service gets set up and starts executing
  • Constructor runs and can resolve the service clients
  • MVC request ends
  • Autofac request lifetime gets disposed along with the service clients
  • Reporting service thread tries to use the clients it resolved but which are now disposed

We see this a lot in Autofac where folks try to spin up background services or non-request-based things in web apps but still try to tie the background service threads to web/request-related dependency resolvers and lifetime scopes.

Given I don't know enough about the Telerik reporting engine, I don't know what hooks there are to allow DI into it. Honestly, I can't even guarantee this is the problem, but it looks exactly like these things that I've seen in other questions on here.

What I'd recommend:

  • Implement a CommonServiceLocator that also has the application container attached. Don't use the MVC dependency resolver in the EmployeeProfileReportManager or anything down that chain.
  • Instead of resolving IService.IReportEmployeeProfileService in your EmployeeProfileReportManager, resolve Owned<IService.IReportEmployeeProfileService>. The Owned<T> relationship in Autofac allows YOU to take control of disposal. This is important because the CommonServiceLocator resolves from the container and you'll get a memory leak if you don't take control of disposal yourself.
  • In your EmployeeProfileReportManager make sure to dispose of the service client when you're done with it. Since you're using Owned<T> that won't automatically happen.
answered on Stack Overflow Sep 5, 2019 by Travis Illig

User contributions licensed under CC BY-SA 3.0