0

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

对此的任何和所有帮助将不胜感激,它是一个 Windows 窗体项目,其中一个顶级服务被用作 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();

        }
    }
}
4

2 回答 2

0

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

Program.bus.Subscribe<AccessEye.LogData>(queue, (msg, messageRecInfo) => 
    Task.Factory.StartNew(() =>
        {
            WriteLogDataToDb(msg.Body);
        }));
于 2013-07-11T17:05:56.723 回答
0

您正在将Typeas 参数传递给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);
   }));
于 2013-07-11T16:58:08.563 回答