【发布时间】:2017-10-26 10:13:05
【问题描述】:
您好,我在 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 服务并成功从客户端发送请求(我通过 Postman 应用程序从发送请求 localhost:23254/api/Weight/GetPersonnelPicture/?personnelId=2154 测试)
但是当发布到本地 IIS 时,邮递员应用程序出现流动错误
{ "Message": "发生错误。", "ExceptionMessage": "在期望 HttpResponseMessage 实例的地方返回了一个空值。", "ExceptionType": "System.InvalidOperationException", “StackTrace”:“在 System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext,对象 actionResult)\r\n 在 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()" }
【问题讨论】:
-
在 catch 块中添加一些错误日志代码,您似乎遇到了一些错误并进入了 catch 部分。
-
我认为您的站点在 IIS 中运行的身份可能无法访问该文件共享。
-
您的应用程序在其下运行的应用程序池具有标识。如果您没有设置它,那么它将是默认的应用程序池标识,它对任何外部都没有权限。您可以将身份更改为更合适的身份,但如果您不小心,您可能会赋予过多的权利。你们有系统/基础设施部门吗?
-
... 但首先要捕获并检查异常。不要试图解决你不知道的问题。
-
谢谢你的回答 如何在iis中添加站点访问图像文件夹的权限? Atlas上的图像文件夹,我可以从本地计算机访问图像文件夹。 iis 在本地计算机上运行。你需要那个添加权限吗?
标签: c# asp.net-web-api httpresponsemessage