Asp.net 在构造函数中使用HttpContextBase时出现Structuremap错误
我正在.NET 4.0上构建一个ASP.NET MVC 2.0应用程序,并正在为IoC使用Structuremap 2.6.1。我最近添加了一个ICookie和Cookie类,Cookie类将HttpContextBase作为构造函数参数(见下文),现在当我运行我的应用程序时,我得到了以下错误:没有为PluginFamily System.Web.HttpContextBase定义默认实例 我以前在另一个具有相同堆栈的MVC应用程序中使用过此方法,但没有得到此错误。我错过什么了吗?如果我确实需要在structuremap配置文件中为HttoContextBase添加一些映射代码,我会使用什么 帮助会很好 Cookie.csAsp.net 在构造函数中使用HttpContextBase时出现Structuremap错误,asp.net,asp.net-mvc-2,.net-4.0,structuremap,Asp.net,Asp.net Mvc 2,.net 4.0,Structuremap,我正在.NET 4.0上构建一个ASP.NET MVC 2.0应用程序,并正在为IoC使用Structuremap 2.6.1。我最近添加了一个ICookie和Cookie类,Cookie类将HttpContextBase作为构造函数参数(见下文),现在当我运行我的应用程序时,我得到了以下错误:没有为PluginFamily System.Web.HttpContextBase定义默认实例 我以前在另一个具有相同堆栈的MVC应用程序中使用过此方法,但没有得到此错误。我错过什么了吗?如果我确实需要
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);
}
}
}
公共类Cookie:ICookie
{
私有只读HttpContextBase_httpContext;
私有静态bool defaultHttpOnly=true;
私有静态浮动defaultExpireDurationInDays=1;
私人只读ICryptographer_加密程序;
公共Cookie(HttpContextBase httpContext,ICryptographer加密程序)
{
Check.Argument.IsNotNull(httpContext,“httpContext”);
Check.Argument.IsNotNull(加密程序,“加密程序”);
_密码学家=密码学家;
_httpContext=httpContext;
}
公共静态bool DefaultHttpOnly
{
[调试步骤至]
获取{return defaultHttpOnly;}
[调试步骤至]
设置{defaultHttpOnly=value;}
}
公共静态浮动默认到期天数
{
[调试步骤至]
获取{return defaultExpireDurationInDays;}
[调试步骤至]
设置
{
Check.Argument.IsNotZeroOrNegative(value,“value”);
defaultExpireDurationInDays=值;
}
}
公共T GetValue(字符串键)
{
返回GetValue(key,false);
}
公共T GetValue(字符串键,bool expireOnceRead)
{
var cookie=_httpContext.Request.Cookies[key];
T值=默认值(T);
if(cookie!=null)
{
如果(!string.IsNullOrWhiteSpace(cookie.Value))
{
var converter=TypeDescriptor.GetConverter(typeof(T));
尝试
{
value=(T)converter.ConvertFromString(_cryptographer.Decrypt(cookie.value));
}
捕获(不支持异常)
{
if(converter.CanConvertFrom(typeof(string)))
{
value=(T)converter.ConvertFrom(_cryptographer.Decrypt(cookie.value));
}
}
}
如果(到期日)
{
cookie=_httpContext.Response.Cookies[key];
if(cookie!=null)
{
cookie.Expires=DateTime.Now.AddDays(-100d);
}
}
}
返回值;
}
公共无效设置值(字符串键,T值)
{
SetValue(键、值、DefaultExpiredOrientifInDays、DefaultHttpOnly);
}
公共void设置值(字符串键、T值、浮点过期天数)
{
SetValue(键、值、过期天数、DefaultHttpOnly);
}
public void SetValue(字符串键、T值、bool httpOnly)
{
SetValue(键、值、DefaultExpireDurationInDays、仅httpOnly);
}
public void SetValue(字符串键、T值、浮点过期天数、bool httpOnly)
{
TypeConverter converter=TypeDescriptor.GetConverter(typeof(T));
string cookieValue=string.Empty;
尝试
{
cookieValue=converter.ConvertToString(值);
}
捕获(不支持异常)
{
if(converter.CanConvertTo(typeof(string)))
{
cookieValue=(字符串)转换器.ConvertTo(值,类型(字符串));
}
}
如果(!string.IsNullOrWhiteSpace(cookieValue))
{
var cookie=新的HttpCookie(密钥,_cryptographer.Encrypt(cookieValue))
{
Expires=DateTime.Now.AddDays(expiredOriendindays),
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");
}
}
公共类iocmap
{
公共静态void Configure()
{
var connectionString=System.Configuration.ConfigurationManager.connectionString[“ProjectName.Core.Properties.Settings.ProjectNameConnectionString”]。connectionString;
MappingSource MappingSource=新属性MappingSource();
ObjectFactory.Initialize(x=>
{
x、 扫描(扫描=>
{
scan.Assembly(“ProjectName.Core”);
scan.Assembly(“ProjectName.WebUI”);
scan.WithDefaultConventions();
});
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use().Ctor(“连接”).Is(connectionString).Ctor(“mappingSource”).Is(mappingSource);
x、 For().Singleton().Use();
x、 For().Singleton().Use().Ctor().Is(HttpRuntime.Cache);
x、 For().Singleton().Use();
x、 For().httpContextScope().Use();
x、 For().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().httpContextScope().Use();
x、 For().HttpCon
For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current));