I have a custom IAuthorizationPolicy which has a dependency on a repository
internal class CustomAuthorizationPolicy : IAuthorizationPolicy
{
private IBaseRepository _baseRepository;
public CustomAuthorizationPolicy(IBaseRepository baseRepository)
{
_baseRepository = baseRepository;
}
}
It is configured like this in web.config
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="CustomAuthorizationPolicy" />
</authorizationPolicies>
</serviceAuthorization>
This fails because WCF is not able to inject the required object when the policy is created. It expects a parameterless constructor.
I am using StructureMap and has a custom IInstanceProvider that handles all other dependencies in my application. But I can't get it to handle this situation.
Is this possible to do??
I ended up solving this with the use of a custom ServiceHost and a ServiceHostFactory. The factory sends the IoC container to the servicehost, which adds the new policy with a reference to the container. Now the policy can use the container to get the objects it needs.
public class CustomServiceHost : ServiceHost
{
public CustomServiceHost(IContainer container, Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
// Keep existing policies
var policies = new List<IAuthorizationPolicy>();
if (Authorization.ExternalAuthorizationPolicies != null)
{
policies.AddRange(Authorization.ExternalAuthorizationPolicies);
}
// Add new policy
policies.Add(new PasswordAuthorizationPolicy(container));
Authorization.ExternalAuthorizationPolicies = policies.AsReadOnly();
// Set correct mode
this.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With