Solved

WCF using ASP.NET session keys

Posted on 2013-01-18
13
1,311 Views
Last Modified: 2013-11-16
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
Comment
Question by:Derrick Hammond
  • 6
  • 6
13 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 38796624
Does your service use HttpBinding? You forgot to attach files :)
0
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 38796652
>> 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
 

Author Comment

by:Derrick Hammond
ID: 38801476
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
 
LVL 22

Expert Comment

by:ambience
ID: 38802707
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
 

Author Comment

by:Derrick Hammond
ID: 38802833
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
 
LVL 22

Expert Comment

by:ambience
ID: 38802904
OK can you confirm that if you use session state in DoWork then it fails as well?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:ambience
ID: 38802936
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
 

Author Comment

by:Derrick Hammond
ID: 38802953
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
 

Author Comment

by:Derrick Hammond
ID: 38803491
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
 
LVL 22

Expert Comment

by:ambience
ID: 38803512
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
 

Author Comment

by:Derrick Hammond
ID: 38805608
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
 

Author Closing Comment

by:Derrick Hammond
ID: 38814573
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to count occurrences of each item in an array.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now