C# 为什么可以';我不能调用WriteLogDataToDb方法吗?
我正在为工作中的内部应用程序制作一个用户不活动记录器,并且我在将消耗的消息写入MySQL数据库时遇到问题,当我尝试调用将消息写入数据库的方法时,它抛出一个错误:“AccessEye.LogData”是一个“类型”,在给定上下文中无效 任何与此相关的帮助都将不胜感激,这是一个windows窗体项目,使用topshelf服务作为RabbitMQ使用者,最终将消息转发到数据库,如前所述=]C# 为什么可以';我不能调用WriteLogDataToDb方法吗?,c#,mysql,winforms,rabbitmq,C#,Mysql,Winforms,Rabbitmq,我正在为工作中的内部应用程序制作一个用户不活动记录器,并且我在将消耗的消息写入MySQL数据库时遇到问题,当我尝试调用将消息写入数据库的方法时,它抛出一个错误:“AccessEye.LogData”是一个“类型”,在给定上下文中无效 任何与此相关的帮助都将不胜感激,这是一个windows窗体项目,使用topshelf服务作为RabbitMQ使用者,最终将消息转发到数据库,如前所述=] using System; using System.Collections.Generic; using Sy
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统诊断;
使用NLog;
使用iwruntimelibrary;
使用Topshelf;
使用System.Data.Odbc;
使用EasyNetQ;
使用RabbitMQ;
使用EasyNetQ.拓扑;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用AccessEye;
使用系统组件模型;
命名空间日志服务
{
公共类WindowsServiceHost:ServiceControl,ServiceShutdown
{
公共静态只读记录器Logger=LogManager.GetCurrentClassLogger();
公共静态无效WriteLogDataToDb(日志数据)
{
使用(var db=new LogService.UserActivityDataContext())
{
DbLogData logData=AutoMapper.Mapper.Map(数据);
int t=(int)data.EventType;
EventType EventType=db.EventTypes.FirstOrDefault(r=>r.Id==t);
if(eventType==null)
{
eventType=db.EventTypes.Add(新的eventType
{
事件=GetEnumDescriptionAttributeValue(data.EventType),
Id=(int)data.EventType
});
db.SaveChanges();
}
logData.EventTypeId=eventType.Id;
db.LogEvents.Add(logData);
db.SaveChanges();
}
}
公共静态字符串GetEnumDescriptionAttributeValue(枚举值)
{
var fieldInfo=value.GetType().GetField(value.ToString());
var attributes=(DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute),false);
返回属性。长度>0?属性[0]。说明:value.ToString();
}
公用bool启动(主机控制主机控制)
{
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(真、假、真、空);
var exchange=exchange.DeclareFanout(“UserActivityFanout”,真、假、空);
var-exchangeTopic=Exchange.DeclareTopic(“UserActivity”,真、假、空);
queue.BindTo(交换“#”);
exchange.BindTo(exchangeTopic,“#”);
Program.bus.Subscribe(queue,(msg,messageRecInfo)=>Task.Factory.StartNew(()=>
{
WriteLogDataToDb(AccessEye.LogData);
//AppForm.WriteLogDataToDb(数据);
//Console.WriteLine(msg.Body.UserName+“--”+msg.Body.ComputerName+“--”+msg.Body.EventType+“--”+msg.Body.TeamviewerId);
}));
返回true;
}
公共广播站(主机控制主机控制)
{
记录器。跟踪(“停止”);
Program.bus.Dispose();
返回true;
}
公共无效关闭(主机控制主机控制)
{
记录器跟踪(“关闭”);
Program.bus.Dispose();
}
}
}
您正在将类型
作为参数传递给WriteLogDataToDb()
方法,而该方法需要一个AccessEye.LogData的实例
我不知道您希望在哪里初始化变量,但要“修复”(您正在使用我建议的代码初始化默认实例)您的问题可能必须更改这段代码
Program.bus.Subscribe<AccessEye.LogData>(queue,
(msg, messageRecInfo) => Task.Factory.StartNew(() =>
{
WriteLogDataToDb(new AccessEye.LogData());
}));
您的操作似乎不正确,您要写入数据库的是消息的主体,而不是消息的类型
Program.bus.Subscribe<AccessEye.LogData>(queue, (msg, messageRecInfo) =>
Task.Factory.StartNew(() =>
{
WriteLogDataToDb(msg.Body);
}));
Program.bus.Subscribe(队列,(msg,messageRecInfo)=>
Task.Factory.StartNew(()=>
{
WriteLogDataToDb(消息体);
}));
Nevermind,我用另一种方式解决了这个问题,无论如何谢谢你的回答有帮助=]虽然接受我的回答对我来说是积极的(谢谢!),但如果你采取的解决方案不同,我认为你最好用你问题的最佳/采取的解决方案来回答你自己的问题。这样做,你可能会在将来帮助任何检查这个问题的人。解决方案非常特定于我的情况,我必须编写var data2=LogDataFactory.CollectData();data2.EventType=AccessEye.UserStateEvents.Logon;WriteLogDataToDb(数据2)代码>
Program.bus.Subscribe<AccessEye.LogData>(queue, (msg, messageRecInfo) =>
Task.Factory.StartNew(() =>
{
WriteLogDataToDb(msg.Body);
}));