gpt4 book ai didi

c# - 为什么我无法调用 WriteLogDataToDb 方法?

转载 作者:行者123 更新时间:2023-11-30 01:29:54 25 4
gpt4 key购买 nike

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

对此的任何和所有帮助将不胜感激,这是一个 Windows 窗体项目,其中一个 topshelf 服务被用作 RabbitMQ Consumer,它最终将消息转发到数据库,如上所述 =]

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();

}
}
}

最佳答案

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

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

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);
}));

关于c# - 为什么我无法调用 WriteLogDataToDb 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17599186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com