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 1097279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:24:31+00:00 2026-05-17T00:24:31+00:00

I am building a ASP.NET MVC 2.0 app on .NET 4.0 and am using

  • 0

I am building a ASP.NET MVC 2.0 app on .NET 4.0 and am using Structuremap 2.6.1 for IoC. I recently added a ICookie and Cookie class, the Cookie class takes HttpContextBase as a constructor parameter (See below) and now when I run my app I get this error :No Default Instance defined for PluginFamily System.Web.HttpContextBase.

I have used this method before in another MVC app with the same stack but did not get this error. Am I missing something? If I do need to add some mapping code for HttoContextBase in my structuremap configuration file what would I use?

And help would be great!!!

Cookie.cs

public class Cookie : ICookie
{
    private readonly HttpContextBase _httpContext;
    private static bool defaultHttpOnly = true;
    private static float defaultExpireDurationInDays = 1;
    private readonly ICryptographer _cryptographer;
    public Cookie(HttpContextBase httpContext, ICryptographer cryptographer)
    {
        Check.Argument.IsNotNull(httpContext, "httpContext");
        Check.Argument.IsNotNull(cryptographer, "cryptographer");
        _cryptographer = cryptographer;
        _httpContext = httpContext;
    }
    public static bool DefaultHttpOnly
    {
        [DebuggerStepThrough]
        get { return defaultHttpOnly; }

        [DebuggerStepThrough]
        set { defaultHttpOnly = value; }
    }

    public static float DefaultExpireDurationInDays
    {
        [DebuggerStepThrough]
        get { return defaultExpireDurationInDays; }

        [DebuggerStepThrough]
        set
        {
            Check.Argument.IsNotZeroOrNegative(value, "value");
            defaultExpireDurationInDays = value;
        }
    }

    public T GetValue<T>(string key)
    {
        return GetValue<T>(key, false);
    }

    public T GetValue<T>(string key, bool expireOnceRead)
    {
        var cookie = _httpContext.Request.Cookies[key];
        T value = default(T);
        if (cookie != null)
        {
            if (!string.IsNullOrWhiteSpace(cookie.Value))
            {
                var converter = TypeDescriptor.GetConverter(typeof(T));
                try
                {
                    value = (T)converter.ConvertFromString(_cryptographer.Decrypt(cookie.Value));
                }
                catch (NotSupportedException)
                {
                    if (converter.CanConvertFrom(typeof(string)))
                    {
                        value = (T)converter.ConvertFrom(_cryptographer.Decrypt(cookie.Value));
                    }
                }
            }
            if (expireOnceRead)
            {
                cookie = _httpContext.Response.Cookies[key];

                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-100d);
                }
            }
        }
        return value;
    }

    public void SetValue<T>(string key, T value)
    {
        SetValue(key, value, DefaultExpireDurationInDays, DefaultHttpOnly);
    }

    public void SetValue<T>(string key, T value, float expireDurationInDays)
    {
        SetValue(key, value, expireDurationInDays, DefaultHttpOnly);
    }

    public void SetValue<T>(string key, T value, bool httpOnly)
    {
        SetValue(key, value, DefaultExpireDurationInDays, httpOnly);
    }

    public void SetValue<T>(string key, T value, float expireDurationInDays, bool httpOnly)
    {
        TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
        string cookieValue = string.Empty;
        try
        {
            cookieValue = converter.ConvertToString(value);
        }
        catch (NotSupportedException)
        {
            if (converter.CanConvertTo(typeof(string)))
            {
                cookieValue = (string)converter.ConvertTo(value, typeof(string));
            }
        }
        if (!string.IsNullOrWhiteSpace(cookieValue))
        {
            var cookie = new HttpCookie(key, _cryptographer.Encrypt(cookieValue))
            {
                Expires = DateTime.Now.AddDays(expireDurationInDays),
                HttpOnly = httpOnly
            };
            _httpContext.Response.Cookies.Add(cookie);
        }
    }
}

IocMapping.cs

public class IoCMapping
{
    public static void Configure()
    {

        var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ProjectName.Core.Properties.Settings.ProjectNameConnectionString"].ConnectionString;
        MappingSource mappingSource = new AttributeMappingSource();
        ObjectFactory.Initialize(x =>
        {
            x.Scan(scan =>
            {
                scan.Assembly("ProjectName.Core");
                scan.Assembly("ProjectName.WebUI");
                scan.WithDefaultConventions();
            });
            x.For<IUnitOfWork>().HttpContextScoped().Use<UnitOfWork>();
            x.For<IDatabase>().HttpContextScoped().Use<Database>().Ctor<string>("connection").Is(connectionString).Ctor<MappingSource>("mappingSource").Is(mappingSource);
            x.For<ILogger>().Singleton().Use<NLogLogger>();
            x.For<ICacheManager>().Singleton().Use<CacheManager>().Ctor<System.Web.Caching.Cache>().Is(HttpRuntime.Cache);
            x.For<IEmailSender>().Singleton().Use<EmailSender>();
            x.For<IAuthenticationService>().HttpContextScoped().Use<AuthenticationService>();
            x.For<ICryptographer>().Use<Cryptographer>();
            x.For<IUserSession>().HttpContextScoped().Use<UserSession>();
            x.For<ICookie>().HttpContextScoped().Use<Cookie>();
            x.For<ISEORepository>().HttpContextScoped().Use<SEORepository>(); 
            x.For<ISpotlightRepository>().HttpContextScoped().Use<SpotlightRepository>(); 
            x.For<IContentBlockRepository>().HttpContextScoped().Use<ContentBlockRepository>();
            x.For<ICatalogRepository>().HttpContextScoped().Use<CatalogRepository>();
            x.For<IPressRoomRepository>().HttpContextScoped().Use<PressRoomRepository>();
            x.For<IEventRepository>().HttpContextScoped().Use<EventRepository>();
            x.For<IProductRegistrationRepository>().HttpContextScoped().Use<ProductRegistrationRepository>();
            x.For<IWarrantyRepository>().HttpContextScoped().Use<WarrantyRepository>();
            x.For<IInstallerRepository>().HttpContextScoped().Use<InstallerRepository>();
            x.For<ISafetyNoticeRepository>().HttpContextScoped().Use<SafetyNoticeRepository>();
            x.For<ITradeAlertRepository>().HttpContextScoped().Use<TradeAlertRepository>();
            x.For<ITestimonialRepository>().HttpContextScoped().Use<TestimonialRespository>();
            x.For<IProjectPricingRequestRepository>().HttpContextScoped().Use<ProjectPricingRequestRepository>();
            x.For<IUserRepository>().HttpContextScoped().Use<UserRepository>();
            x.For<IRecipeRepository>().HttpContextScoped().Use<RecipeRepository>();
        });

        LogUtility.Log.Info("Registering types with StructureMap");
    }
}
  • 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-17T00:24:32+00:00Added an answer on May 17, 2026 at 12:24 am

    I believe you would need to register the HttpContextBase on every request in your Begin_Request handler like so:

    For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current));
    

    Update: Make sure you register a lambda, otherwise you StructureMap will store the HttpContext available at registration time as a singleton.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building small web shop with asp.net mvc and Structuremap ioc/di. My Basket class
My team is considering building our next web app using the ASP.NET MVC framework.
I'm building an ASP.NET MVC app and I'm using a repository to store and
I am building my first ASP.Net MVC based app and have a problem accessing
I am currently building an application using ASP.NET MVC. The data entry pages are
I'm am building my asp.net web application using MVC (Preview 5), and am also
I'm building an asp.net MVC 2 app. I have a list view which lists
I'm building an advanced search form for my ASP.NET MVC app. I have a
I am building an ASP.NET MVC application using the 1.0 release using Visual Web
I am building an ASP.NET 4.0 MVC 2 app with a generic repository based

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.