Domanda

I have created a web service with ASP.NET Web Api and trying to send post request to this restful web api using AFNetworking 2.

When I send post request to my local server in IIS and debug using visual studio 2012 every things works well and i get 204 response.

but when I upload the web api application to the server and sen POST request I get internal server error (500)

here is how I send POST request:

static NSString * const BaseURLString = @"http://webapi.r2gblog.ir/";
NSURL *baseURL = [NSURL URLWithString:BaseURLString];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

//Indicationg this device is online and sending its dEVICE token to the server
Device *device = [Device new];

device.IsOnline = @"True";
//updating current active users of this app in the server
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:
                            device.DeviceToken,@"DeviceToken",
                            device.IsOnline,@"IsOnline",
                            device.LastActivityDate,@"LastActivityDate",
                            device.DeviceModel,@"DeviceModel",
                            nil];

[manager POST:@"api/iosAppStats" parameters:dictionary success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"%@",responseObject);

} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"%@",error);
}];

the error message:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: internal server error (500)" UserInfo=0x175884d0 {NSErrorFailingURLKey=http://webapi.r2gblog.ir/api/iosAppStats, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x17592e90> { URL: http://webapi.r2gblog.ir/api/iosAppStats } { status code: 500, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 0;
Date = "Tue, 01 Apr 2014 20:10:09 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
"X-Powered-By-Plesk" = PleskWin;
} }, NSLocalizedDescription=Request failed: internal server error (500)}

I have a controller in my WebApi named "iosAppStatsController" with one method:

// POST api/stats
        public void Post(Device device)
        {
           .
           .
           .
        }

and in my webApiConfig.cs which is located in App_start I have added this which causes all the responses be in json format:

var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.Converters = new List<JsonConverter>() { new GuidRefJsonConverter() };
        json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
        config.Formatters.Remove(config.Formatters.XmlFormatter);

POST request raw text captured using fiddler 2:

POST http://webapi.r2gblog.ir/api/iosAppStats HTTP/1.1
Host: webapi.r2gblog.ir
Connection: keep-alive
Accept: */*
Accept-Encoding: gzip, deflate
Content-Length: 194
Content-Type: application/json; charset=utf-8
Accept-Language: en;q=1
Connection: keep-alive
User-Agent: Apper/1.0 (iPhone; iOS 7.0.4; Scale/2.00)

{"DeviceToken":"1234567890","LastActivityDate":"2014-04-02T00:40:12.605+0430","IsOnline":"True","OSVersion":"7.0.4","DeviceModel":"iPhone4"}

Response:

HTTP/1.1 500 Version string portion was too short or too long.
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Tue, 01 Apr 2014 20:10:09 GMT
Content-Length: 0

does anyone have any idea why I get this internal server error 500 when I deploy my app to the server?

È stato utile?

Soluzione

I solved the issue. maybe it helps other ones.
When you get internal server error 500 it means your web service is crashing because an internal error or an exception occurred in you controller.
if your web service working in local but not in the server, republish you project again. you can do this in visual studio by right clicking on your solution and click publish.
then upload all the created dll files to your server. also check you replace the local web.config with the one on your server. be careful to change your connection string.

if you are using dependency injection be sure to reslove your dependency. if you have forgotten to resolve it you can't rich the calling action!

you can use Log4net to find where in your code exception occurres and email it to you! also you can upload the yourWebService.pdb file created in the bin folder of your project to the server. when your application crashes, IIS can use the uploaded pdb file and tell exactly in which line of your code crashes happens.

THE FINAL WORD: if you get error 500 you must be sure your code crashes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top