0

I'll preface this by saying that I've been developing in .NET for many years, and VB/BASIC for many years before that, but my background is mostly in desktop applications and system interfaces - I'm still pretty new to all this web application stuff, so I apologize up front if this is a silly question.

That said, here's my question: when you create an object in an ASPX page and store it in cache, how can you access it from an ASMX web service that resides in the same application?

To further elaborate: I have a single web application which includes ASPX pages, an ASMX web service, and a class library consisting of two object classes. When the user signs in to the application, they configure some settings, and the objects are created and stored in a system.Web.Caching.Cache object. The custom objects are then pulled out of cache on the next page, and the user then makes an AJAX call (via jQuery) to the web service to retrieve some data.

The problem is that in the web service response, I need to parse the returned data based on the content of the user-created objects stored in the web application's cache. However, I can't find any way to access the cached object from inside the web service.

I have a sneaking suspicion that it may be possible to serialize my custom .NET objects into JSON objects and pass them via the AJAX call to the web service for deserialization, but frankly, I would have no idea how to even begin at that. Plus the objects are potentially 30 - 40K in size, and the AJAX call is being made as frequently as once every 3 seconds, so I'd really like to avoid the overhead of passing all that extra data with each call, especially since the data I need is already sitting in memory in the application where the web service resides.

So once again, I ask: when you create an object in an ASPX page and store it in cache, how can you access it from an ASMX web service that resides in the same application?

Is this making sense? Am I crazy? Missing something obvious? Any insight anyone can provide would be VERY highly appreciated. Thanks!

2 Answers 2

6

You can access current HTTP pipeline state using HttpContext.Current. So for accessing the cache, you need to use HttpContext.Current.Cache.

BTW, asmx web services are considered to be legacy technology (see this) - so I will suggest you to migrate to WCF services. If you go for WCF services, then you must enable ASP.NET compatibility mode (see this) to access HttpContext.

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

1 Comment

That did it! I knew there had to be an easy way to do it, I just didn't know that object model. Thanks a ton!
0

I have not tried what you are looking for but objects are stored in cache in the form of key value pairs where key can be anything from simple integer to guids and value being your object. As per my understanding the asmx service just needs the key so that it can look into the cache and returns the object. But again its my understanding not I had tried this scenario.

3 Comments

Any idea how I'd do that in code? When I do this: ` Dim cacheObj As New System.Web.Caching.Cache Dim MyWall As Wall = cacheObj.Get("Wall") ` I get an "object reference not set to an instance of an object" error...
@Erick - it looks like you're creating a new instance of the cache, not accessing the current one. I think you want to use VinayC's approach in his answer.
D'oh! Yeah, that would be pretty obvious, huh? That's what I get for debugging at the end of a 14 hour work day. VinnyC's answer was spot on. Thanks again for your help :-)

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.