Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Dear Geeks,

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 :

C#
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:

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


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

C#
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 <serviceDebug> 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 <serviceDebug> 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:        
Posted

1 solution

I have figured out the solution

all we need to do here is to allow the compatibility of aspnet caching through adding the line in the web.config :

<servicehostingenvironment aspnetcompatibilityenabled="true" />   


and in your data access layer, add the following line at the very top of your class like this :

C#
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]


so that the class looks like this :

C#
using System.ServiceModel.Activation;
namespace WcfService2
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
       
        public List<errorentities> GetALL()
        {


            List<errorentities> res = null;
           if (CacheHelper.GetCache("GetALL")==null)
               {
                string cacheKey = "GetAll";
                SqlCommand com = Global.GetCommand("select * from [BlockingList]");
                com.Connection.Open();
                SqlDataReader reader = com.ExecuteReader();
                res = Fill(reader);
                CacheHelper.Cache(cacheKey, res, 2);
                com.Connection.Close();
                return res;
              
               }
           else
               {
                res = CacheHelper.GetCache("GetALL") as List<errorentities>;
                return res;
                }
        }

</errorentities></errorentities></errorentities>


as you noticed you must add

C#
using System.ModelService.Activation; 


so that the added service could work,,

hope this might save for anyone his valuable time, because i've been working on this problem for 3days with no hope to find any solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900