1

Hi I Have Method In Web Api Service For Get Personal Image

 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;
    }

When run Web Service in visual studio 2017 in IIS Express And Send Request From Client Successful (I Test by Postman app From send Request localhost:23254/api/Weight/GetPersonnelPicture/?personnelId=2154 )

But When Publish to Local IIS I Get Flowing Error By Postman Application

{ "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()" }

Mostafa Bagheri
  • 142
  • 3
  • 16
  • 2
    Add some error log code in the catch block it seems you are getting some error and going in catch section. – Sain Pradeep Oct 26 '17 at 10:25
  • I think it is likely the identity your site is running under in IIS cannot access that file share. – Crowcoder Oct 26 '17 at 11:05
  • The application pool your application is running under has an identity. If you did not set it then it will be the default Application Pool Identity which will have no rights to anything external. You can change the identity to something more appropriate but you can give too many rights if you are not careful. Do you have systems/infrastructure department by any chance? – Crowcoder Oct 26 '17 at 11:16
  • ... but do catch and inspect the exception first. Don't try to fix a problem you don't know you have. – Crowcoder Oct 26 '17 at 11:17
  • Tank you for Answering How to add permission to site in iis to access to image folder ? the image folder on Atlas and i can access from local computer to image folder . iis run in local computer . do you need that add permission? – Mostafa Bagheri Oct 26 '17 at 11:18
  • I Solved https://stackoverflow.com/questions/2532079/iis7-folder-permissions-for-web-application – Mostafa Bagheri Oct 29 '17 at 08:14

2 Answers2

0

You need to define the httpwebrequest, before you expect a response from that http server.

         HttpWebRequest request = (HttpWebRequest)WebRequest.Create("httpurl");
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                      //your code
                    }
                }
hustlecoder
  • 187
  • 1
  • 9
  • Tanks For your Answer I use This Code For Get Image PersonalImage =new BitmapImage(new Uri([IIS Ip ]/api/Weight/GetPersonnelPicture/?personnelId=2154)); MY App Platform Is UWP in Iot Core Raspberry Pi – Mostafa Bagheri Oct 26 '17 at 11:04
  • I Solved https://stackoverflow.com/questions/2532079/iis7-folder-permissions-for-web-application – Mostafa Bagheri Oct 29 '17 at 08:13
0

In IIS 7 (not IIS 7.5), sites access files and folders based on the account set on the application pool for the site. By default, in IIS7, this account is NETWORK SERVICE.

Specify an Identity for an Application Pool (IIS 7)

In IIS 7.5 (Windows 2008 R2 and Windows 7), the application pools run under the ApplicationPoolIdentity which is created when the application pool starts. If you want to set ACLS for this account, you need to choose IIS AppPool\ApplicationPoolName instead of NT Authority\Network Service. Answer Here

Mostafa Bagheri
  • 142
  • 3
  • 16