简体   繁体   中英

WCF Caching List Error

I am trying to cache a dynamic list retrieved from a SQL Server in WCF, the retrieving process is working fine so far, and I've tested it and there are no problems at all with it, the problem is when I try to cache this retrieved list, an error occurs that I am not able to find out the reason behind it.

Here is my method :

public List<ErrorEntities> GetALL()
{
            List<ErrorEntities> res = null;
            if (HttpContext.Current.Cache["GetALL"] == null)
            {
                SqlCommand com = Global.GetCommand("select * from [BlockingList]");
                com.Connection.Open();
                SqlDataReader reader = com.ExecuteReader();
                res = Fill(reader);

                HttpContext.Current.Cache.Add("GetALL", res, null, DateTime.Now.Add(new TimeSpan(0, 0, 15)), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

                com.Connection.Close();
                return res;
            }
            else
            {
                res = HttpRuntime.Cache["GetALL"] as List<ErrorEntities>;
                return res;
            }
}

I've tried to enable the caching on the web.config file by adding the following line of code but the problem wasn't solved as well:

<scriptResourceHandler enableCompression="true"    enableCaching="true" />

and here is the error I get when I compile the solution:

The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.

 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more 

information about the error and where it originated in the code.

 Exception Details: System.ServiceModel.FaultException: The server was unable to process the request due to an internal error. For more 

information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.

 Source Error: Line 113: Line 114: public ServiceReference1.ErrorEntities[] GetALL() { Line 115: return base.Channel.GetALL(); Line 116: } Line 117: 

I assume that code shown in question is called inside WCF method.

WCF works in its own context and HttpContext.Current should not be used there.

Microsoft added new cache that has no System.Web dependency in .NET 4.0.

MemoryCache should work fine in WCF.

Try to use this code for getting cache object:

ObjectCache cache = MemoryCache.Default;

http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

I would also recommend to use using or try-finally blocks for disposable resources ( Connection in code sample)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM