【问题标题】:Why Can't I call this method?为什么我不能调用这个方法?
【发布时间】:2013-07-09 23:15:04
【问题描述】:

我正在尝试从服务中的另一个类调用方法,但它表示我尝试调用的方法不存在,如果可能的话希望得到一些帮助。

该程序是一个工作项目,它记录用户不活动,因为我们遇到了人们不接电话的问题,代码如下,这是一个使用来自 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;

namespace LogService
{

    public class WindowsServiceHost : ServiceControl, ServiceShutdown
    {
        public static readonly Logger Logger = LogManager.GetCurrentClassLogger();


        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();
                Console.WriteLine(msg.Body.UserName + " -- " + msg.Body.ComputerName + " -- " + msg.Body.EventType + " -- " + msg.Body.TeamviewerId);
            }));

            return true;

        }

这就是我要调用的方法

 public partial class AppForm : Form
    {

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

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

    }

【问题讨论】:

  • 包含WriteLogDataToDb方法的类是什么?
  • 不应该提供参数吗?
  • 如果函数没有在 WindowsServiceHost 中声明,而是像 OP 这样的另一个类,则传递参数将无济于事

标签: c# winforms class object methods


【解决方案1】:

如果您声明了WriteLogDataToDb() 的类被称为ClassA,那么做两件事。制作方法static,实际上你必须通过它传递一些LogData数据。

public class AppForm
{
    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();
        }
    }
}

然后在您的Start 代码中,您必须调用AppForm.WriteLogDataToDb(data)

编辑:

现在这些类位于两个不同的项目中,您需要添加引用以便您的WindowsServiceHost 可以使用AppForm。为此:

  1. 在包含AppForm 的项目上单击鼠标右键> 属性。在应用程序选项卡上,记下Assembly name:
  2. 右键单击WindowsServiceHost 中的引用项并选择Add reference
  3. 转到Projects 标签
  4. 添加步骤#1 中记下的Assembly name:
  5. WindowsSerivceHostResolve 中右键单击AppForm,添加您的 using 语句。

【讨论】:

  • 我已经更新了答案以包含包含该方法的类,但是当我调用 AppForm.WriteLogDataToDb(); 时,它说名称“Appform”在当前上下文中不存在
  • 这一切都在一个项目中吗?右击的时候可以Resolve
  • 不,它不会让我解决,我忘了提到这一点,但它们在同一个解决方案中,但它们是不同的项目
  • 需要在包含WindowsServiceHost的项目中引用包含AppForm的项目。
  • 好的,因此您可能需要重新考虑项目的设计。您不能让两个项目相互引用。您可能需要将它们移动到一个项目中或添加另一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多