I have a simple one-way web service in my C# .NET web application. I need to download an image from a given URL and save it to my server.
However, every OTHER time I call the service it works perfectly. The other times I get a "Thread was being aborted" exception on the webClient.DownloadFile line.
I've read that WebClient uses httpWebRequest which requires access to HttpContext.Current. Is this true? If so, why can it access it, literally, every other time?
I've tried adding the following lines before my call:
WebService service = new WebService();
HttpContext.Current = service.Context;
But it doesn't make a difference.
I ran the service 10 times, here are the results:
1: Success
2: Exception: Thread was being aborted. Stack Trace: at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.WebClient.DownloadBitsState.RetrieveBytes(Int32& bytesRetrieved) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) at System.Net.WebClient.DownloadFile(Uri address, String fileName)...
3: Success
4: Exception: Thread was being aborted. Stack Trace: at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.WebClient.DownloadBitsState.RetrieveBytes(Int32& bytesRetrieved) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) at System.Net.WebClient.DownloadFile(Uri address, String fileName)...
5: Success
6: Exception: Thread was being aborted. Stack Trace: at System.Net.WebClient.DownloadBitsState.SetResponse(WebResponse response) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) at System.Net.WebClient.DownloadFile(Uri address, String fileName) ...
7: Success
8: Exception: Thread was being aborted. Stack Trace: at System.Net.WebClient.DownloadBitsState.SetResponse(WebResponse response) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) at System.Net.WebClient.DownloadFile(Uri address, String fileName) ...
9: Success
10: Exception: Thread was being aborted. Stack Trace: at System.Net.WebClient.DownloadBitsState.SetResponse(WebResponse response) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) at System.Net.WebClient.DownloadFile(Uri address, String fileName)...
Is there any way to make this work??
EDIT: In response to question regarding posting code: There is not much to post. It is all quite simple.
[WebService(Namespace = "http://nerdliness.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
[System.ComponentModel.ToolboxItem(false)]
public class FileProcessor : System.Web.Services.WebService
{
[WebMethod]
[SoapDocumentMethod(OneWay = true)]
public void ParseFile(String urlOfFileToGrab, String destinationPath)
{
try
{
WebClient client = new WebClient();
client.DownloadFile(urlOfFileToGrab, destinationPath);
client.Dispose();
}
catch (Exception ex)
{
//Log it
}
}
}