本文整理汇总了C#中CreationContext类的典型用法代码示例。如果您正苦于以下问题:C# CreationContext类的具体用法?C# CreationContext怎么用?C# CreationContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CreationContext类属于命名空间,在下文中一共展示了CreationContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Request
public virtual object Request(CreationContext context)
{
object instance;
using(rwlock.ForWriting())
{
if (available.Count != 0)
{
instance = available.Pop();
if (instance == null)
{
throw new PoolException("Invalid instance on the pool stack");
}
}
else
{
instance = componentActivator.Create(context);
if (instance == null)
{
throw new PoolException("Activator didn't return a valid instance");
}
}
inUse.Add(instance);
}
return instance;
}
示例2: Resolve
public override object Resolve(CreationContext context)
{
HttpContext current = HttpContext.Current;
if (current == null)
throw new InvalidOperationException(
"HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net");
if (current.Items[PerRequestObjectID] == null)
{
if (!PerWebRequestLifestyleModule.Initialized)
{
string message = "Looks like you forgot to register the http module " +
typeof(PerWebRequestLifestyleModule).FullName +
"\r\nAdd '<add name=\"PerRequestLifestyle\" type=\"Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel\" />' " +
"to the <httpModules> section on your web.config";
throw new ConfigurationErrorsException(message);
}
object instance = base.Resolve(context);
current.Items[PerRequestObjectID] = instance;
PerWebRequestLifestyleModule.RegisterForEviction(this, instance);
}
return current.Items[PerRequestObjectID];
}
示例3: ObtainInterceptors
/// <summary>
/// Obtains the interceptors associated with the component.
/// </summary>
/// <param name="kernel">The kernel instance</param>
/// <param name="model">The component model</param>
/// <param name="context">The creation context</param>
/// <returns>interceptors array</returns>
protected IInterceptor[] ObtainInterceptors(IKernel kernel, ComponentModel model, CreationContext context)
{
var interceptors = new List<IInterceptor>();
foreach (IReference<IInterceptor> interceptorRef in GetInterceptorsFor(model))
{
try
{
var interceptor = interceptorRef.Resolve(kernel, context);
SetOnBehalfAware(interceptor as IOnBehalfAware, model);
interceptors.Add(interceptor);
}
catch (Exception e)
{
foreach (var interceptor in interceptors)
{
kernel.ReleaseComponent(interceptor);
}
if(e is InvalidCastException)
{
var message = String.Format(
"An interceptor registered for {0} doesn't implement the {1} interface",
model.Name, typeof(IInterceptor).Name);
throw new DependencyResolverException(message);
}
throw;
}
}
return interceptors.ToArray();
}
示例4: Resolve
public object Resolve(CreationContext context,
ISubDependencyResolver parentResolver,
ComponentModel model,
DependencyModel dependency)
{
return _kernel.ResolveAll(dependency.TargetType.GetElementType(), null);
}
示例5: Resolve
public override object Resolve(CreationContext context)
{
lock(slot)
{
var map = (Dictionary<IComponentActivator, object>)Thread.GetData(slot);
if (map == null)
{
map = new Dictionary<IComponentActivator, object>();
Thread.SetData( slot, map );
}
Object instance;
if (!map.TryGetValue(ComponentActivator, out instance))
{
instance = base.Resolve(context);
map.Add(ComponentActivator, instance);
instances.Add(instance);
}
return instance;
}
}
示例6: GetSubHandler
protected IHandler GetSubHandler(CreationContext context, Type genericType)
{
lock(type2SubHandler)
{
IHandler handler;
if (type2SubHandler.ContainsKey(genericType))
{
handler = type2SubHandler[genericType];
}
else
{
Type service = ComponentModel.Service.MakeGenericType(context.GenericArguments);
ComponentModel newModel = Kernel.ComponentModelBuilder.BuildModel(
ComponentModel.Name, service, genericType, null);
newModel.ExtendedProperties[ComponentModel.SkipRegistration] = true;
Kernel.AddCustomComponent(newModel);
handler = Kernel.HandlerFactory.Create(newModel);
type2SubHandler[genericType] = handler;
}
return handler;
}
}
示例7: CanResolve
public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model,
DependencyModel dependency)
{
return dependency.TargetType == typeof(IBookStore) && contextHandlerResolver.CanResolve(context, contextHandlerResolver, model,
new DependencyModel(typeof(IBookStore).FullName,
typeof(IBookStore), false));
}
示例8: GetAzureServiceConfig
private static IMicrosoftStorageConfig GetAzureServiceConfig(IKernel k, ComponentModel cm, CreationContext cc)
{
var serviceConfig = new DefaultAzureServiceConfigurationProvider(k.Resolve<IAppConfigSettings>()).GetConfigRaw();
var webConfig = serviceConfig["Web"];
return new DictionaryAdapterFactory().GetAdapter<IMicrosoftStorageConfig>(webConfig);
}
示例9: Resolve
public override object Resolve(CreationContext context)
{
var store = GetStore();
var instance = base.Resolve(context);
if (instance == null)
{
if (context.Handler.ComponentModel.ExtendedProperties[Constants.REG_IS_INSTANCE_KEY] != null)
{
throw new DependencyResolutionException("Cannot find the instance in the context store.");
}
}
else if (store[Model.Name] == null)
{
store[Model.Name] = instance;
store.GetContextInstances().Add(new ContextStoreDependency(Model.Name, instance, this));
_isRegisteredForCleanup = true;
}
if (!_isRegisteredForCleanup)
{
store.GetContextInstances().Add(new ContextStoreDependency(Model.Name, instance, this));
_isRegisteredForCleanup = true;
}
return store[Model.Name];
}
示例10: Create
public override object Create(IKernel kernel, object instance, ComponentModel model,
CreationContext context, params object[] constructorArguments)
{
var channelHolder = instance as IWcfChannelHolder;
if (channelHolder == null)
{
throw new ArgumentException(string.Format("Given instance is not an {0}", typeof(IWcfChannelHolder)), "instance");
}
if (channelHolder.RealProxy == null)
{
return channelHolder.Channel;
}
if (model.Services.Count() > 1)
{
throw new ArgumentException(string.Format(
"Component {0}, which was designated as a WCF proxy exposes {1} services. The facility currently only supports single-service components.",
model.Name, model.Services.Count()));
}
var isDuplex = IsDuplex(channelHolder.RealProxy);
var proxyOptions = model.ObtainProxyOptions();
var serviceContract = model.GetServiceContract();
var generationOptions = CreateProxyGenerationOptions(serviceContract, proxyOptions, kernel, context);
var additionalInterfaces = GetInterfaces(model.Services, proxyOptions, isDuplex);
var interceptors = GetInterceptors(kernel, model, serviceContract, channelHolder, context);
return generator.CreateInterfaceProxyWithTarget(typeof(IWcfChannelHolder),
additionalInterfaces, channelHolder, generationOptions, interceptors);
}
示例11: Resolve
public override object Resolve(CreationContext context)
{
var current = HttpContext.Current;
if (current == null)
throw new InvalidOperationException(
"HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net");
if (current.Items[PerRequestObjectID] == null)
{
if (!PerWebRequestLifestyleModule.Initialized)
{
var message =
string.Format(
"Looks like you forgot to register the http module {0}{1}Add '<add name=\"PerRequestLifestyle\" type=\"Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor\" />' to the <httpModules> section on your web.config",
typeof(PerWebRequestLifestyleModule).FullName, Environment.NewLine);
throw new Exception(message);
}
var instance = base.Resolve(context);
current.Items[PerRequestObjectID] = instance;
PerWebRequestLifestyleModule.RegisterForEviction(this, instance);
}
return current.Items[PerRequestObjectID];
}
示例12: GetSubHandler
protected IHandler GetSubHandler(CreationContext context, Type genericType)
{
lock (type2SubHandler)
{
IHandler handler;
if (type2SubHandler.ContainsKey(genericType))
{
handler = type2SubHandler[genericType];
}
else
{
Type service = ComponentModel.Service.MakeGenericType(context.GenericArguments);
ComponentModel newModel = Kernel.ComponentModelBuilder.BuildModel(
ComponentModel.Name, service, genericType, null);
newModel.ExtendedProperties[ComponentModel.SkipRegistration] = true;
CloneParentProperties(newModel);
// Create the handler and add to type2SubHandler before we add to the kernel.
// Adding to the kernel could satisfy other dependencies and cause this method
// to be called again which would result in extra instances being created.
handler = Kernel.HandlerFactory.Create(newModel);
type2SubHandler[genericType] = handler;
Kernel.AddCustomComponent(newModel);
}
return handler;
}
}
示例13: Resolve
public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
string componentName = null;
if (!_knownDependencies.ContainsKey(dependency))
componentName = dependency.DependencyKey;
return _kernel.Resolve(componentName, dependency.TargetType);
}
示例14: CreateInstance
protected override object CreateInstance(CreationContext context, ConstructorCandidate constructor, object[] arguments)
{
var commandHandler = base.CreateInstance(context, constructor, arguments);
var result = chainBuilder.BuildChain(commandHandler);
return result;
}
示例15: CanResolve
/// <inheritdoc />
public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, Castle.Core.ComponentModel model, DependencyModel dependency)
{
if (dependency.TargetType == null)
{
return false;
}
Type genericArgument = null;
if ((dependency.TargetType.IsGenericType) && (dependency.TargetType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
genericArgument = dependency.TargetType.GetGenericArguments()[0];
}
else
{
dependency.TargetType.GetInterfaces().Any(implemented => (implemented.IsGenericType) && (implemented.GetGenericTypeDefinition() == typeof(IEnumerable<>)) &&
((genericArgument = implemented.GetGenericArguments()[0]) != null));
}
if ((genericArgument == null) || (!_kernel.HasComponent(genericArgument)))
{
return false;
}
return true;
}