Hi,
We are using Microsoft Unity application block for DI and logging. We're planning to upgrade to Unity package (5.11.1). We're investigation as to what it takes to get this working. So we removed Microsoft.Practices.Unity.dll, Microsoft.Practices.Unity.Configuration.dll, Microsoft.Practices.Unity.Interception.dll and added Unity.Abstractions.dll, Unity.Configuration.dll, Unity.Container.dll, Unity.Interception.dll. We updated config file in unity section to point to Unit.Container. Somehow, we got the unity working. However, we encountered issues while logging using unity container.
`public class LogMethodRegistration : UnityContainerExtension
{
///
/// Initializes this instance.
///
protected override void Initialize()
{
this.Container.AddNewExtension();
this.Context.Registering += new EventHandler(this.OnRegister);
}
/// <summary>
/// Called when [register].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RegisterEventArgs"/> instance containing the event data.</param>
private void OnRegister(object sender, RegisterEventArgs e)
{
System.Diagnostics.Debug.Assert(false, "OnRegister");
if (e != null && e.TypeFrom != null && e.TypeFrom.IsInterface)
{
/*var interfaceInterceptor = new Interceptor<InterfaceInterceptor>();
interfaceInterceptor.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, Context.Policies);
var interceptionBehavior = new InterceptionBehavior<LogMethodInterceptionBehavior>();
interceptionBehavior.AddPolicies(e.TypeFrom, e.TypeTo, e.Name, Context.Policies);*/
}
}
}
[SecurityCritical]
public class LogMethodInterceptionBehavior : IInterceptionBehavior
{
///
/// Gets a value indicating whether [will execute].
///
///
/// true if [will execute]; otherwise, false.
///
public bool WillExecute
{
[SecurityCritical]
get { return true; }
}
/// <summary>
/// Invokes the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="getNext">The get next.</param>
/// <returns>Object of type IMethodReturn</returns>
[SecurityCritical]
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
ArgumentValidator.ValidateForNotNull("getNext", getNext);
// Log method invoked, by default we don't log method inputs
bool logMethodInputs = false;
this.LogMethodInvoked(input, logMethodInputs);
// Invoke the next behavior in the chain.
IMethodReturn methodReturn = getNext()(input, getNext);
// Log method completed, by default we don't log method outputs
bool logMethodOutputs = false;
this.LogMethodCompleted(input, methodReturn, logMethodOutputs);
return methodReturn;
}
/// <summary>
/// Gets the required interfaces.
/// </summary>
/// <returns>Object of type IEnumerable of Type</returns>
[SecurityCritical]
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
/// <summary>
/// Logs the method invoked.
/// </summary>
/// <param name="methodInvocation">The method invocation.</param>
/// <param name="logMethodInputs">if set to <c>true</c> [log method inputs].</param>
protected void LogMethodInvoked(IMethodInvocation methodInvocation, bool logMethodInputs)
{
ArgumentValidator.ValidateForNotNull("methodInvocation", methodInvocation);
// Before invoking the method on the original target.
Logger.Log(
LogMessage.InvokedMethod,
LogCategory.Information,
methodInvocation.MethodBase.DeclaringType,
methodInvocation.MethodBase.Name,
methodInvocation.MethodBase);
if (logMethodInputs)
{
if (methodInvocation.Arguments.Count > 0)
{
bool logAdditionalInformation = false;
Logger.Log(LogMessage.MethodInputParameters, LogCategory.Information, logAdditionalInformation);
for (int i = 0; i < methodInvocation.Arguments.Count; i++)
{
Logger.Log(
LogMessage.ParameterNameAndValue,
LogCategory.Information,
logAdditionalInformation,
methodInvocation.Arguments.ParameterName(i),
methodInvocation.Arguments[i]);
}
}
}
}
/// <summary>
/// Logs the method completed.
/// </summary>
/// <param name="methodInvocation">The method invocation.</param>
/// <param name="methodReturn">The method return.</param>
/// <param name="logMethodOutputs">if set to <c>true</c> [log method outputs].</param>
protected void LogMethodCompleted(
IMethodInvocation methodInvocation,
IMethodReturn methodReturn,
bool logMethodOutputs)
{
ArgumentValidator.ValidateForNotNull("methodInvocation", methodInvocation);
ArgumentValidator.ValidateForNotNull("methodReturn", methodReturn);
// After invoking the method on the original target, log the method completion
if (methodReturn.Exception != null)
{
// Log method exception
Logger.Log(
LogMessage.CompletedMethodWithException,
LogCategory.Information,
methodInvocation.MethodBase.DeclaringType,
methodInvocation.MethodBase.Name,
methodInvocation.MethodBase,
methodReturn.Exception);
// Log any inner exception
if (methodReturn.Exception.InnerException != null)
{
bool logAdditionalInformation = false;
Logger.Log(
LogMessage.InnerException,
LogCategory.Information,
logAdditionalInformation,
methodReturn.Exception.InnerException);
}
}
else
{
// Log method method completed
Logger.Log(
LogMessage.CompletedMethod,
LogCategory.Information,
methodInvocation.MethodBase.DeclaringType,
methodInvocation.MethodBase.Name,
methodInvocation.MethodBase);
// Log method outputs if required
if (logMethodOutputs)
{
if (methodReturn.Outputs.Count > 0)
{
bool logAdditionalInformation = false;
Logger.Log(LogMessage.MethodOutputParameters, LogCategory.Information, logAdditionalInformation);
for (int i = 0; i < methodReturn.Outputs.Count; i++)
{
Logger.Log(
LogMessage.ParameterNameAndValue,
LogCategory.Information,
logAdditionalInformation,
methodReturn.Outputs.ParameterName(i),
methodReturn.Outputs[i]);
}
}
if (methodReturn.ReturnValue != null)
{
bool logAdditionalInformation = false;
Logger.Log(
LogMessage.MethodOutput,
LogCategory.Information,
logAdditionalInformation,
methodReturn.ReturnValue);
}
}
}
}
}
`
When launching app, it threw at this.Container.AddNewExtension() The error is Attempt by method 'LogMethodRegistration.Initialize()' to access method 'Unity.ExtensionExtensions.AddNewExtension(Unity.IUnityContainer)' failed
We don't know what the error means and don't know its usage either. There is no documentation about Unity as a whole.
Could you assist us in resolving this issue? Also, could point any documentation for all Unity packages for reference?
Hi, We are using Microsoft Unity application block for DI and logging. We're planning to upgrade to Unity package (5.11.1). We're investigation as to what it takes to get this working. So we removed Microsoft.Practices.Unity.dll, Microsoft.Practices.Unity.Configuration.dll, Microsoft.Practices.Unity.Interception.dll and added Unity.Abstractions.dll, Unity.Configuration.dll, Unity.Container.dll, Unity.Interception.dll. We updated config file in unity section to point to Unit.Container. Somehow, we got the unity working. However, we encountered issues while logging using unity container.
`public class LogMethodRegistration : UnityContainerExtension { ///
/// Initializes this instance.
///
protected override void Initialize()
{
this.Container.AddNewExtension();
this.Context.Registering += new EventHandler(this.OnRegister);
}
[SecurityCritical] public class LogMethodInterceptionBehavior : IInterceptionBehavior { ///
/// Gets a value indicating whether [will execute].
///
///
/// true if [will execute]; otherwise, false .
///
public bool WillExecute
{
[SecurityCritical]
get { return true; }
}
`
When launching app, it threw at this.Container.AddNewExtension() The error is Attempt by method 'LogMethodRegistration.Initialize()' to access method 'Unity.ExtensionExtensions.AddNewExtension(Unity.IUnityContainer)' failed
We don't know what the error means and don't know its usage either. There is no documentation about Unity as a whole.
Could you assist us in resolving this issue? Also, could point any documentation for all Unity packages for reference?
Thanks, Amit