为什么我不能调用我的WriteLogDataToDb方法?

时间:2013-07-11 16:54:14

标签: c# mysql winforms rabbitmq

我正在为工作中的内部应用程序创建用户不活动记录器,并且我在将消耗的消息写入MySQL数据库时遇到问题,当我尝试调用将消息写入数据库的方法时,它会抛出一个错误:'AccessEye.LogData'是'type',在给定的上下文中无效

任何和所有这方面的帮助将不胜感激,这是一个Windows表单项目,其中topshelf服务被用作RabbitMQ使用者,最终将消息转发到数据库,如上所述=]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using NLog;
using IWshRuntimeLibrary;
using Topshelf;
using System.Data.Odbc;
using EasyNetQ;
using RabbitMQ;
using EasyNetQ.Topology;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccessEye;
using System.ComponentModel;

namespace LogService
{
    public class WindowsServiceHost : ServiceControl, ServiceShutdown
    {
        public static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        public static void WriteLogDataToDb(LogData data)
        {
            using (var db = new LogService.UserActivityDataContext())
            {
                DbLogData logData = AutoMapper.Mapper.Map<LogData, DbLogData>(data);

                int t = (int)data.EventType;

                EventType eventType = db.EventTypes.FirstOrDefault(r => r.Id == t);

                if (eventType == null)
                {
                    eventType = db.EventTypes.Add(new EventType
                    {
                        Event = GetEnumDescriptionAttributeValue(data.EventType),
                        Id = (int)data.EventType
                    });
                    db.SaveChanges();
                }

                logData.EventTypeId = eventType.Id;
                db.LogEvents.Add(logData);

                db.SaveChanges();
            }
        }

        public static string GetEnumDescriptionAttributeValue(Enum value)
        {
            var fieldInfo = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }

        public bool Start(HostControl hostControl)
        {
            Program.bus = RabbitHutch.CreateBus("host=as01.access.local;virtualHost=DEV-Reece;username=reece;password=reece").Advanced;

            //var bus = RabbitHutch.CreateBus("host=as01.access.local;virtualHost=DEV-Reece;username=reece;password=reece").Advanced;
            var queue = Queue.Declare(true, false, true, null);
            var exchange = Exchange.DeclareFanout("UserActivityFanout", true, false, null);
            var exchangeTopic = Exchange.DeclareTopic("UserActivity", true, false, null);
            queue.BindTo(exchange, "#");
            exchange.BindTo(exchangeTopic, "#");
            Program.bus.Subscribe<AccessEye.LogData>(queue, (msg, messageRecInfo) => Task.Factory.StartNew(() =>
            {
                WriteLogDataToDb(AccessEye.LogData);

                //AppForm.WriteLogDataToDb(data);
                //Console.WriteLine(msg.Body.UserName + " -- " + msg.Body.ComputerName + " -- " + msg.Body.EventType + " -- " + msg.Body.TeamviewerId);
            }));

            return true;    
        }

        public bool Stop(HostControl hostControl)
        {
            Logger.Trace("STOP");
            Program.bus.Dispose();
            return true;
        }

        public void Shutdown(HostControl hostControl)
        {
            Logger.Trace("SHUTDOWN");
            Program.bus.Dispose();

        }
    }
}

2 个答案:

答案 0 :(得分:0)

您正在将Type作为参数传递给WriteLogDataToDb()方法,当它需要AccessEye.LogData的实例时。

我不知道你想在哪里初始化你的变量,但要“修复”(你用我建议的代码初始化一个默认实例)你的问题可能你必须改变这段代码。

Program.bus.Subscribe<AccessEye.LogData>(queue, 
       (msg, messageRecInfo) => Task.Factory.StartNew(() =>
       {
            WriteLogDataToDb(new AccessEye.LogData());
       }));

请注意,我不知道为AccessEye.LogData类型定义了哪些构造函数,但您可能知道。

另一方面,我认为lambda表达式的一个参数是所需类型的实例。

Suscribe()方法的类型AccessEye.LogData是什么?

修改

正如OP在他的评论中所说,解决方案是使用以下代码片段替换原始代码:

Program.bus.Subscribe<AccessEye.LogData>(queue, 
   (msg, messageRecInfo) => Task.Factory.StartNew(() =>
   {
       var data2 = LogDataFactory.CollectData();
       data2.EventType = AccessEye.UserStateEvents.Logon;
       WriteLogDataToDb(data2);
   }));

答案 1 :(得分:0)

您的操作似乎不正确,您要写入数据库的是消息的正文,而不是消息的类型;

Program.bus.Subscribe<AccessEye.LogData>(queue, (msg, messageRecInfo) => 
    Task.Factory.StartNew(() =>
        {
            WriteLogDataToDb(msg.Body);
        }));