1

I am in the middle of big web application, I use Entity Framework as my data service, now we need some windows application to work with our data, so I want to give them a service with WCF

But when my client wants to get service some error is happened from my public property which I use for caching Entity Model

    public partial class DepositEntities : ObjectContext
    {
        public static DepositEntities Current
        {
            get
            {
                DepositEntities oc = 
                    HttpContext.Current.Items["ObjectContext"] as DepositEntities;

                if (oc == null)
                {
                    oc = new DepositEntities();
                    HttpContext.Current.Items["ObjectContext"] = oc;
                }

                return oc;
            }
        }
    }

I know the problem is from this line, after I debug my code

DepositEntities oc = System.Web.HttpContext.Current.Items["ObjectContext"] as DepositEntities;

When I change my Current property body to some thing like this

public static DepositEntities Current
{
   get
   {
      DepositEntities oc = new DepositEntities();
      return oc;
   }
}

everything is OK when I get data from services I have no problem

But everywhere I have join in my codes I have problem because It thinks there are different data source because of new DepositEntities();

6
  • Does it raise an exception or just not give back any data? Commented Apr 14, 2012 at 6:49
  • Dear @LosFrijoles Exception is just Object reference not set to an instance of an object. for this line {DepositEntities oc = System.Web.HttpContext.Current.Items["ObjectContext"] as DepositEntities;} but this line works correctly in my whole project when I want to use WCF I get this error Commented Apr 14, 2012 at 6:58
  • The HttpContext only exists in the context of an ASP.NET web application. A Windows Forms application doesn't have an HttpContext so you cannot use it to cache data..... you need to check out some other means of caching that's not depedent on HttpContext. Commented Apr 14, 2012 at 7:12
  • @marc_s give me some alternative !? Commented Apr 14, 2012 at 7:31
  • 1
    You could check out the new .NET 4 System.Runtime.Caching namespace for general purpose caching that is available to all project types and doesn't depend on a HttpContext or anything Commented Apr 14, 2012 at 8:02

2 Answers 2

1

You're most likely experiencing problems because WCF doesn't have HttpContext.Current. Read more about contexts in WCF - this question may be a good start: http://social.msdn.microsoft.com/Forums/en/wcf/thread/27896125-b61e-42bd-a1b0-e6da5c23e6fc.

I also think it would be better for you to manage lifetime of an ObjectContext with a DI Container (ie. Castle Windsor). Thanks to this, it won't be necessary to expose static property Current which is a problem for WCF service, unit tests, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

your answer seems correct but I couldn't find my solution it's a little complicated for me! can you tell me which session should I use to solve the problem !?
I change my way! I use Web Service in my client instead of using WCF
1

Check out "Hosting WCF Services in ASP.NET Compatibility Mode" in wcf service and ASP.NET. It explains how to get a valid HttpContext in a wcf service.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.