How can I get constructor parameters of autofac component on activating - autofac

What I want is create caching proxies for all my components decorated with some attributes. So, I made Autofac Module like this:
public class CachingModule : Autofac.Module
{
private readonly ProxyGenerator generator;
public CachingModule()
{
generator = new ProxyGenerator();
}
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
{
var type = registration.Activator.LimitType;
if (type.GetCustomAttribute<CachedAttribute>(true) != null
|| type.GetMethods().Any(m => m.GetCustomAttribute<CachedAttribute>(true) != null))
{
registration.Activating += (s, e) =>
{
var proxy = generator.CreateClassProxyWithTarget(e.Instance.GetType(),
e.Instance,
interceptors: e.Context.Resolve<IEnumerable<CacheInterceptor>>().ToArray());
e.ReplaceInstance(proxy);
};
}
}
}
What I can't get to work is: I can't create proxy instances with parametrized constructors, is there any way to do this?

Ok, I've managed to get it all working, so for anyone interested here is a sample of proxy generation with non-default constructors
public class CachingModule : Autofac.Module
{
private readonly ProxyGenerator generator;
public CachingModule()
{
generator = new ProxyGenerator();
}
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
{
var type = registration.Activator.LimitType;
if (type.GetCustomAttribute<CachedAttribute>(true) != null
|| type.GetMethods().Any(m => m.GetCustomAttribute<CachedAttribute>(true) != null))
{
var ctors = type.GetConstructors();
registration.Activating += (s, e) =>
{
if (e.Instance is IProxyTargetAccessor)
{
return;
}
var param = new List<object>();
var infos = ctors.First().GetParameters();
if (ctors.Length > 0 && infos.Length > 0)
{
foreach (var p in e.Parameters)
{
foreach (var info in infos)
{
if (p.CanSupplyValue(info, e.Context, out var valueProvider))
{
param.Add(valueProvider());
}
}
}
}
var instance = e.Instance;
var toProxy = instance.GetType();
var proxyGenerationOptions = new ProxyGenerationOptions(new CacheProxyGenerationHook());
var proxy = generator.CreateClassProxyWithTarget(toProxy,
instance,
proxyGenerationOptions,
param.ToArray(),
interceptors: e.Context.Resolve<IEnumerable<CacheInterceptor>>().ToArray());
e.ReplaceInstance(proxy);
};
}
}
}

Related

masstransit - consumers are no registered and activated

I'm trying to register consumers but no success using mass transit.
I registered MT using Autofac using module approach.
Firstly - I created some simple message:
public class SimpleMessage
{
public string msg { get; set; }
}
and I've managed to send them into queue:
var endpointTest = await _busControl.GetSendEndpoint(new Uri("queue:queueTest"));
await endpointTest.Send(new SimpleMessage
{
msg = "test"
});
Then I created a consumer:
public class SimpleMessageConsumer : IConsumer<SimpleMessage>
{
private readonly ILogger _logger;
public SimpleMessageConsumer(ILogger logger)
{
_logger = logger;
}
public async Task Consume(ConsumeContext<SimpleMessage> context)
{
_logger.Info($"got msg from queue: {context.Message}");
}
}
But it won't run when the message appeared in the queue. My configuration is:
public class BusModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<BusSettings>().As<IBusSettings>();
builder.AddMassTransit(cfg =>
{
cfg.AddConsumer<SimpleMessageConsumer, SimpleMessageConsumerDefinition>();
cfg.Builder.Register(context =>
{
var busSettings = context.Resolve<IBusSettings>();
var logger = context.Resolve < ILogger >();
var busControl = Bus.Factory.CreateUsingRabbitMq(bus =>
{
bus.AutoDelete = busSettings.AutoDelete;
bus.Durable = busSettings.Durable;
bus.Exclusive = busSettings.Exclusive;
bus.ExchangeType = busSettings.Type;
//bus.UseNServiceBusJsonSerializer();
bus.Host(busSettings.HostAddress, busSettings.Port, busSettings.VirtualHost, null, h =>
{
h.Username(busSettings.Username);
h.Password(busSettings.Password);
});
bus.ReceiveEndpoint("queueTest", ec =>
{
ec.Consumer(() => new SimpleMessageConsumer(logger));
});
});
return busControl;
}).SingleInstance().As<IBusControl>().As<IBus>();
});
}
}
in program.cs
I have:
services.AddMassTransitHostedService();
and
containerBuilder.RegisterModule<BusModule>();
Such I mentioned - sending a msg to queue works but consumer wasn't running.
Can you help me what did I do wrong? how should I fix the configuration? in order to activate the consumer?
I've updated your configuration to work properly, using the actual bus configuration methods instead of mixing the two solutions:
public class BusModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<BusSettings>().As<IBusSettings>();
builder.AddMassTransit(cfg =>
{
cfg.AddConsumer<SimpleMessageConsumer, SimpleMessageConsumerDefinition>();
cfg.UsingRabbitMq((context,cfg) =>
{
var busSettings = context.GetRequiredService<IBusSettings>();
var logger = context.GetRequiredService<ILogger>();
//bus.UseNServiceBusJsonSerializer();
bus.Host(busSettings.HostAddress, busSettings.Port, busSettings.VirtualHost, null, h =>
{
h.Username(busSettings.Username);
h.Password(busSettings.Password);
});
bus.ReceiveEndpoint("queueTest", ec =>
{
// i'm guessing these apply to the receive endpoint, not the bus endpoint
ec.AutoDelete = busSettings.AutoDelete;
ec.Durable = busSettings.Durable;
ec.Exclusive = busSettings.Exclusive;
ec.ExchangeType = busSettings.Type;
ec.ConfigureConsumer<SimpleMessageConsumer>(context);
});
});
});
}
}

Preventing default values being used for keys in Entity Framework Core?

I would like to prevent any default values for a type being used for keys in Entity Framework core. So for example, 00000000-0000-0000-0000-000000000000 for Guids, 0 for ints, etc
Using this helper class
static class KeyValidator
{
public static void ValidateKeys(this DbContext context)
{
foreach (var entity in context.AddedOrModified())
{
foreach (var key in entity.Metadata.GetKeys())
{
foreach (var property in key.Properties)
{
var propertyEntry = entity.Property(property.Name);
if (!IsDefaultValue(property.ClrType, propertyEntry.CurrentValue))
{
continue;
}
throw new Exception($#"Invalid empty key.
EntityType: {entity.Metadata.ClrType.FullName}
PropertyName: {property.Name}
PropertyType: {property.ClrType.FullName}.");
}
}
}
}
static bool IsDefaultValue(Type clrType, object currentValue)
{
if (clrType.IsValueType)
{
var instance = Activator.CreateInstance(clrType);
return instance.Equals(currentValue);
}
return currentValue == null;
}
static IEnumerable<EntityEntry> AddedOrModified(this DbContext context)
{
return context.ChangeTracker.Entries()
.Where(e => e.State == EntityState.Added ||
e.State == EntityState.Modified);
}
}
The in the DbContext include
public override int SaveChanges()
{
this.ValidateKeys();
return base.SaveChanges();
}
public override Task<int> SaveChangesAsync(bool acceptAllChanges, CancellationToken cancellation = default)
{
this.ValidateKeys();
return base.SaveChangesAsync(acceptAllChanges, cancellation);
}

MEF and WEB API 2.2

I am trying to inject dependencies into a Web Api Controller.
I created an own IHttpControllerActivator class and replaced the default one in lobalConfiguration.
public class SimpleASPWebAPIContainer : IHttpControllerActivator
{
private readonly CompositionContainer container;
public SimpleASPWebAPIContainer(CompositionContainer compositionContainer)
{
container = compositionContainer;
}
public IHttpController Create(System.Net.Http.HttpRequestMessage request, System.Web.Http.Controllers.HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
if (controllerType != null)
{
var export = container.GetExports(controllerType, null, null).FirstOrDefault();
IHttpController result = null;
if (null != export)
{
result = export.Value as IHttpController;
}
else
{
//result = base.GetControllerInstance(requestContext, controllerType);
//container.ComposeParts(result);
}
return result;
}
else
{
return null;
}
}
public void Dispose()
{
if (container != null)
container.Dispose();
}
}
var apiSimpleContainer = new SimpleASPWebAPIContainer(container);
System.Web.Http.GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), apiSimpleContainer);
But when the client app is calling a controller method the IHttpControllerActivation Create method is not invoked.
Anybody can help me?
It was a very silly mistake.
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
MefConfig.RegisterMef(config);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
AutoMapperConfig.InitAutoMapper();
}
I should have to used the new HttoConfiguration instance to replace default IHttpControllerActivator instead of System.Web.Http.GlobalConfiguration.Configuration.

Updating a UserProfile and its Roles using Entity Framework

I'm trying to find a way that would allow me to update a UserProfile entity along with a list of Roles that the user is assigned to. I've written the code below, but it doesn't work.
public void UpdateUserProfile(UserProfile userProfile)
{
context.Entry(userProfile).State = EntityState.Added;
var databaseRoleIds = context.Roles
.Where(r => r.UserProfiles
.Any(u => u.UserId == userProfile.UserId))
.Select(r => r.RoleId).ToList();
var clientRoleIds = userProfile.Roles.Select(r => r.RoleId).ToList();
var removedRoleIds = databaseRoleIds.Except(clientRoleIds).ToList();
var addedRoleIds = clientRoleIds.Except(databaseRoleIds).ToList();
var unchangedRoleIds = removedRoleIds.Union(addedRoleIds).ToList();
foreach (var roleId in unchangedRoleIds)
{
var role = context.Roles.Find(roleId);
context.Entry(role).State = EntityState.Unchanged;
}
foreach (var roleId in removedRoleIds)
{
userProfile.RemoveRole(context.Roles.Find(roleId));
}
foreach (var roleId in addedRoleIds)
{
userProfile.AddRole(context.Roles.Find(roleId));
}
context.Entry(userProfile).State = EntityState.Modified;
}
Here is the unitOfWork
namespace MvcWebsite.WorkUnits
{
public class WorkUnit : IWorkUnit, IDisposable
{
private MvcContext context = new MvcContext();
private RoleRepository roleRepository;
private UserProfileRepository userProfileRepository;
public IRoleRepository RoleRepository
{
get
{
if (this.roleRepository == null)
{
roleRepository = new RoleRepository(context);
}
return roleRepository;
}
}
public IUserProfileRepository UserProfileRepository
{
get
{
if (this.userProfileRepository == null)
{
userProfileRepository = new UserProfileRepository(context);
}
return userProfileRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
... and here is the HttpPost Edit method
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(UserProfileEditViewModel model)
{
try
{
if (ModelState.IsValid)
{
var clientUserProfile = new UserProfile();
clientUserProfile.UserId = model.UserId;
clientUserProfile.UserName = model.UserName;
clientUserProfile.FirstName = model.FirstName;
clientUserProfile.LastName = model.LastName;
clientUserProfile.Email = model.Email;
clientUserProfile.RowVersion = model.RowVersion;
clientUserProfile.Roles = new List<Role>();
foreach(var role in model.Roles)
{
if (role.Assigned)
{
clientUserProfile.Roles.Add(new Role
{
RoleId = role.RoleId,
RoleName = role.RoleName,
RowVersion = role.RowVersion,
});
}
}
unitOfWork.UserProfileRepository.UpdateUserProfile(clientUserProfile);
unitOfWork.Save();
return RedirectToAction("Details", new { id = clientUserProfile.UserId });
}
}
catch (DataException ex)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
return View(model);
}
Does anyone have any idea why this isn't working? Or could suggest a tutorial somewhere that actually works. Any help, as always, is greatly appreciated.
Instead of creating a new UserProfile in your controller, get the UserProfile from the repository, modify its fields, then send it back to UpdateUserProfile and call Save.
Finally found that I had it totally wrong in the first place. I wasn't changing the relationships at all. I've included the code below, which allows me to attach the parent entity as modified and then mark the relationships as added and deleted as required
public void UpdateUserProfile(UserProfile userProfile)
{
context.Entry(userProfile).State = EntityState.Modified;
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
foreach (var role in userProfile.Roles)
{
context.Entry(role).State = EntityState.Unchanged;
}
var databaseRoleIds = context.Roles
.Where(r => r.UserProfiles
.Any(u => u.UserId == userProfile.UserId))
.Select(r => r.RoleId)
.ToList();
var clientRoleIds = userProfile.Roles.Select(r => r.RoleId).ToList();
var removedRoleIds = databaseRoleIds.Except(clientRoleIds).ToList();
var addedRoleIds = clientRoleIds.Except(databaseRoleIds).ToList();
foreach (var roleId in removedRoleIds)
{
var role = context.Roles.Find(roleId);
objectContext
.ObjectStateManager
.ChangeRelationshipState(userProfile, role, u => u.Roles, EntityState.Deleted);
}
foreach (var roleId in addedRoleIds)
{
var role = context.Roles.Find(roleId);
objectContext
.ObjectStateManager
.ChangeRelationshipState(userProfile, role, u => u.Roles, EntityState.Added);
}
}

From AutoMapper to Emit Mapper

I've recently discovered AutoMapper for bridging ViewModels and my actual DB objects. I use it in the way decribed here: http://automapper.codeplex.com/wikipage?title=Projection&referringTitle=Home
I've discovered Emit Mapper to :), but I can't find anytning similar to (where I can specify custom projecting rules):
.ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.EventDate.Date))
Thanks in advance!
For the Record this is the best solution that I came across on how to do it:
http://emitmapper.codeplex.com/discussions/259655
Check the solution on the last post. It works really well.
Update: The code for future reference:
public class ExtDefaultMapConfig<TSrc, TDst> : DefaultMapConfig
{
private readonly Dictionary<string, Func<TSrc, object>> _properties = new Dictionary<string, Func<TSrc, object>>();
public ExtDefaultMapConfig<TSrc, TDst> ForMember(string property, Func<TSrc, object> func)
{
if (!_properties.ContainsKey(property))
_properties.Add(property, func);
return this;
}
public ExtDefaultMapConfig<TSrc, TDst> ForMember(Expression<Func<TDst, object>> dstMember, Func<TSrc, object> func)
{
var prop = ReflectionHelper.FindProperty(dstMember);
return ForMember(prop.Name, func);
}
public ExtDefaultMapConfig<TSrc, TDst> Ignore(Expression<Func<TDst, object>> dstMember)
{
var prop = ReflectionHelper.FindProperty(dstMember);
IgnoreMembers<TSrc, TDst>(new[] { prop.Name });
return this;
}
public override IMappingOperation[] GetMappingOperations(Type from, Type to)
{
var list = new List<IMappingOperation>();
list.AddRange(base.GetMappingOperations(from, to));
list.AddRange(
FilterOperations(
from,
to,
ReflectionUtils.GetPublicFieldsAndProperties(to)
.Where(f => _properties.ContainsKey(f.Name))
.Select(
m =>
(IMappingOperation)new DestWriteOperation
{
Destination = new MemberDescriptor(m),
Getter =
(ValueGetter<object>)
(
(value, state) =>
{
Debug.WriteLine(string.Format("Mapper: getting value of field or property {0}", m.Name));
return ValueToWrite<object>.ReturnValue(_properties[m.Name]((TSrc) value));
}
)
}
)
)
);
return list.ToArray();
}
}
class ReflectionHelper
{
public static MemberInfo FindProperty(LambdaExpression lambdaExpression)
{
Expression expression = lambdaExpression;
bool flag = false;
while (!flag)
{
switch (expression.NodeType)
{
case ExpressionType.Convert:
expression = ((UnaryExpression)expression).Operand;
break;
case ExpressionType.Lambda:
expression = ((LambdaExpression)expression).Body;
break;
case ExpressionType.MemberAccess:
MemberExpression memberExpression = (MemberExpression)expression;
if (memberExpression.Expression.NodeType != ExpressionType.Parameter && memberExpression.Expression.NodeType != ExpressionType.Convert)
throw new ArgumentException(string.Format("Expression '{0}' must resolve to top-level member.", lambdaExpression), "lambdaExpression");
return memberExpression.Member;
default:
flag = true;
break;
}
}
return null;
}
public static object GetValue(string property, object obj)
{
PropertyInfo pi = obj.GetType().GetProperty(property);
return pi.GetValue(obj, null);
}
}