none
Set WCF ServiceAuthorizationManager in custom behavior RRS feed

  • Question

  • We have a BizTalk application that uses a WCF-WebHttp adapter.

    We need to use a custom ServiceAuthorizationManager that requires dependency injection and has some configurable parameters.

    To do this, we're using a custom behavior to set the custom authorization manager:

    internal class CustomAuthorizationServiceBehavior : IServiceBehavior
    {  
        CustomAuthorizationConfiguration configuration;
    
        public CustomAuthorizationServiceBehavior(CustomAuthorizationConfiguration configuration)
        {
            this.configuration = configuration;
        }
    
        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {            
            var repository = ... //should be injected
            serviceHostBase.Authorization.ServiceAuthorizationManager = new CustomAuthorizationManager(repository, configuration);
        }
    
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }
    
        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }
    }

    Questions:

    1. How to properly do dependency injection?

    2. Is it ok to set the custom authorization manager in the AddBindingParameters method?

    It is currently working, but I'm not sure it conforms to the way the method was intended to be used. On the other hand, if we set the authorization manager in the ApplyDispatchBehavior method, then I believe we have to explicitly call serviceHostBase.Authorization.ApplyDispatchBehavior, to make sure that the authorization manager is applied to the DispatchRuntime of all the endpoints. So ApplyDispatchBehavior would be called twice. Either way seems like a hack. What would be the recommended method according to WCF principles?


    Wednesday, June 6, 2018 10:58 AM

All replies