Solved

WCF using ASP.NET session keys

Posted on 2013-01-18
13
1,339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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
 
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Total AV worth it? 4 386
How Do I Widen a Column in a SharePoint 2013 List 2 35
TFS 2015 Access denied. 1 30
URL Link Expander - Want to check safety of shortened links 3 37
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

726 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