I have one method in Windows Form App(.NET) where I make Rest call, deserialize the json in object and show the result in UI.
So I copied the same exact method from Windows Form App(.NET) to a new WPF App(.NET) but the Rest response time changed.
Here is the Rest response time in:
Windows Form App(.NET) in milliseconds
WPF App(.NET) in milliseconds
The method in Windows Form App(.NET):
private Stopwatch Stopwatch = new Stopwatch();
public Uri url = new Uri("http://xxx.xxx.xxx.xxx");
private readonly HttpClient _client;
//constructor
public Main()
{
_client = new HttpClient();
_client.BaseAddress = url;
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
_client.DefaultRequestHeaders.Accept.Add(contentType);
timer.Start();
timer.Interval = 1;
}
public void Timer_Tick(object sender, EventArgs e)
{
timer.Stop();
try
{
string JsonD= @"{""bal"":{""bla"":0},""data"":{""bla"":""bla""}}";
_stopwatch.Restart();
//JsonD is string fot HttpContent
var contentData = new StringContent(JsonD);
using (var responseMessage = _client.PostAsync("/xxx/xxx/xxx", contentData).Result)
{
if (responseMessage.IsSuccessStatusCode)
{
string strContext = responseMessage.Content.ReadAsStringAsync().Result;
var result= System.Text.Json.JsonSerializer.Deserialize<Dis>(strContext);
}
}
_stopwatch.Stop();
txbRestResp.Text = _stopwatch.ElapsedMilliseconds.ToString();
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace.ToString() + " - " + ex.Message);
}
timer.Start();
}
The method in WPF App(.NET):
private Stopwatch Stopwatch = new Stopwatch();
public MainWindow()
{
InitializeComponent();
BgWorker = new BackgroundWorker { WorkerReportsProgress = true };
BgWorker.DoWork += ResetAll;
}
private void ResetAll(object sender, DoWorkEventArgs e)
{
while (true)
{
_stopwatch.Restart();
Display();
_stopwatch.Stop();
lblRestResponse.Content = _stopwatch.ElapsedMilliseconds.ToString();
}
}
private void Display()
{
string JsonD= @"{""bal"":{""bla"":0},""data"":{""bla"":""bla""}}";
string BaseUrl = "http://xxx.xxx.xxx.xxx";
StringContent httpContentDistanza = new StringContent(JsonD);
using var client = new HttpClient { BaseAddress = new Uri(BaseUrl) };
using var responseMessage = client.PostAsync("/xxx/xxx/xxx", httpContentDistanza).Result;
if (responseMessage.IsSuccessStatusCode)
{
string strContext = responseMessage.Content.ReadAsStringAsync().Result;
var result = System.Text.Json.JsonSerializer.Deserialize<Dis>(strContext);
}
}
Why is there so much difference between the response ? Right now I'm working in WPF App(.NET) and I need to obtain the same Rest response like in Windows Form App(.NET). I was expecting that WPF should be better.
Am I doing somthing wrong?
Any suggestions/improvements?