views:

49

answers:

1

My requirement is to call WCF web service from ASP.NET code behind and pass some data for example:

void Add(int x, int y);

Result of the operation should be somehow stored within WCF web service (member variable). Later user should be able to call

double Equals();

and get the result of the operation. Of course since it's an open website many non-authenticated users can open the ASP.NET page and call the web service.

Alternatively Add method can return "key" that can be used to get result back, for example :

Guid Add(int x, int y);
double Equals(Guid key);

I don't think I can use WCF sessions since web service will think that there is only 1 client ( WebSite calling the webserivce hosted in IIS ).
Users are not authenticated - they are anonymous users.


I could use : ( please take a look at my EDIT )

InstanceContextMode =InstanceContextMode.Single

as attribute over my web service and store Dictionary<key, result> but the problem is when can I remove entry from Dictionary?
Should I use some sort of BackgroundWorker to check dictionary every e.g. 15 minutes and remove old entries? In that case I would have to store DateTime against each entry but that's not a problem if this is the best way to implement this solution...

My example Add/Equals methods are only to illustrate the problem, real requirement need to store much more data per user than simple double result

Thanks in advance for all the help.

EDIT:
After thinking more about the InstanceContextMode =InstanceContextMode.Single I think it's not the best option to use with ASP.NET since if 100 users will call the web service at the same time I will get 100 sequential invocations which will cause performance issues.
In that case do you have any ideas how to implement this without changing InstanceContextMode setting (which by default is PerSession as far as I know) ?

A: 

I think,Keeping the

InstanceContextMode =InstanceContextMode.Single 

as the service behavior, if your architecture permits, you can use the System.Web.Caching.Cache object implemented in a static class(so that it will be instantiated only once) ,where you can specify the cache dependency/expiration time etc and hence you can get the complete set of functionalities offered from the cache service, instead of implementing on your own.

Edit: If your primary intention is about Caching, then you need not use the Instance Context mode even. You can simply implement your own static Cache provider based on the System.Web.Caching.Cache and pass the cacheKey as shown in the following sample:

 public int Add(int a, int b)
        {
            string cacheKey = a.ToString()+b.ToString();
            if (!CacheProvider.Contains(cacheKey))
            {
                CacheProvider.Add(cacheKey, a + b);
                return a + b;
            }
            else
            {
                return (int)CacheProvider.GetValue(cacheKey);
            }

        }

CacheProvider in the above code is the static implementation of your Cache provider.

Siva Gopal
Thanks for your reply. I was thinking about InstanceContextMode.Single and now I think I cannot use it... please see my Edit for more details. If you have any other idea how this can be implemented please let me know. Thanks
Thundrel
Siva Gopal
Please see the Edit of my previous answer and if the solution can fix your problem, then please don't forget to mark it as answer :-))
Siva Gopal