2014-10-27 38 views
2

考慮下面的代碼:如何使用OrmLite將ServiceStack.Messaging.Message記錄到數據庫?

public class AppHost : BasicAppHost 
{ 
    public AppHost() 
     : base(typeof(LeadService).Assembly){} 

    public override void Configure(Container container) 
    { 
     SetConfig(new HostConfig 
     { 
      DebugMode = ConfigUtils.GetAppSetting<bool>("DebugMode:Enabled", false) 
     }); 

     //DataAccess 
     //Set ORMLite to work with columns like ColumnLikeThis 
     PostgreSqlDialect.Provider.NamingStrategy = new OrmLiteNamingStrategyBase(); 
     //Set ORMLite to use ServiceStack.Text for JSON serialization 
     PostgreSqlDialect.Provider.StringSerializer = new JsonStringSerializer(); 
     var dbFactory = new OrmLiteConnectionFactory(ConfigUtils.GetConnectionString("Lead:Default"), PostgreSQLDialectProvider.Instance); 
     container.Register<IDbConnectionFactory>(dbFactory); 

     //RabbitMQ 
     container.Register<IMessageService>(c => new RabbitMqServer() 
     { 
      AutoReconnect = true, 
      DisablePriorityQueues = true, 

     }); 
     var mqServer = container.Resolve<IMessageService>(); 

     //Handlers 
     container.Register<IMessageHandlers>(c => new MessageHandlers(c.Resolve<IDbConnectionFactory>())); 
     var handlers = container.Resolve<IMessageHandlers>(); 

     mqServer.RegisterHandler<LeadInformation>(handlers.OnProcessLeadInformation, handlers.OnExceptionLeadInformation); 

     mqServer.Start();  
    } 
} 


public class MessageHandlers : IMessageHandlers 
{ 
    private readonly ILog _log = LogManager.GetLogger(typeof(MessageHandlers)); 

    private readonly IDbConnectionFactory _connectionFactory; 

    public MessageHandlers(IDbConnectionFactory connectionFactory) 
    { 
     _connectionFactory = connectionFactory; 
    } 

    public object OnProcessLeadInformation(IMessage<LeadInformation> request) 
    { 
     var sw = Stopwatch.StartNew(); 
     try 
     { 
      // Log to the database 
      using (var db = _connectionFactory.OpenDbConnection()) 
      { 
       db.CreateTableIfNotExists<Message>(); 
       var msg = request as Message<LeadInformation>; // Anyway not to have to cast it? 
       db.Save(msg); // Does not work 
      } 
      // Run rules against lead 

      // Log response to database 

      // return response 
     } 
     catch (Exception exception) 
     { 
      _log.Error(request, exception); 
     } 
     return new LeadInformationResponse 
     { 
      TimeTakenMs = sw.ElapsedMilliseconds, 
      Result = "Processed lead {0}".Fmt(request.GetBody().LeadApplication.LastName) 
     }; 
    } 

    public void OnExceptionLeadInformation(IMessage<LeadInformation> request, Exception exception) 
    { 
     _log.Error(request, exception); 
    } 

} 

是否有可能持續整個消息?表格被創建,並且我能夠保存一條消息,這就是不再使用不同的消息保存。

更新

原來在保存操作

Npgsql.NpgsqlException被捉住了,我得到一個例外 _HResult = -2147467259 _message = ERROR:42P01:關於 「消息1" does not exist HResult=-2147467259 IsTransient=false Message=ERROR: 42P01: relation "Message 1」不存在 源= Npgsql的錯誤碼= -2147467259 BaseMessage =關係 「消息1" does not exist Code=42P01 ColumnName="" ConstraintName="" DataTypeName="" Detail="" ErrorSql=SELECT "Id", "CreatedDate", "Priority", "RetryAttempts", "ReplyId", "ReplyTo", "Options", "Error", "Tag", "Body" FROM "Message 1」 WHERE 「ID」=(( 'ab297bca-5aea-4886-B09B-5a606b0764d5'):: UUID) 文件= SR Ç\後端\解析器\ parse_relation.c 提示= 「」 行= 986 位置= 119 例行= parserOpenTable 的SchemaName = 「」 嚴重性= ERROR 表名= 「」 凡= 「」 堆棧跟蹤: 在Npgsql.NpgsqlState.d__0.MoveNext() 在Npgsql.ForwardsOnlyDataReader.GetNextResponseObject(布爾清理) 在Npgsql.ForwardsOnlyDataReader.GetNextRowDescription() 在Npgsql.ForwardsOnlyDataReader.NextResultInternal() 在Npgsql.ForwardsOnlyDataReader..ctor(IEnumerable的1 dataEnumeration, CommandBehavior behavior, NpgsqlCommand command, NotificationThreadBlock threadBlock, Boolean preparedStatement, NpgsqlRowDescription rowDescription) at Npgsql.NpgsqlCommand.GetReader(CommandBehavior cb) at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior cb) at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() at ServiceStack.OrmLite.OrmLiteReadExtensions.ExecReader(IDbCommand dbCmd, String sql) at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ConvertTo[T](IDbCommand dbCmd, String sql) at ServiceStack.OrmLite.OrmLiteReadExtensions.SingleById[T](IDbCommand dbCmd, Object value) at ServiceStack.OrmLite.OrmLiteWriteExtensions.Save[T](IDbCommand dbCmd, T obj) at ServiceStack.OrmLite.OrmLiteWriteConnectionExtensions.<>c__DisplayClass5a 1 .b__58(IDbCommand dbCmd) at ServiceStack.OrmLite.OrmLiteExecFilter .Exec [T](IDbConnection dbConn,Func 2 filter) at ServiceStack.OrmLite.ReadConnectionExtensions.Exec[T](IDbConnection dbConn, Func 2 filter) at ServiceStack.OrmLite.OrmLiteWriteConnectionExtensions.Save [T](IDbConnection dbConn,T obj,Boolean references) at LO.Leads.Processor.ServiceInterface.MessageHandlers.OnProcessLeadInformation( IMessage`1請求)在E:\鉛的\ src \ LO.Leads.Processor \ LO.Leads.Processor.ServiceInterface \ MessageHandlers.cs:行41

更新2

原來我的演員陣容錯誤,現在可以使用

using (var db = _connectionFactory.OpenDbConnection()) 
{ 
    db.CreateTableIfNotExists<Message>(); 
    db.Save(request as Message); 
} 

謝謝, Stephen

回答

2

您必須將IMessage轉換回消息DTO才能使其工作。例如

using (var db = _connectionFactory.OpenDbConnection()) 
{ 
    db.CreateTableIfNotExists<Message>(); 
    db.Save(request as Message); 
} 
相關問題