第一次请求时抛出Web api rest服务null引用异常

时间:2014-05-09 13:51:42

标签: c# rest asp.net-web-api

设置

我们有.net web api休息服务,基本上将网页呈现为图像,并将该图像附加到电子邮件并将其发送到地址。我们将服务传递给JSON电子邮件对象,该对象包含要呈现的from,to,subject,body,mailServerName和html。这项服务将被大量调用,但它是第一次提出问题的电话。

问题

当天的第一个.net web api休息服务请求总是抛出以下异常:

  

消息:"发生了错误。",ExceptionMessage:" Object   引用未设置为对象的实例",ExceptionType:   " System.NullReferenceException",Stacktrace:"在   EmailService.Controllers.EmailController.Post(电子邮件)   lambda_method(Closure,Object,Object [])at ...

注意:

在下面添加自定义例外后,现在出现以下异常消息:

  

ExceptionMessage:"将图像保存到Jpeg流时出现异常"

堆栈跟踪:

  

在EmailService.Controllers.EmailController.Post(电子邮件)   lambda_method(Closure,Object,Object [])at   System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor<> c_DisplayClass10.b_9(对象   instance,Object [] methodParameters)at   System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext   controllerContext,IDictionary`2参数,CancellationToken   cancelToken)   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Tasktask)   在   System.RuntimeCompilerServices.TaskAwaiter.HandleNonSuccessAnsDebuggerNotification(任务   任务)......

与此行代码相关:

websiteImage.Save(websiteStream, System.Drawing.Imaging.ImageFormat.Jpeg);

在执行第一个请求后,服务正常工作。再次,只有经过漫长的等待,通常直到第二天,我们才能看到上述异常抛出。

问题

是什么导致服务抛出NullReferenceException以及如何解决它?

控制器类的C#代码和执行实际请求的jQuery如下所示。

控制器类:

namespace EmailService.Controllers
{

    public class EmailController : ApiController
    {

        // POST api/email
        public HttpResponseMessage Post(Email email)
        {

            if (email == null)
            {
                throw new Exception("Email parameter is null");
            }

            Bitmap websiteImage;

            try
            {
                websiteImage = WebsiteToImage.Generate();
            }

            catch (Exception)
            {

                // 500 Internal Server Error
                response.StatusCode = HttpStatusCode.InternalServerError;

                throw new Exception("Exception happened while generating Image");
            }



            // create memory stream from bitmap and save it as a jpeg, this allows us to attach the image from memory, without having to store it on the server
            System.IO.Stream websiteStream = new System.IO.MemoryStream();
            try
            {
                websiteImage.Save(websiteStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }


            catch (Exception)
            {

                // 500 Internal Server Error
                response.StatusCode = HttpStatusCode.InternalServerError;

                throw new Exception("Exception happened while saving Image to Jpeg stream");
            }


            try
            {

                websiteStream.Position = 0;
            }


            catch (Exception)
            {

                // 500 Internal Server Error
                response.StatusCode = HttpStatusCode.InternalServerError;

                throw new Exception("Exception happened while setting stream posiiton=0");
            }


            // create response with status code 200 OK, since we aren't actually creating anything 
            var response = this.Request.CreateResponse(HttpStatusCode.OK);


            try
            {
                // MailMessage is used to represent the e-mail being sent
                using (MailMessage message = new MailMessage(email.from, email.to, email.subject, email.body))
                {

                    // attach jpeg from memory
                    message.Attachments.Add(new Attachment(websiteStream, "letter.jpg", "image/jpeg"));

                    // create mail client
                    SmtpClient mailClient = new SmtpClient(email.mailServerName);

                    // use the Windows credentials of the account (i.e. user account) 
                    mailClient.UseDefaultCredentials = true;

                    // send the message
                    mailClient.Send(message);
                }

            }
            catch (Exception)
            {

                // 500 Internal Server Error
                response.StatusCode = HttpStatusCode.InternalServerError;

                throw new Exception("Exception happened while creating and sending mail message");

            }

            //return new HttpResponseMessage() { Content = new StringContent(html) };
            return response;

        }

        // PUT api/email/5
        public HttpResponseMessage Put(int id, [FromBody]string value)
        {
            // return response status code of 501 Not Implemented
            return this.Request.CreateResponse(HttpStatusCode.NotImplemented);
        }

    }
}

服务请求代码:

$.ajax ({
        type: "POST",
        url: "/api/email/",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(email),
        success: finish,
        error: error

    });

Global.asax中

   public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {

            // add details to server errors 
            GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);


        }
    }

WebApiConfig.cs

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

WebsiteToImage.cs

public class WebsiteToImage
{
    private Bitmap m_Bitmap;
    private string m_Url;
    private string m_FileName = string.Empty;

    private string m_html;

    public WebsiteToImage(string html)
    {
        // Without file 
        //m_Url = url;

        m_html = html;

    }

    public WebsiteToImage(string url, string fileName)
    {
        // With file 
        m_Url = url;
        m_FileName = fileName;
    }

    public Bitmap Generate()
    {
        // Thread 
        var m_thread = new Thread(_Generate);
        m_thread.SetApartmentState(ApartmentState.STA);
        m_thread.Start();
        m_thread.Join();
        return m_Bitmap;
    }

    private void _Generate()
    {
        WebBrowser browser = new WebBrowser { ScrollBarsEnabled = false };
        //browser.Navigate(m_Url);
        browser.DocumentText = m_html;

        browser.DocumentCompleted += WebBrowser_DocumentCompleted;

        while (browser.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }

        browser.Dispose();
    }

    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // Capture 
        var browser = (WebBrowser)sender;
        browser.ClientSize = new Size(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);
        browser.ScrollBarsEnabled = false;
        m_Bitmap = new Bitmap(browser.Document.Body.ScrollRectangle.Width, browser.Document.Body.ScrollRectangle.Bottom);
        browser.BringToFront();
        browser.DrawToBitmap(m_Bitmap, browser.Bounds);

        // Save as file? 
        if (m_FileName.Length > 0)
        {
            // Save 
            m_Bitmap.SaveJPG100(m_FileName);
        }
    }
}

public static class BitmapExtensions
{
    public static void SaveJPG100(this Bitmap bmp, string filename)
    {
        var encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
        bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
    }

    public static void SaveJPG100(this Bitmap bmp, Stream stream)
    {
        var encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
        bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);
    }

    public static ImageCodecInfo GetEncoder(ImageFormat format)
    {
        var codecs = ImageCodecInfo.GetImageDecoders();

        foreach (var codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }

        // Return 
        return null;
    }
}

Email.cs

namespace EmailService.Models
{
    public class Email
    {
        // email properties
        public string mailServerName {get; set;}
        public string from {get; set;}
        public string to {get; set;}
        public string subject {get; set;}
        public string body {get; set;}
        public string content { get; set; }

    }
}

1 个答案:

答案 0 :(得分:0)

从我读过的所有内容来看,在ASP.NET应用程序中使用Single Threaded ApartmentState(STA)时会出现一些问题。请查看文章here,其中介绍了作者如何解决这些问题。

我无法确定为什么这种行为只会在当天的初始请求中发生。

可以找到herehere的其他资源。我希望这有帮助!

尝试将您的generate方法更改为(假设m_Bitmap已在该类的其他位置构建):

public Bitmap Generate()
{
    return m_Bitmap;
}