从WebApi错误HttpResponseMessage获取图像

时间:2017-10-26 10:13:06

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

您好,我有Web Api服务获取个人形象的方法

 public HttpResponseMessage GetPersonnelPicture( int personnelId)
    {
        JamsazERPLiteEntities db = new JamsazERPLiteEntities();

        var fingerPrint = db.FingerPrints.FirstOrDefault(c => c.SequenceNumber == fingerNumber && c.MAC == MAC && !c.IsDeleted);

        if (fingerPrint != null && fingerPrint.Personnel != null)
        {
            try
            {
                var result = new HttpResponseMessage(HttpStatusCode.OK);
                FileStream fileStream =
                    new FileStream(@"\\atlas\Personnel Pictures\" + personnelId + ".jpg",
                        FileMode.Open);
                Image image = Image.FromStream(fileStream);
                MemoryStream memoryStream = new MemoryStream();
                image.Save(memoryStream, ImageFormat.Jpeg);
                result.Content = new ByteArrayContent(memoryStream.ToArray());
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                memoryStream.Close();
                memoryStream.Dispose();
                fileStream.Close();
                fileStream.Dispose();

                return result;
            }
            catch { }
        }

        return null;
    }

在IIS Express中的Visual Studio 2017中运行Web Service并从客户端发送请求成功(我通过Postman应用程序测试来自发送请求localhost:23254/api/Weight/GetPersonnelPicture/?personnelId=2154

但是当发布到本地IIS时,我通过邮递员应用程序得到流动错误

  

{           “消息”:“发生错误。”,           “ExceptionMessage”:“返回了一个空值,其中预期有一个HttpResponseMessage实例。”,           “ExceptionType”:“System.InvalidOperationException”,           “StackTrace”:“在System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext)   controllerContext,Object actionResult)\ r \ n at   System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n \ n
  在   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n \ n
  在   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()”       }

2 个答案:

答案 0 :(得分:0)

在您期望来自该http服务器的响应之前,您需要定义httpwebrequest。

         HttpWebRequest request = (HttpWebRequest)WebRequest.Create("httpurl");
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                      //your code
                    }
                }

答案 1 :(得分:0)

在IIS 7(不是IIS 7.5)中,站点根据在站点的应用程序池上设置的帐户访问文件和文件夹。默认情况下,在IIS7中,该帐户是NETWORK SERVICE。

为应用程序池(IIS 7)指定身份

在IIS 7.5(Windows 2008 R2和Windows 7)中,应用程序池在ApplicationPoolIdentity下运行,该应用程序池是在应用程序池启动时创建的。如果要为此帐户设置ACLS,则需要选择IIS AppPool \ ApplicationPoolName而不是NT Authority \ Network Service。 Answer Here