Unhandled Aggregate Exception

Wahm picture Wahm · Jan 22, 2016 · Viewed 12k times · Source

I have followed the tutorial in ( http://www.c-sharpcorner.com/UploadFile/a6fd36/understand-self-host-of-a-web-apiC-Sharp/ ) and ( http://www.c-sharpcorner.com/uploadfile/a6fd36/understanding-how-to-call-the-web-api-from-a-client-applica/ ) but seem to still be getting System.AggregateException on

                 //Call HttpClient.GetAsync to send a GET request to the appropriate URI   
            HttpResponseMessage resp = client.GetAsync("api/books").Result;

and

                var resp = client.GetAsync(string.Format("api/books/{0}", id)).Result;

and

                var resp = client.GetAsync(query).Result;

The error states: An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll

Exception Detail -

  System.AggregateException was unhandled
  HResult=-2146233088
   Message=One or more errors occurred.
   Source=mscorlib
  StackTrace:
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at TestClient.Program.ListAllBooks() in c:\Users\wa\Documents\Visual Studio 2013\Projects\SelfHost\TestClient\Program.cs:line 35
   at TestClient.Program.Main(String[] args) in c:\Users\wa\Documents\Visual Studio 2013\Projects\SelfHost\TestClient\Program.cs:line 20
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Net.Http.HttpRequestException
   HResult=-2146233088
   Message=An error occurred while sending the request.
   InnerException: System.Net.WebException
        HResult=-2146233079
        Message=Unable to connect to the remote server
        Source=System
        StackTrace:
             at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
             at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
        InnerException: System.Net.Sockets.SocketException
             HResult=-2147467259
             Message=No connection could be made because the target machine actively refused it [::1]:8080
             Source=System
             ErrorCode=10061
             NativeErrorCode=10061
             StackTrace:
                  at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
                  at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
             InnerException: 

Any Ideas?

Thank You

Answer

Marty picture Marty · Jan 22, 2016

Please read the last line :)

InnerException: System.Net.WebException
        HResult=-2146233079
        Message=Unable to connect to the remote server