从 WebApi 错误 HttpResponseMessage 获取图像
Get Image From WebApi Error 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;
}
当 运行 Web 服务在 visual studio 2017 中在 IIS Express 中成功发送来自客户端的请求(我通过 Postman 应用程序从发送请求 localhost:23254/api/Weight/GetPersonnelPicture/?personnelId=2154
进行测试)
但是当发布到本地 IIS 时,我收到 Postman 应用程序的流动错误
{
"Message": "An error has occurred.",
"ExceptionMessage": "A null value was returned where an instance of HttpResponseMessage was expected.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext
controllerContext, Object actionResult)\r\n at
System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n---
End of stack trace from previous location where exception was thrown
---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)\r\n at
System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n---
End of stack trace from previous location where exception was thrown
---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)\r\n at
System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"
}
您需要先定义 httpwebrequest,然后才能期望来自该 http 服务器的响应。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("httpurl");
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//your code
}
}
在 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 服务。
Answer Here
您好,我有 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;
}
当 运行 Web 服务在 visual studio 2017 中在 IIS Express 中成功发送来自客户端的请求(我通过 Postman 应用程序从发送请求 localhost:23254/api/Weight/GetPersonnelPicture/?personnelId=2154
进行测试)
但是当发布到本地 IIS 时,我收到 Postman 应用程序的流动错误
{ "Message": "An error has occurred.", "ExceptionMessage": "A null value was returned where an instance of HttpResponseMessage was expected.", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext, Object actionResult)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()" }
您需要先定义 httpwebrequest,然后才能期望来自该 http 服务器的响应。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("httpurl");
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
//your code
}
}
在 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 服务。 Answer Here