在阅读Stack Overflow上的无数帖子之后,我觉得把它扔到那里看是否有人可以帮助我会伤害我...
我发布了一个ASP.NET Web.API。我还有一个在互联网连接非常慢的船上发布的Windows服务(通常都没有)。
该服务在计时器上运行,一旦计时器过去,就开始验证它是否能够连接到我的API。
// Verify the remote connection via the RAC Sync Service
public bool CheckConnection()
{
//using (HttpClient c = new HttpClient())
//{
HttpClient c = new HttpClient();
log.Debug("Verifying the remote connection");
c.Timeout = new TimeSpan(0, 0, 10, 0);
c.BaseAddress = new Uri(ConfigurationManager.AppSettings["RACAPIAddress"]);
var userID = Int32.Parse(ConfigurationManager.AppSettings["userID"]);
//log.Debug("Attempting to connect using " + ".../api/RAC/CheckConnection?userID=" + userID);
//var result = c.GetAsync(String.Format(@"/api/RAC/CheckConnection?userID={0}", userID)).Result;
//log.Debug("Finished the attempt");
//log.Debug("Reading the string");
//var content = result.Content.ReadAsStringAsync().Result;
var httpRequest = new HttpRequestMessage();
httpRequest.Version = HttpVersion.Version10;
httpRequest.RequestUri = new Uri(ConfigurationManager.AppSettings["RACAPIAddress"] + "/api/RAC/CheckConnection?userID=" + userID);
httpRequest.Headers.Add("Connection", new[] { "Keep-Alive" });
var result = c.SendAsync(httpRequest).Result;
var content = result.Content.ReadAsStringAsync().Result;
c.Dispose();
if (result.IsSuccessStatusCode)
{
log.Debug("Received a status success code");
var response = JsonDeserialize<string>(content);
log.Debug(response);
return true;
}
else
{
log.Debug("Received Status Code: " + result.StatusCode.ToString());
var error = result.Headers.FirstOrDefault(x => x.Key == "Error").Value.FirstOrDefault();
throw new Exception("CheckConnection() failed with message " + error);
}
//}
}
当我使用远程桌面连接查看我看到的日志时:
2015-04-01 17:02:44,546 [4] DEBUG Logger Sync Service started
2015-04-01 17:02:44,562 [4] DEBUG Logger The sleep time is set to expire every 5 seconds
2015-04-01 17:02:49,578 [4] DEBUG Logger Sleep time expired
2015-04-01 17:02:49,593 [4] DEBUG Logger Verifying the remote connection
2015-04-01 17:02:51,234 [4] ERROR Logger One or more errors occurred.
2015-04-01 17:02:51,250 [4] ERROR Logger System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
at System.Net.PooledStream.EndRead(IAsyncResult asyncResult)
at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task`1.get_Result()
at RAC.SyncService.SyncService.CheckConnection()
at RAC.SyncService.SyncService.TimerElapsed(Object sender, ElapsedEventArgs args)
---> (Inner Exception #0) System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
at System.Net.PooledStream.EndRead(IAsyncResult asyncResult)
at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---<---
奇怪的是,如果我尝试在远程处理(使用浏览器)时调用CheckConnection功能,一切看起来都很正常。该船具有Internet连接并返回响应。那里有什么想法吗?
我已尝试过(来自其他帖子):
- Adding logging (see above)
- Removing my using block and calling dispose instead
- Using a webRequest as opposed to httpClient
- Adding HttpVersion.Version10
- Including "Keep-Alive"
- Maxing the timeouts and file sizes both on the client and server
是否有防火墙阻止我的通话?如果是这样,我为什么能够使用浏览器呢?
对此的任何帮助都将非常感激。