无法在try-catch C#

时间:2017-03-16 12:25:51

标签: c# asp.net exception exception-handling try-catch

这个异常被抛出,我显然无法抓住它:

  

无法建立连接,因为目标计算机主动拒绝了它127.0.0.1:10000

来源:

Line 102:            try
Line 103:            {
Line 104:                var blobClient = GetClient();  <-- throws here
Line 105:                var container = blobClient.GetContainerReference(containerName);
Line 106:                container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
  

源文件:... Blob.cs行:104

整个功能:

private static CloudBlobContainer GetContainer(string containerName)
{
    try
    {
        var blobClient = GetClient();
        var container = blobClient.GetContainerReference(containerName);
        container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
        return container;
    }
    catch (HttpException ex)
    {
        Helpers.Error.Functions.RecordError(ex);
        throw new HttpException(500, "Could not connect.");
    }
}

GetClient()功能:

private static CloudBlobClient GetClient()
{
    try
    {
        var account = CloudStorageAccount.Parse(Settings.Deployment.AzureConnectionString);
        return account.CreateCloudBlobClient();
    }
    catch (Exception ex)
    {
        Helpers.Error.Functions.RecordError(ex);
        throw new HttpException(500, "Could not connect GetClient.");
    }
}

堆栈追踪:

  

[SocketException(0x274d):无法建立连接,因为   目标机器主动拒绝它127.0.0.1:10000]
  System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,   SocketAddress socketAddress)+520
  System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,   插座s4,插座s6,插座&amp; socket,IPAddress&amp;地址,   ConnectSocketState状态,IAsyncResult asyncResult,Exception&amp;   例外)+621

     

[WebException:无法连接到远程服务器]
  System.Net.HttpWebRequest.GetResponse()+1724
  Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync(RESTCommand`1   cmd,IRetryPolicy策略,OperationContext operationContext)in   c:\ Program Files   (86)\詹金斯\工作空间\ release_dotnet_master \ LIB \ ClassLibraryCommon \核心\执行人\ Executor.cs:699

     

[StorageException:无法连接到远程服务器]
  Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync(RESTCommand`1   cmd,IRetryPolicy策略,OperationContext operationContext)in   c:\ Program Files   (86)\詹金斯\工作空间\ release_dotnet_master \ LIB \ ClassLibraryCommon \核心\执行人\ Executor.cs:604   Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.CreateIfNotExists(BlobContainerPublicAccessType   accessType,BlobRequestOptions requestOptions,OperationContext   operationContext)在c:\ Program Files中   (86)\詹金斯\工作空间\ release_dotnet_master \ LIB \ ClassLibraryCommon \斑点\ CloudBlobContainer.cs:233   C3.Code.Controls.Application.Storage.Blob.Blob.GetContainer(字符串   containerName)in ... Blob.cs:104
  C3.Code.Controls.Application.Storage.Blob.Blob.AddBlob(字符串   D:\ Dropbox中的containerName,String blobReference,Byte [] bytes)   (Scirra Ltd)\ Tom \ C3   网站\ C3Alpha2 \代码\控制\应用\存储\斑点\ Blob.cs:87
  C:.Code.Callbacks.Application.ApplicationStart()在D:\ Dropbox中(Scirra   有限公司)\ Tom \ C3网站\ C3Alpha2 \ Code \ Callbacks \ Application.cs:26
  D:\ Dropbox中的C3.Global.Application_Start(Object sender,EventArgs e)   (Scirra Ltd)\ Tom \ C3 Website \ C3Alpha2 \ Global.asax.cs:30

     

[HttpException(0x80004005):无法连接到远程服务器]
  System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的   上下文,HttpApplication app)+534
  System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的   appContext,HttpContext上下文,MethodInfo []处理程序)+186
  System.Web.HttpApplication.InitSpecial(HttpApplicationState状态,   MethodInfo [] handlers,IntPtr appContext,HttpContext context)+175
  System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr的   appContext,HttpContext context)+424
  System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr的   appContext)+360

     

[HttpException(0x80004005):无法连接到远程服务器]
  System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+539
  System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)   +118 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest)   wr,HttpContext context)+730

1 个答案:

答案 0 :(得分:2)

来自您发布的异常消息:

  

[ SocketException (0x274d):无法建立连接,因为目标计算机主动拒绝了它127.0.0.1:10000]

请注意,这是SocketException,而不是HttpException,但您的代码说明了这一点:

catch (HttpException ex)

所以它没有被抓住。使用通用Exception来捕获所有内容,或者切换到捕获SocketException