4

I have a simple web api that will retrieve a image file and return the bytes to the caller. It will work when I host the api project inside visualstudio in local (IIS Express), however, it will fail when I publish this project to my IIS server. Anyone has any idea?

[RoutePrefix("api/v1/Test")]
public class TestController : ApiController
{
    [Route("GetBytesCount")]
    public async Task<int> GetBytesCount()
    {
        var client = new HttpClient() { Timeout = new TimeSpan(0, 5, 0) };
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var response = await client.GetAsync(@"http://icons.wxug.com/i/c/k/clear.gif");
        response.EnsureSuccessStatusCode();
        var imageBytes = await response.Content.ReadAsByteArrayAsync();
        return imageBytes.Length;
    }

}

if I called it from local box

http://localhost:57988/lkr/api/v1/Test/Getbytescount

it will return

int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">1689</int>

When I call it from my server, it will return

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Response status code does not indicate success: 503 (Service Unavailable).
</ExceptionMessage>
<ExceptionType>System.Net.Http.HttpRequestException</ExceptionType>
<StackTrace>
......
</StackTrace>
</Error>
L.T.
  • 673
  • 7
  • 16
  • As an unrelated side note, you should use `response.Content.ReadAsByteArrayAsync()` – Matias Cicero Sep 27 '16 at 12:49
  • Please do not delete the stacktrace...the stack trace points to a line number in your code.... – Hackerman Sep 27 '16 at 12:51
  • @ Matias Cicero, I agree, I will chang the code. – L.T. Sep 27 '16 at 12:54
  • Is there an entry in the EventLog on the server? – Dr Rob Lang Sep 27 '16 at 12:55
  • @Hackerman it is the line "response.EnsureSuccessStatusCode();" throw the exception. The http status code is 503. – L.T. Sep 27 '16 at 12:56
  • Can you tell us which version of the .net framework your project use? – Hackerman Sep 27 '16 at 13:03
  • HTTP 503 is *The Web server is effectively 'closed for repair'. It is still functioning minimally because it can at least respond with a 503 status code, but full service is impossible i.e. the Web site is simply unavailable. There are a myriad possible reasons for this, but generally it is because of some human intervention by the operators of the Web server machine. You can usually expect that someone is working on the problem, and normal service will resume as soon as possible.* - That points to a server problem - not anything in your code – Shannon Holsinger Sep 27 '16 at 13:03
  • Also can you tell us if your IIS machine match the .net framework? – Hackerman Sep 27 '16 at 13:03
  • @Hackerman my project's target framework is 4.5. – L.T. Sep 27 '16 at 13:06
  • Did you have the 4.5 .Net Framework installed on your IIS machine? – Hackerman Sep 27 '16 at 13:08
  • @ Shannon Holsinger, you gave me a new idea to change a different url to give a try. Thanks – L.T. Sep 27 '16 at 13:09
  • @Hackerman, yes the server has 4.5 installed. – L.T. Sep 27 '16 at 13:22
  • @Rob Lang, unfortunately, I don't have the right to view the server log. – L.T. Sep 27 '16 at 13:23
  • Can you post the contents of your `Global.asax` `Application_Start()` method? I tend to find that 503 is given when the server is trying but cannot start your application. – Dr Rob Lang Sep 27 '16 at 13:25
  • @Rob Lang,I think my site is running fine. I can access other pages and apis without any problems. The exception is thrown when the httpclient is tying to call the thirdparty site. – L.T. Sep 27 '16 at 13:47
  • Then I agree with @ShannonHolsinger, you will need to report a bug with the 3rd party. – Dr Rob Lang Sep 27 '16 at 14:00
  • @Rob Lang, the server may have something to do with it, but one thing bothers me is why it works when running in local host mode. Something might relate my IIS server too. – L.T. Sep 27 '16 at 14:21
  • @L.T. I agree that you might need to make a change in IIS but the error is being reported internally; all the 5xx HTTP status codes mean that it's a problem with their server, not yours. – Dr Rob Lang Sep 27 '16 at 14:26
  • looks like the server has some setup that the image site doesn't like. I switched my code to another server on the cloud, seems working fine. :-( – L.T. Sep 27 '16 at 19:55

1 Answers1

1

Go to the Project Properties.

Get SSL URL and use in your browser to access the resource.

enter image description here