• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1389
  • Last Modified:

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.
0
Derrick Hammond
Asked:
Derrick Hammond
  • 6
  • 6
1 Solution
 
ambienceCommented:
Does your service use HttpBinding? You forgot to attach files :)
0
 
ambienceCommented:
>> I have added a WCF Service to it for communication with a desktop application.

Does your desktop application request the service only? If the request is coming from an existing session only then it would be available in the WCF service. Normally you'd use a CookieContainer on the client side and attach it to every WebRequest - REST services.

When consuming WCF services either using proxies or the Channel you'd need to inject the session Id every time you call a method.

See this: http://blogs.msdn.com/b/wenlong/archive/2010/02/21/using-asp-net-sessions-from-wcf.aspx
0
 
Derrick HammondApplications DeveloperAuthor Commented:
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
0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
ambienceCommented:
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.
0
 
Derrick HammondApplications DeveloperAuthor Commented:
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.
0
 
ambienceCommented:
OK can you confirm that if you use session state in DoWork then it fails as well?
0
 
ambienceCommented:
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?
0
 
Derrick HammondApplications DeveloperAuthor Commented:
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.
0
 
Derrick HammondApplications DeveloperAuthor Commented:
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.
0
 
ambienceCommented:
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.
0
 
Derrick HammondApplications DeveloperAuthor Commented:
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?
0
 
Derrick HammondApplications DeveloperAuthor Commented:
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.
0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now