本文整理汇总了C#中IKernel.GetHandler方法的典型用法代码示例。如果您正苦于以下问题:C# IKernel.GetHandler方法的具体用法?C# IKernel.GetHandler怎么用?C# IKernel.GetHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IKernel
的用法示例。
在下文中一共展示了IKernel.GetHandler方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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 (InterceptorReference interceptorRef in GetInterceptorsFor(model))
{
IHandler handler;
if (interceptorRef.ReferenceType == InterceptorReferenceType.Interface)
{
handler = kernel.GetHandler(interceptorRef.ServiceType);
}
else
{
handler = kernel.GetHandler(interceptorRef.ComponentKey);
}
if (handler == null)
{
// This shoul be virtually impossible to happen
// Seriously!
throw new Exception("The interceptor could not be resolved");
}
if(handler.IsBeingResolvedInContext(context))
{
throw new DependencyResolverException(
string.Format(
"Cycle detected - interceptor {0} wants to use itself as its interceptor. This usually signifies a bug in custom {1}",
handler.ComponentModel.Name, typeof(IModelInterceptorsSelector).Name));
}
try
{
var contextForInterceptor = RebuildContext(handler.Service, context);
var interceptor = (IInterceptor)handler.Resolve(contextForInterceptor);
interceptors.Add(interceptor);
SetOnBehalfAware(interceptor as IOnBehalfAware, model);
}
catch (InvalidCastException)
{
var message = String.Format(
"An interceptor registered for {0} doesn't implement the IInterceptor interface",
model.Name);
throw new Exception(message);
}
}
return interceptors.ToArray();
}
示例2: ObtainInterceptors
/// <summary>
/// Obtains the interceptors associated with the component.
/// </summary>
/// <param name="kernel">The kernel instance</param>
/// <param name="model">The component model</param>
/// <returns>interceptors array</returns>
protected IInterceptor[] ObtainInterceptors(IKernel kernel, ComponentModel model)
{
IInterceptor[] interceptors = new IInterceptor[model.Interceptors.Count];
int index = 0;
foreach(InterceptorReference interceptorRef in model.Interceptors)
{
IHandler handler;
if (interceptorRef.ReferenceType == InterceptorReferenceType.Interface)
{
handler = kernel.GetHandler(interceptorRef.ServiceType);
}
else
{
handler = kernel.GetHandler(interceptorRef.ComponentKey);
}
if (handler == null)
{
// This shoul be virtually impossible to happen
// Seriously!
throw new ApplicationException("The interceptor could not be resolved");
}
try
{
IInterceptor interceptor = (IInterceptor) handler.Resolve(CreationContext.Empty);
interceptors[index++] = interceptor;
SetOnBehalfAware(interceptor as IOnBehalfAware, model);
}
catch(InvalidCastException)
{
String message = String.Format(
"An interceptor registered for {0} doesnt implement " +
"the IMethodInterceptor interface",
model.Name);
throw new ApplicationException(message);
}
}
return interceptors;
}
示例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)
{
List<IInterceptor> interceptors = new List<IInterceptor>();
foreach(InterceptorReference interceptorRef in GetInterceptorsFor(model))
{
IHandler handler;
if (interceptorRef.ReferenceType == InterceptorReferenceType.Interface)
{
handler = kernel.GetHandler(interceptorRef.ServiceType);
}
else
{
handler = kernel.GetHandler(interceptorRef.ComponentKey);
}
if (handler == null)
{
// This shoul be virtually impossible to happen
// Seriously!
throw new Exception("The interceptor could not be resolved");
}
try
{
IInterceptor interceptor = (IInterceptor) handler.Resolve(context);
interceptors.Add(interceptor);
SetOnBehalfAware(interceptor as IOnBehalfAware, model);
}
catch(InvalidCastException)
{
String message = String.Format(
"An interceptor registered for {0} doesnt implement the IInterceptor interface",
model.Name);
throw new Exception(message);
}
}
return interceptors.ToArray();
}
示例4: InspectServiceDependency
private void InspectServiceDependency(IHandler inspectingHandler, DependencyModel dependency, IKernel kernel)
{
var type = dependency.TargetItemType;
var handler = kernel.GetHandler(type);
if (handler == null)
{
message.AppendFormat("- Service '{0}' which was not registered.", type.FullName ?? type.Name);
message.AppendLine();
}
else if (handler == inspectingHandler)
{
var alternatives = kernel.GetHandlers(type);
message.AppendFormat("- Service '{0}' which points back to the component itself.", type.FullName ?? type.Name);
message.AppendLine();
message.Append("A dependency cannot be satisfied by the component itself, did you forget to ");
if (alternatives.Length == 1)
{
message.AppendLine("register other components for this service?");
}
else
{
message.AppendLine("make this a service override and point explicitly to a different component exposing this service?");
message.AppendLine();
message.Append("The following components also expose the service, but none of them can be resolved:");
foreach (var maybeDecoratedHandler in alternatives.Where(maybeDecoratedHandler => maybeDecoratedHandler != inspectingHandler))
{
var info = maybeDecoratedHandler as IExposeDependencyInfo;
if (info != null)
{
info.ObtainDependencyDetails(this);
}
else
{
message.AppendLine();
message.AppendFormat("'{0}' is registered and is matching the required service, but cannot be resolved.",
maybeDecoratedHandler.ComponentModel.Name);
}
}
}
}
else
{
message.AppendFormat("- Service '{0}' which was registered but is also waiting for dependencies.", handler.ComponentModel.Name);
var info = handler as IExposeDependencyInfo;
if (info != null)
{
info.ObtainDependencyDetails(this);
}
}
}
示例5: InspectServiceOverrideDependency
private void InspectServiceOverrideDependency(DependencyModel dependency, IKernel kernel)
{
var referenceName = dependency.ReferencedComponentName;
var handler = kernel.GetHandler(referenceName);
//TODO: what about self dependency?
if (handler == null)
{
message.AppendFormat(
"- Component '{0}' (via override) which was not found. Did you forget to register it or misspelled the name? If the component is registered and override is via type make sure it doesn't have non-default name assigned explicitly or override the dependency via name.",
referenceName);
message.AppendLine();
}
else
{
message.AppendFormat("- Component '{0}' (via override) which was registered but is also waiting for dependencies.", referenceName);
message.AppendLine();
var info = handler as IExposeDependencyInfo;
if (info != null)
{
info.ObtainDependencyDetails(this);
}
}
}
示例6: GetInjectableProperties
private static TypePropertyDescriptor GetInjectableProperties(Type type, IKernel kernel,
Action<PropertyInfo, ComponentModel> validatePropertyRegistration)
{
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
if (properties.Length == 0)
return null;
var injectableProperties = new List<PropertyInfo>(properties.Length);
foreach (PropertyInfo prop in properties)
{
if (!prop.CanWrite || prop.PropertyType.IsValueType)
continue;
IHandler registration = kernel.GetHandler(prop.PropertyType);
if (registration != null)
{
validatePropertyRegistration?.Invoke(prop, registration.ComponentModel);
injectableProperties.Add(prop);
}
}
return new TypePropertyDescriptor(type,
injectableProperties.Count > 0 ? injectableProperties.ToArray() : null);
}
示例7: InspectServiceOverrideDependency
private void InspectServiceOverrideDependency(DependencyModel dependency, IKernel kernel)
{
var key = dependency.DependencyKey;
var handler = kernel.GetHandler(key);
//TODO: what about self dependency?
if (handler == null)
{
message.AppendFormat("- Service '{0}' (via override) which was not registered. Did you misspell the name?", key);
message.AppendLine();
}
else
{
message.AppendFormat("- Service '{0}' (via override) which was registered but is also waiting for dependencies.", key);
message.AppendLine();
var info = handler as IExposeDependencyInfo;
if (info != null)
{
info.ObtainDependencyDetails(this);
}
}
}
示例8: GetHandler
/// <summary>
/// Gets handler for the dependency, or null if dependency is not satisfied by a component from a container or a handler was not found
/// </summary>
/// <param name = "kernel"></param>
/// <returns></returns>
public IHandler GetHandler(IKernel kernel)
{
#if DEBUG
if (!initialized)
{
throw new InvalidOperationException("Not initialized!");
}
#endif
if (reference != null)
{
return kernel.GetHandler(reference);
}
if (targetItemType != null)
{
return kernel.GetHandler(targetItemType);
}
return null;
}
示例9: GetInterceptorHandler
private IHandler GetInterceptorHandler(InterceptorReference interceptorRef, IKernel kernel)
{
IHandler handler;
if (interceptorRef.ReferenceType == InterceptorReferenceType.Interface)
{
handler = kernel.GetHandler(interceptorRef.ServiceType);
}
else
{
handler = kernel.GetHandler(interceptorRef.ComponentKey);
}
return handler;
}