运行Service for Sharepoint 2010时出现System.NullReferenceException [英] System.NullReferenceException in running Service for sharepoint 2010
问题描述
我想启动一项服务,但我在Windows的EventViewer for sharepoint 2010中遇到以下错误。我的错误是:
Hi, I want to start a service but i got the following error in EventViewer of Windows for sharepoint 2010. my error is:
服务无法启动。 System.NullReferenceException:未将对象引用设置为对象的实例。在System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象状态)的AutoMail.Service1.OnStart(String [] args)
Service cannot be started. System.NullReferenceException: Object reference not set to an instance of an object. at AutoMail.Service1.OnStart(String[] args) at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Net.Mail;using System.Timers;using Microsoft.SharePoint;using Microsoft.SharePoint.Utilities;using Microsoft.SharePoint.WebControls;namespace AutoMail{ public partial class Service1 : ServiceBase { DateTime EndDate; DateTime LastService; string[] AssetName = new string[5]; public Service1() { InitializeComponent(); if (EventLog.SourceExists("AutoMail")) { EventLog.CreateEventSource("AutoMail", "Mails"); } eventLog1.Source = "AutoMail"; eventLog1.Log = "Mails"; if (!System.Diagnostics.EventLog.SourceExists("MailSend")) { System.Diagnostics.EventLog.CreateEventSource("MailSend", "AutoMailLog"); } eventLog1.Source = "MailSend"; eventLog1.Log = "AutoMailLog"; } protected override void OnStart(string[] args) { SPSite site1= null; SPWeb spWeb = null; SPListCollection lists = null; SPList list1 = null; SPList assetrepository = null; SPListItemCollection rplistitemcollection = null; SPListItemCollection parlistitemcollection = null; string SiteUrl = "http://dellserver:15000"; using ( site1 = new SPSite(SiteUrl)) { using (spWeb = site1.OpenWeb()) { lists = spWeb.Lists; list1 = spWeb.Lists["PooL Asset Request"]; assetrepository = spWeb.Lists["Asset Repository"]; rplistitemcollection = assetrepository.Items; parlistitemcollection = list1.Items; int i = 0; foreach (SPListItem rpListItem in rplistitemcollection) { LastService = Convert.ToDateTime(rpListItem["LastServiceDate"]); EndDate = DateTime.Now; string AssetStatus = rpListItem["Status"].ToString(); TimeSpan span = EndDate.Subtract(LastService); // label1.Text = span.ToString(); if ((AssetStatus == "Issued") && (Convert.ToInt32(span.Days) > (9))) { AssetName[i] = rpListItem["Asset Key"].ToString(); // label1.Text = AssetName[1].ToString(); i++; } } foreach (SPListItem parListItem in parlistitemcollection) { } } } eventLog1.WriteEntry("In OnStart --- Sending Mail to"+AssetName[0].ToString());// ERROR SHOW// IF i replace eventLog1.WriteEntry("In OnStart --- Sending Mail to"); no error show System.Timers.Timer time = new System.Timers.Timer(); time.Start(); time.Interval = 30000; time.Elapsed += time_elapsed; } protected override void OnStop() { } public void time_elapsed(object sender, ElapsedEventArgs e) { eventLog1.WriteEntry("Mail Sending on "+AssetName[0].ToString()); SendEmail("fgrikuki123@gmail.com","jayant.amar@gmail.com", "Automatic Mail sending", "Successfully working contact Jayant.Kumar2@lntinfotech.com"); } public bool SendEmail(string strTo, string strFrom, string strSubject, string strBody) { bool flag = false; MailMessage mailMsg = new MailMessage(); MailAddress mailAddress = null; try { // To mailMsg.To.Add(strTo); mailAddress = new MailAddress(strFrom); mailMsg.From = mailAddress; mailMsg.Subject = strSubject; mailMsg.Body = strBody; SmtpClient smtpClient = new SmtpClient("smtp.gmail.com",587); System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("jayant.amar","10307064"); smtpClient.Credentials = credentials; smtpClient.Send(mailMsg); flag = true; eventLog1.WriteEntry("Mail Send Successfully"); } catch (Exception ex) { eventLog1.WriteEntry("Error occured"); //Response.Write(ex.Message); } finally { mailMsg = null; mailAddress = null; } return flag; } private void eventLog1_EntryWritten(object sender, EntryWrittenEventArgs e) { } }}
推荐答案
你知道哪一行导致你出问题吗?
Do you know which line is causing you problem?
chanmm
这篇关于运行Service for Sharepoint 2010时出现System.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!