Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 942507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:17:02+00:00 2026-05-15T22:17:02+00:00

I have a WCF service that is using a custom UsernamePasswordValidator. The validator needs

  • 0

I have a WCF service that is using a custom UsernamePasswordValidator. The validator needs to access my entity framework context.

I would like to create one ObjectContext for the entire service call and then destroy/dispose it at the end of the call. So I created a singleton static class that provided this functionality, however, what’s happening now is that if two service calls happen concurrently, one of the calls disposes the singleton.

I either keep a local reference to the ObjectContext, in which case the second service to use it sees it as disposed and throws and error, or, I put a wrapper property around the Singleton class wherever I need it and then all my changes get thrown away because I’m getting a new instance of the object if another call has disposed it.

So basically my question is how do I instantiate an ObjectContext per service call?

NOTE: The instance needs to be accesible in both the service code AND the custom UsernamePasswordValidator code.

I can’t just do it in the constructor or use a using statement because then the custom UsernamePasswordValidator doesn’t have access to it. Is there a way to have a static class per call? It does sound impossible, but what’s the way around this? Should I be caching the object in a session?

My service is hosted in IIS.

UPDATE:
So I’ve nailed this down to storing state in the InstanceContext using an IExtension object. But How do I access the current InstanceContext in a UsernamePasswordValidator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:17:03+00:00Added an answer on May 15, 2026 at 10:17 pm

    Ok, so in the end I solved it by using the following static class and relying on ASP.NET to cache the context for me.

    I’m not sure if this is the best way to do things, but this allows me to use one ObjectContext per request so I’m not spinning up too many and this also means I don’t have to use a lock on the object which would become a nightmare if many users were using the service.

    public static class MyContextProvider
        {
            public static MyModel Context
            {
                get
                {
                    if (HttpContext.Current.Items["context"].IsNull())
                    {
                        HttpContext.Current.Items["context"] = new MyModel();
                    }
    
                    return HttpContext.Current.Items["context"] as MyModel;
                }
            }    
        }
    

    Then wherever I need an ObjectContext in the app I just call

    var context = MyContextProvider.Context;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF service that needs to hosted using basicHttpBinding using SSL. So
I have a WCF service and a client, that uses a custom UserNamePasswordValidator for
I have a WCF service that I secure with a custom UserNamePasswordValidator and Message
I have a WCF service that is using a custom ServiceAuthorizationManager . The custom
I have a Data Service created using WCF that internally uses nHibernate. This WCF
I have a .NET web service (using asmx...have not upgraded to WCF yet) that
I have a WCF service that needs to be secured via https. The WCF
I have a WCF service that needs to know the Principal of the calling
I have a WCF service which I would like to product XML and JSON
I have an existing .NET 3.5 based framework that is extended using custom plugins.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.