I have an ASP.Net API i've created. It accesses a database, which I have to use a VPN to connect with. The API will work just fine while doing this.
I then created a windows service which receives a JSON from this API. In testing, I have used it as a console application with a method mentioned here: https://social.technet.microsoft.com/wiki/contents/articles/30957.c-windows-service-in-console-mode-for-debug-and-admin.aspx
When testing this way, everything works fine and as expected. However, if I build the service in release mode and install, it will not connect with the API. I believe this is likely because it's running at an OS level and isn't being affected by the VPN? I'm just unsure of how to get around this.
Below is how I am connecting and receiving the JSON from the API.
var client = new HttpClient();
client.BaseAddress = new Uri(URL);
client.DefaultRequestHeaders.Add("User-Agent", "Anything");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"))
//Initializes a temp list to be returned
List<Company> temp2 = new List<Company>();
var task = client.GetAsync(path);
task.Wait();
var response = task.Result;
if (response.IsSuccessStatusCode)
{
var temp = response.Content.ReadAsStringAsync().Result;
temp2 = JsonConvert.DeserializeObject<List<Company>>(temp);
}
I briefly read about DefaultProxy but didn't quite understand it's use and if it's what I need in this case.
I discovered the issue. Thanks to @fildor for suggesting I use await instead. This didn't solve the issue but caused it to finally give me an error I could work with.
What was happening is the API domain didn't use a valid SSL/TLS certificate. Now, using:
var handler = new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = delegate { return true; },
};
var client = new HttpClient(handler);
and everything works as intended.