Uncategorized

Hazina Plugins: AI-Powered Runtime Code Generation with Roslyn

What if your application could write its own extensions at runtime? Hazina’s Dynamic Plugin System uses Roslyn to compile C# code on-the-fly, enabling AI to create new capabilities without redeployment. This is not science fiction – it’s a production-ready pattern.

How It Works

  1. AI (or a user) writes C# code for a new capability
  2. Hazina compiles it at runtime using Roslyn
  3. The compiled plugin runs in a sandboxed environment
  4. Results are returned to the caller

Creating a Plugin

var pluginManager = serviceProvider.GetRequiredService<IPluginManager>();

var plugin = await pluginManager.CreatePluginAsync(new PluginDefinition {
    Name = "WelcomeEmailPlugin",
    Description = "Sends welcome email to new customers",
    Code = @"
        var customer = context.GetParameter<Customer>( - customer - );
        var emailService = context.GetService<IEmailService>();

        await emailService.SendTemplateAsync(
            to: customer.Email,
            template:  - welcome - ,
            data: new { CustomerName = customer.Name }
        );

        return PluginResult.Success( - Email sent - );
    "
});

Executing a Plugin

var result = await pluginManager.ExecuteAsync("WelcomeEmailPlugin",
    new Dictionary<string, object> {
        ["customer"] = newCustomer
    });

if (result.IsSuccess)
    Console.WriteLine(result.Output);

Security: The Sandbox

Every plugin runs in a controlled environment:

var securitySettings = new PluginSecuritySettings {
    AllowedNamespaces = new[] {
        "System", "System.Linq", "System.Collections.Generic"
    },
    BlockedTypes = new[] {
        "System.IO.File", "System.Net.Http.HttpClient"
    },
    MaxExecutionTime = TimeSpan.FromSeconds(30),
    MaxMemoryMB = 128
};

Version Management

// Update a plugin (new version created automatically)
await pluginManager.UpdatePluginAsync("WelcomeEmailPlugin", newCode);

// View version history
var versions = await pluginManager.GetVersionHistoryAsync("WelcomeEmailPlugin");

// Rollback if something goes wrong
await pluginManager.RollbackAsync("WelcomeEmailPlugin", previousVersion);

Real-World Use Cases

  • Custom business rules – Let AI generate validation logic from natural language
  • Data transformations – Dynamic ETL pipelines that adapt to new data formats
  • Automation workflows – Create new automation steps without deploying code
  • Report generators – AI writes the data processing logic for custom reports

In the next post, we’ll explore plugin security in depth – how to keep dynamic code safe.

Frequently Asked Questions

How does Hazina’s Dynamic Plugin System work with Roslyn?

Hazina’s Dynamic Plugin System utilizes Roslyn to compile C# code at runtime. This allows AI or users to create new plugins on-the-fly, which are then executed in a sandboxed environment to ensure security and stability.

What are some real-world use cases for Hazina plugins?

Hazina plugins can be used for various applications, including generating custom business rules, creating dynamic ETL pipelines, automating workflows, and generating reports. This flexibility allows businesses to adapt quickly to changing requirements without redeploying code.

How does Hazina ensure the security of runtime-generated plugins?

Hazina runs every plugin in a controlled sandbox environment, restricting access to specific namespaces and types. Additional security settings, such as execution time limits and memory constraints, further enhance the safety of executing dynamic code.

Can Hazina plugins be updated or rolled back easily?

Yes, Hazina provides built-in functionality for updating plugins, which automatically creates a new version. Users can also view version history and roll back to previous versions if needed, ensuring flexibility and control over plugin management.

Terug naar overzicht
ENNL