【问题标题】:Windows Service calling helper class inside class library类库中的 Windows 服务调用帮助程序类
【发布时间】:2017-05-29 00:36:17
【问题描述】:

早上好。我一直想弄清楚如何从同一个帮助类的另一部分调用一个帮助类中的函数?我拥有的助手类正在尝试使用 SMTP 发送助手类。在我深入编码之前,我想确保可以做到这一点。

助手类 A 是我的邮件发件人助手 帮助类 B 在确定谁应该接收电子邮件后发送电子邮件警报。

这就是我到目前为止尝试做的事情。我在 B 类中为 A 类设置了 using 子句。

我的印象是我可以像这样简单地调用帮助类来创建一个对象:

        ServiceLibrary.SmtpHelperClass smtp = new SmtpHelperClass();

当我尝试使用 smtp.SendMail(...);它出错了。有人可以阐明这是如何完成的吗?这些帮助类将与 Windows 服务一起使用。我的计划是根据计划的运行时间调用其他助手。

调用者代码是这样写的:

class AuditReminders
{
    SmtpHelperClass mailHelper = new SmtpHelperClass();
    mailHelper.SendMailMessage();
}

我收到一条错误消息,指出此上下文中不存在 SendMailMessage。我的 SmtpHelperClass 是这样写的:

    using System;
using System.Net.Mail;

namespace ServiceLibrary
{
    public class SmtpHelperClass
    {
        public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
        {
            System.Diagnostics.EventLog evlFormatter = new System.Diagnostics.EventLog();
            evlFormatter.Source = "WAST Windows Service";
            evlFormatter.Log = "WAST Windows Service Log";

            // Instantiate a new instance of MailMessage
            MailMessage mMailMessage = new MailMessage();
            // Set the sender address of the mail message
            mMailMessage.From = new MailAddress(from);
            // Set the recepient address of the mail message
            mMailMessage.To.Add(new MailAddress(to));
            // Check if the bcc value is null or an empty string
            if ((bcc != null) && (bcc != string.Empty))
            {
                // Set the Bcc address of the mail message
                mMailMessage.Bcc.Add(new MailAddress(bcc));
            }
            // Check if the cc value is null or an empty value
            if ((cc != null) && (cc != string.Empty))
            {
                string[] words = cc.Split(';');
                foreach (string word in words)
                    try
                    {
                        mMailMessage.CC.Add(new MailAddress(word));
                    }
                    catch (Exception ex)
                    {
                        // place writer for event viewer here
                        evlFormatter.WriteEntry("Error encountered: " + ex.ToString());
                    }
                // Set the CC address of the mail message

            }   // Set the subject of the mail message
            mMailMessage.Subject = subject;
            // Set the body of the mail message
            mMailMessage.Body = body;
            // Set the format of the mail message body as HTML
            mMailMessage.IsBodyHtml = true;
            // Set the priority of the mail message to normal
            mMailMessage.Priority = MailPriority.High;

            // Instantiate a new instance of SmtpClient
            SmtpClient mSmtpClient = new SmtpClient();
            // Send the mail message
            mSmtpClient.Send(mMailMessage);
        }
    }
}

【问题讨论】:

  • 错误是什么?它可能与类本身无关。
  • 我把它写成这样用于测试: class AuditReminders { SmtpHelperClass mailHelper = new SmtpHelperClass(); mailHelper.SendMailMessage();它说 mailHelper 在当前上下文中不存在。
  • 怎么写的?如果需要,您可以编辑您的问题以添加更多信息和代码。
  • 我在上面的 Visual Studio 中表示,在调试期间,收到一条消息说:“SendMailMessage 在此上下文中不存在”

标签: c# .net class-library


【解决方案1】:

您是否引用了MailHelper 在您的项目中的ServiceLibrary 程序集?在您尝试使用它的项目中是否有 using ServiceLibrary; 声明?这些是您遇到错误的最可能原因。

此外,我在您的代码中看到了一些内容。首先,SendMailMessage 被标记为 static,但您试图将其称为实例方法:mailHelper.SendMailMessage();

其次,SendMailMessage 有参数,在调用中没有提供任何参数。

对于第一个问题,您可以像这样调用静态方法(带参数):

SmtpHelperClass.SendMailMessage(from, to, bcc, cc, subject, body);

from、to、bcc、cc、subject 和 body 是具有您要使用的值的变量。

或者把方法改成实例方法(去掉static关键字):

public void SendMailMessage((string from, string to, string bcc, string cc, string subject, string body))

【讨论】:

  • 实际上我确实弄清楚了,这是我试图调用该过程的方式。此外,由于某些奇怪的原因,主项目失去了对帮助程序库的引用。现在一切都好,谢谢:)
【解决方案2】:

根本原因有两个。第一个问题是对该库的引用丢失,导致我无法正确调用该类。一旦我修复了参考,然后使用

using ServiceLibrary.SmtpHelperClass;

这让我可以调用 SmtpMail 辅助函数以及访问库的其他方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-06
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多