0

我正在为工作制作一个内部用户不活动监视器,并且我正在使用 Windows 窗体和 topshelf 服务来执行此操作,代码将写入 RabbitMQ 服务器,服务将使用消息并将它们转发到数据库,我的代码目前抛出异常并出现以下错误;

Missing type map configuration or unsupported mapping.

Mapping types:
LogData -> DbLogData
AccessEye.LogData -> AccessEye.DbLogData

这是我的服务的代码

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=*****;virtualHost=*****;username=***;password=***").Advanced;

            //var bus = RabbitHutch.CreateBus("host=*******;virtualHost=******;username=****;password=******").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(() =>
            {
                var data2 = LogDataFactory.CollectData();
                data2.EventType = AccessEye.UserStateEvents.Logon;
                WriteLogDataToDb(data2);


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

        }
    }
}

和形式

using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using AccessEye;
using System.Linq;
using EasyNetQ;
using EasyNetQ.Topology;
using Microsoft.Win32;
using MySql.Data.MySqlClient;
using NLog;
using ProtoBuf;
using System.Threading;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Reflection; 

namespace LogProgram
{

    public partial class AppForm : Form
    {

        public static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private Screensaver watcher;
        public Inactivity inactivity;
        IAdvancedBus bus;
        IExchange exchange;

        public AppForm()
        {

                InitializeComponent();
                ConfigureForm();

                // todo: should be in setting
                int pollingInterval = 5000;

                inactivity = new Inactivity(pollingInterval);
                inactivity.Inactive += inactivity_Inactive;
                inactivity.Active += inactivity_Active;
                inactivity.InactivityThresholdMs = 5 * 1000; // todo: should be in setting
                inactivity.Start();
                watcher = new Screensaver(pollingInterval);
                watcher.ScreensaverOff += watcher_ScreensaverOff;
                watcher.ScreensaverOn += watcher_ScreensaverOn;
                watcher.Start();
                SystemEvents.SessionEnding += SystemEvents_SessionEnding;
                SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
                LogManager.ThrowExceptions = true;
                // todo: connection string should be in setting
                bus = RabbitHutch.CreateBus("host=as01.access.local;virtualHost=DEV-Reece;username=reece;password=reece").Advanced;
                exchange = Exchange.DeclareTopic("UserActivity", true, false, null);
                var fanout = Exchange.DeclareFanout("FanoutExchange", true, false, null);
                fanout.BindTo(exchange, new[] { "#" });

        }




        public void ConfigureForm()
        {
            this.Hide();
            TrayDisplayer.Visible = false;
        }

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

        private void AppForm_Load(object sender, EventArgs e)
        {


        }


        void watcher_ScreensaverOn(object sender, EventArgs e)
        {
            var data = LogDataFactory.CollectData();
            data.EventType = AccessEye.UserStateEvents.ScreensaverOn;
            PublishLogData(data);
        }

        void watcher_ScreensaverOff(object sender, EventArgs e)
        {
            var data = LogDataFactory.CollectData();
            data.EventType = AccessEye.UserStateEvents.ScreensaverOff;
            PublishLogData(data);
        }

        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            var data = LogDataFactory.CollectData();

            switch (e.Reason)
            {
                case SessionSwitchReason.SessionLock:
                    data.EventType = UserStateEvents.Lock;
                    break;
                case SessionSwitchReason.SessionUnlock:
                    data.EventType = UserStateEvents.Unlock;
                    break;
            }

            PublishLogData(data);
        }

        public void PublishLogData(AccessEye.LogData LogData)
        {
            WriteLogDataToDb(LogData);

            if (!bus.IsConnected) return;

            try
            {
                using (var publishChannel = bus.OpenPublishChannel())
                {
                    publishChannel.Publish(exchange, LogData.EventType.ToString(), new Message<LogData>(LogData));
                }
            }
            catch (EasyNetQException)
            {
                //todo: handle
            }
        }

        public static byte[] Serialize<T>(T instance)
        {
            using (var stream = new MemoryStream())
            {
                Serializer.Serialize(stream, instance);
                return stream.ToArray();
            }
        }

        private static T DeSerialize<T>(byte[] data)
        {
            using (var stream = new MemoryStream(data))
            {
                return Serializer.Deserialize<T>(stream);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            bus.Dispose();
        }

        public void inactivity_Active(object sender, EventArgs e)
        {

            inactivity.Stop();
            var data = LogDataFactory.CollectData();
            data.EventType = UserStateEvents.Active;
            PublishLogData(data);
            inactivity.Start();

        }

        public void inactivity_Inactive(object sender, EventArgs e)
        {
            inactivity.Stop();
            var data = LogDataFactory.CollectData();
            data.EventType = UserStateEvents.Inactive;
            PublishLogData(data);
            inactivity.Start();
        }

        public void SystemEvents_SessionEnding(object sender, EventArgs e)
        {

            var data = LogDataFactory.CollectData();
            data.EventType = UserStateEvents.Logoff;
            PublishLogData(data);
            Logger.Trace("Logged off");
            }



        }


    }

日志数据类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProtoBuf;

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace AccessEye
{


    public class DbLogData : LogData
    {
        [Key]
        public int Id { get; set; }

        public int? EventTypeId { get; set; }

        [ForeignKey("EventTypeId")]
        public virtual EventType EventTypeTest { get; set; }
    }
}
4

1 回答 1

4

在使用地图之前,您不需要创建地图吗?

AutoMapper.Mapper.CreateMap<LogData, DbLogData>();
于 2013-07-12T09:54:03.130 回答