Created
February 1, 2012 12:42
-
-
Save jptoto/1716843 to your computer and use it in GitHub Desktop.
WebAPI Error Catch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[WebInvoke(UriTemplate = "", Method = "POST")] | |
public Email Post(Email email) | |
{ | |
try | |
{ | |
repository.Add(email); | |
foreach (var file in email.Attachments) | |
{ | |
if (!string.IsNullOrEmpty(file.Content)) | |
{ | |
if (File.Exists(attachmentSaveFolder + file.Name)) | |
File.Delete(attachmentSaveFolder + file.Name); | |
byte[] filebytes = Convert.FromBase64String(file.Content); | |
FileStream fs = new FileStream(attachmentSaveFolder + file.Name, | |
FileMode.CreateNew, | |
FileAccess.Write, | |
FileShare.None); | |
fs.Write(filebytes, 0, filebytes.Length); | |
fs.Close(); | |
} | |
} | |
return email; | |
} | |
catch (HttpRequestException e) | |
{ | |
// !!! | |
// HttpResponseExceptions are not properly returned yet. It's a known bug in WCF WebAPI preview 6. I'm hoping it's fixed in a subsequent release. | |
// !!! | |
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(e.InnerException.ToString()) }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment