为什么可以';t我调用WriteLogDataToDb方法

本文关键字:调用 WriteLogDataToDb 方法 为什么 | 更新日期: 2023-09-27 18:22:04

我正在为工作中的内部应用程序制作一个用户非活动记录器,并且我在将消耗的消息写入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();
        }
    }
}

为什么可以';t我调用WriteLogDataToDb方法

您正在将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);
   }));

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

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