Saeid Mirzaei Saeid Mirzaei - 1 month ago 15
C# Question

Setter Property Required primitive dependency is not explicitly defined

I have Asp.Net MVC Application and I have used structuremap,
I use custom ThrottleAttribute ActionFilter.
follow rate limiting in asp.net mvc

public enum TimeUnit
{
Minute = 60,
Hour = 3600,
Day = 86400
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class ThrottleAttribute : ActionFilterAttribute
{
public TimeUnit Time { get; set; }
public int Count { get; set; }

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var seconds = Convert.ToInt32(Time);

var key = string.Join(
"-",
seconds,
filterContext.HttpContext.Request.HttpMethod,
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
filterContext.ActionDescriptor.ActionName,
filterContext.HttpContext.Request.UserHostAddress
);

// increment the cache value
var cnt = 1;
if (HttpRuntime.Cache[key] != null)
{
cnt = (int)HttpRuntime.Cache[key] + 1;
}
HttpRuntime.Cache.Insert(
key,
cnt,
null,
DateTime.UtcNow.AddSeconds(seconds),
Cache.NoSlidingExpiration,
CacheItemPriority.Low,
null
);

if (cnt >= Count)
{

filterContext.Result = new ContentResult
{
Content = "You are allowed to make only " + Count + " requests per " + Time.ToString().ToLower()
};
filterContext.HttpContext.Response.StatusCode = 429;
}
}
}


after I use ThrottleAttribute throw Exception:


Setter Property 'Time' (ActionFilters.TimeUnit): Required primitive dependency is not explicitly defined


And my ActionFilter Registry:

public class ActionFilterRegisteryClass : StructureMap.Registry
{
public ActionFilterRegisteryClass(Func<StructureMap.IContainer> containerFactory)
{
For<IFilterProvider>().Use(
new StructurMapFilterProvider(containerFactory));

Policies.SetAllProperties(x =>
{
x.Matching(p =>
p.DeclaringType.CanBeCastTo(typeof(ActionFilterAttribute)) &&
(
p.DeclaringType.Namespace.StartsWith("MyNamespace")
) &&
!p.PropertyType.IsPrimitive &&
p.PropertyType != typeof(string)
);
});
}
}


Exception is fire in: container.BuildUp(filter.Instance);

public class StructurMapFilterProvider : FilterAttributeFilterProvider
{
private readonly Func<StructureMap.IContainer> _Container;

public StructurMapFilterProvider(Func<StructureMap.IContainer> container)
{
_Container = container;
}

public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext,
ActionDescriptor actionDescriptor)
{
var filters = base.GetFilters(controllerContext, actionDescriptor);

var container = _Container();

foreach (var filter in filters)
{
container.BuildUp(filter.Instance);

yield return filter;
}
}
}



Setter Property 'Time' (ActionFilters.TimeUnit): Required primitive dependency is not explicitly defined


What should I do?

How explicitly defined PrimitiveTypes?

Answer

The problem was in Registering ActionFilterClass in structuremap. I have changed it as follows:

public class ActionFilterRegisteryClass : StructureMap.Registry
{
    public ActionFilterRegisteryClass(Func<StructureMap.IContainer> containerFactory)
    {
        For<IFilterProvider>().Use(
            new StructurMapFilterProvider(containerFactory));

        Policies.SetAllProperties(x =>
        {
            x.Matching(p =>
                p.DeclaringType.CanBeCastTo(
                !p.PropertyType.IsPrimitive &&
                !p.PropertyType.IsEnum &&
                p.PropertyType != typeof(string));
        });
    }
}