Asp.net web api Microsoft.AspNet.WebApi.Client IsSuccessStatusCode不工作
环境: .net 4.0控制台应用程序Asp.net web api Microsoft.AspNet.WebApi.Client IsSuccessStatusCode不工作,asp.net-web-api,Asp.net Web Api,环境: .net 4.0控制台应用程序 Microsoft.AspNet.WebApi.Client version=4.0.30506.0 targetFramework="net40" Microsoft.Net.Http" version=2.0.20710.0 targetFramework="net40" Newtonsoft.Json version=4.5.11 targetFramework="net40" 好的,我已经建立了一个自托管的WebAPI服务,我可以毫无问题
Microsoft.AspNet.WebApi.Client version=4.0.30506.0 targetFramework="net40"
Microsoft.Net.Http" version=2.0.20710.0 targetFramework="net40"
Newtonsoft.Json version=4.5.11 targetFramework="net40"
好的,我已经建立了一个自托管的WebAPI服务,我可以毫无问题地访问该服务。
我想优雅地处理服务未运行的场景
问题是GetAsync调用在检查IsSuccessStatusCode之前就出现了代码错误
然而,如果我启动Fiddler,那么代码就会按照我的预期运行,这对我来说真的很奇怪
下面是一些演示代码
Private Function GetConnInfo(hostIP As String, hostPort As Integer) As Boolean
GetConnInfo = False
Dim client As HttpClient = New HttpClient()
client.BaseAddress = New Uri("http://" & hostIP & ":" & hostPort)
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
Dim resp = client.GetAsync("api/clientconnectioninfo/GetAllConnections").Result
If resp.IsSuccessStatusCode Then
GetConnInfo = True
End If
Return GetConnInfo
End Function
堆栈跟踪:
System.AggregateException was unhandled
HResult=-2146233088
Message=One or more errors occurred.
Source=mscorlib
StackTrace:
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at DemoApiServiceNotRunning.Module1.GetConnInfo(String hostIP, Int32 hostPort) in C:\Users\tonye\Documents\Visual Studio 2012\Projects\DemoApiServiceNotRunning\DemoApiServiceNotRunning\Module1.vb:line 19
at DemoApiServiceNotRunning.Module1.Main() in C:\Users\tonye\Documents\Visual Studio 2012\Projects\DemoApiServiceNotRunning\DemoApiServiceNotRunning\Module1.vb:line 8
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
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
ErrorCode=10061
HResult=-2147467259
Message=No connection could be made because the target machine actively refused it 192.168.20.124:8700
NativeErrorCode=10061
Source=System
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:`
IsSuccessStatusCode仅在发出HTTP请求时返回值。如果它无法连接到服务器,您将得到异常抛出。它与fiddler一起运行,因为您的客户端实际上正在连接到fiddler,然后fiddler正在尝试连接到您的服务器
你有两个选择。捕获异常,或者安装另一个在同一端口上运行但使用弱通配符的自宿主web api。只有在主服务未运行时,第二个服务才会被命中。IsSuccessStatusCode仅在发出HTTP请求时才会返回值。如果它无法连接到服务器,您将得到异常抛出。它与fiddler一起运行,因为您的客户端实际上正在连接到fiddler,然后fiddler正在尝试连接到您的服务器
你有两个选择。捕获异常,或者安装另一个在同一端口上运行但使用弱通配符的自宿主web api。只有在主服务未运行时,第二个服务才会被命中。您说的“出错”是什么意思?您可以发布异常消息吗?我正在发布。
issucessstatuscode
用于检查响应是否在200-299范围内,您需要响应。因此,当服务器未运行时,您不能使用IsSuccessStatusCode
,您需要捕获聚合异常
,并对其进行检查……您所说的“出错”是什么意思?您可以发布异常消息吗?我正在发布。issucessstatuscode
用于检查响应是否在200-299范围内,您需要响应。因此,当服务器未运行时,您无法使用issucessstatuscode
,您需要捕获aggregateeexception
并对其进行检查……因此fiddler实际上正在生成http响应?@Wieknot-Yup。它的行为就像任何代理一样。那么fiddler实际上是在生成http响应吗?@Wieknot-Yup。就像任何代理一样。