如何导出NServiceBus生成的NHibernate Saga映射文件?
Great question! While NServiceBus doesn’t include a built-in one-click method like FluentNHibernate’s ExportTo for auto-generated Saga mappings, there are several practical ways to retrieve those mapping details—perfect for troubleshooting issues or using as a baseline for custom adjustments.
1. Manually Export Mapping Docs via NHibernate Configuration
NServiceBus’s NHibernate persistence sits on top of standard NHibernate, so you can grab the underlying Configuration object after setting up your persistence and use NHibernate’s own APIs to export the mappings:
// Set up your NServiceBus endpoint and NHibernate persistence var endpointConfig = new EndpointConfiguration("YourEndpointName"); var nhPersistence = endpointConfig.UsePersistence<NHibernatePersistence>(); nhPersistence.ConnectionString("YourDatabaseConnectionString"); // Get the NHibernate Configuration instance used by NServiceBus var nhibernateConfig = nhPersistence.GetConfiguration(); // Export each auto-generated Saga mapping to an XML file (hbm-style) foreach (var classMapping in nhibernateConfig.ClassMappings) { var xmlSerializer = new System.Xml.Serialization.XmlSerializer(classMapping.GetType()); using var writer = new StreamWriter($"SagaMapping_{classMapping.EntityName}.hbm.xml"); xmlSerializer.Serialize(writer, classMapping); } // Optional: Export the schema (DDL) to cross-verify mapping behavior new SchemaExport(nhibernateConfig) .SetOutputFile("SagaSchema.sql") .Create(false, false);
This will spit out XML files matching the structure of traditional hbm files, giving you a clear view of how NServiceBus is mapping your Saga data to the database.
2. Use NHibernate Logging to Inspect Mapping Details
If you don’t need full hbm files and just want to debug mapping issues, turn on NHibernate’s debug logging. This will show you detailed info about how NHibernate initializes and uses the Saga mappings, including generated SQL and field associations.
For example, with Serilog, you can configure it to capture NHibernate’s debug logs like this:
Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .Filter.ByIncludingOnly(evt => evt.SourceContext.StartsWith("NHibernate")) .CreateLogger();
Check your logs for entries related to mapping initialization—you’ll find plenty of clues about how your Saga data is being mapped.
3. Export Custom Saga Mappings (If You’re Customizing)
If you’ve already started customizing your Saga mappings (using IConfigureHowToFindSaga or FluentNHibernate-based custom maps), you can use FluentNHibernate’s ExportTo method directly for your custom mappings:
// Your custom Saga mapping class public class OrderProcessingSagaMapping : ClassMap<OrderProcessingSagaData> { public OrderProcessingSagaMapping() { Id(x => x.Id); Map(x => x.OrderId).Unique(); // Add your custom mapping rules here } } // Configure FluentNHibernate and export the mappings var fluentConfig = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2012.ConnectionString("YourConnectionString")) .Mappings(m => m.FluentMappings.Add<OrderProcessingSagaMapping>()); // Export the custom mappings to a folder fluentConfig.Mappings(m => m.FluentMappings.ExportTo(@"C:\SagaMappings"));
This is ideal if you’re moving away from auto-generated mappings and want to document or tweak your custom setup.
内容的提问来源于stack exchange,提问作者carmbrester

