/// <summary> /// Adds the interception policy. /// </summary> /// <param name="builder">The <see cref="InterceptionBuilder"/> to perform interception service registration.</param> /// <param name="configure">The <see cref="Action{IInterceptionPolicyBuilder}"/> used to build interception policy.</param> /// <returns>The current <see cref="InterceptionBuilder"/>. </returns> public static InterceptionBuilder AddPolicy(this InterceptionBuilder builder, Action <IInterceptionPolicyBuilder> configure) { Guard.ArgumentNotNull(builder, nameof(builder)); Guard.ArgumentNotNull(configure, nameof(configure)); var serviceProvider = builder.Services.BuildServiceProvider(); var registrationBuilder = new InterceptionPolicyBuilder(serviceProvider); configure.Invoke(registrationBuilder); var resolver = new PolicyInterceptorProviderResolver(registrationBuilder.Build()); builder.InterceptorProviderResolvers.Add(nameof(PolicyInterceptorProviderResolver), resolver); return(builder); }
/// <summary> /// Adds the policy. /// </summary> /// <param name="builder">The <see cref="InterceptionBuilder"/>.</param> /// <param name="fileName">The name of interception policy file.</param> /// <param name="configure">The <see cref="Action{PolicyFileBuilder}"/> to provide <see cref="ScriptOptions"/>.</param> /// <returns>The <see cref="InterceptionBuilder"/>.</returns> public static InterceptionBuilder AddPolicy(this InterceptionBuilder builder, string fileName, Action <PolicyFileBuilder> configure = null) { Guard.ArgumentNotNull(builder, nameof(builder)); Guard.ArgumentNotNullOrWhiteSpace(fileName, nameof(fileName)); var serviceProvider = builder.Services.BuildServiceProvider(); var fileBuilder = new PolicyFileBuilder() .AddImports("Dora.Interception", "System") .AddReferences(typeof(InterceptionPolicyBuilder).Assembly); configure?.Invoke(fileBuilder); var options = ScriptOptions.Default .WithReferences(fileBuilder.References) .WithImports(fileBuilder.Imports); var policyBuilder = new InterceptionPolicyBuilder(serviceProvider); var contents = fileBuilder.ReadAllText(fileName); var script = CSharpScript .Create($"var policyBuilder = Builder;{Environment.NewLine}{contents}", options, typeof(Globals)); script.RunAsync(new Globals(policyBuilder)).Wait(); var resolver = new PolicyInterceptorProviderResolver(policyBuilder.Build()); builder.InterceptorProviderResolvers.Add(nameof(PolicyInterceptorProviderResolver), resolver); return(builder); }