I don't know if anybody else has hit on this but it seems to me like
some documentation somewhere needs to be made better...
I was trying to use this interceptor I wrote [1] in a testing website we
are building. The interceptor is used on a half dozen types and we
needed to know what service was being intercepted. Unfortunately the
code in [1] doesn't seem to do this. Instead it always has the same
value in the name field (which is the value of whatever service was
instantiated last I think). Changing the code to [2] made it work the
way I want. Am I doing this right?
1:
public class StubInterceptor:IInterceptor, IOnBehalfAware{
string name;
public void Intercept(IInvocation invocation) {
var method = invocation.GetConcreteMethod();
var parameters = method.GetParameters();
var parameterdictionary = new Dictionary<string, object>();
for (int i = 0; i < invocation.Arguments.Length; i++) {
parameterdictionary[parameters[i].Name] =
invocation.Arguments[i];
}
invocation.Proceed();
var cp = new CallProperties() {
HasReturnValue = method.ReturnType != typeof (void),
MethodName = method.Name,
ServiceName = name,
Parameters = parameterdictionary
};
if (cp.HasReturnValue) {
cp.ReturnValue = invocation.ReturnValue;
}
Service.Locate<ICallLogger>().LogCall(cp);
}
public void SetInterceptedComponentModel(ComponentModel target) {
name = target.Service.Name;
}
}
2:
public class StubInterceptor : IInterceptor, IOnBehalfAware {
readonly object lockobj = new object();
readonly Dictionary<string, string> names = new
Dictionary<string, string>();
public void Intercept(IInvocation invocation) {
var method = invocation.GetConcreteMethod();
var parameters = method.GetParameters();
var parameterdictionary = new Dictionary<string, object>();
for (int i = 0; i < invocation.Arguments.Length; i++) {
parameterdictionary[parameters[i].Name] =
invocation.Arguments[i];
}
invocation.Proceed();
CallProperties cp;
lock (lockobj) {
cp = new CallProperties() {
HasReturnValue = method.ReturnType != typeof(void),
MethodName = method.Name,
ServiceName = names[invocation.TargetType.Name],
Parameters = parameterdictionary
};
}
if (cp.HasReturnValue) {
cp.ReturnValue = invocation.ReturnValue;
}
Service.Locate<ICallLogger>().LogCall(cp);
}
public void SetInterceptedComponentModel(ComponentModel target) {
lock (lockobj) {
names[target.Implementation.Name] = target.Service.Name;
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---