Link to home
Start Free TrialLog in
Avatar of Derrick Hammond
Derrick HammondFlag for United States of America

asked on

WCF using ASP.NET session keys

I have an ASP.NET 4 application that uses OAuth and OpenID. I have added a WCF Service to it for communication with a desktop application. I have been able to pass a simple string value from the web app to the desktop app but when I try to initialize a method in the web app from the desktop app I get a Null Reference Exception on the HttpContext.Current.Session.Keys.Count.
The session keys are not being referenced when the method is initialized from the Service.
I need to pass the existing keys to the method to be able to run it.

I have tried to set the asp 'compatibility mode' to required and allowed but the service will not run with the required code segments inserted.

Attached are the code files that I am working with, I have removed the 'compatibility mode' statements.
Avatar of ambience
ambience
Flag of Pakistan image

Does your service use HttpBinding? You forgot to attach files :)
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Derrick Hammond

ASKER

ModernMatt:
Thanks for the tips on posting! I am new to this blog and I wasn't sure how to handle the zones for my post. Thanks for the advice.

ambience:
I am using wsHttpBinging.
I am new to development and using WCF services so some of my questions may be very basic.
The client application (desktop application) is a large application and requires a login so I believe there is an active session.
The host application (cloud application) requires a separate login to the cloud servers it accesses for the required data.

The solution I am developing:
 The user logs into the client application then the host application is called and the user must login to the host application before the data can be accessed by the client application. Once both applications are active the client application needs to be able to call the host to run its processes to get the required data from the cloud servers then pass that data back to the client. The host application currently does all the required connections to the cloud servers and the client application connects to the host at a very basic level using WCF wcHttpBindings.
Problem:
When I call the host methods from the client; the host errors when it tries to run the methods because the methods require a System.Web.HttpContext.Current.Session.Keys.Count. I get a Null Reference Exception when any method is called from the WCF service in the host.
The methods work when they are run by clicking a button inside the host application but they do not work when they are called by the WCF service.
Questions:
Is there a way to make the service call the methods as if the user has clicked a button?
Is there a better way to do this connection?
Can I make the client application run the login process in the host application so it doesn't require a dual login by the user?

I will attach the files in a zip folder. The code has been modified slightly for confidentiality.
attachments.zip
Inside the implementation of DoWork do you happen to access the Session state? I wonder why one is working and the others not. I would expect both to fail.
I do not call any session state when running DoWork(). That was the first call I made with the service just to test if the service would communicate. It passes a single string value from the host to the client.
OK can you confirm that if you use session state in DoWork then it fails as well?
When the user logins the host application the response would contain the ASPNET session cookie. You'll have to keep that value around and inject it into each of the WCF method calls as shown in the sample article in my earlier post.

I dont quite understand why you do a client login and then a host login. Why cant you just do a host login?
Not sure how to implement that. DoWork() doesn't try to run any methods in the host other than what is in the service where the others actually call the host to run its methods to give a value to a variable.
Is the session state of the service the same as the session state of the host? I don't think the issue is with the session state of the service but with the session state of the host. The host methods being called from the service are looking for the session state of the host, not the session state from the service.
Thank you for the explanation.
I will take another look at the article.

The host application is a work around application for my companies main software. It's a temporary add-in for marketing purposes and getting data from a particular cloud data source until our software is cloud based.
Im a little confused now about what you mean when you session keys are not being referenced when the method is initialized from the Service'.

>> The host methods being called from the service are looking for the session state of the host, not the session state from the service.

This is as expected, since these are entirely different sessions. Since the host is in cloud I would say the best bet is to actually pass the service session variable either as parameters to the host or headers.

I maybe off target here, let me know if this even makes sense.
I think I understand.
When you say :

>>pass the service session variable either as parameters to the host or headers.

Would you then be adding the session variable to the host session state or would you just run the methods off the service session variable?
Was able to pass the SessionId using the example situation posted. I had to do a little reworking to get it pass but most of the example worked with my solution.