Introduction
For Support Desk, I used Visual Studio 2015 ASP.NET Web API to host a help desk server. It will allow authenticated users to create, edit, and search support tickets, customers, and solutions. The client is in WPF, but any client using HTTP can access the server through Web API allowing the customer to easily customize their client.
Day One
On day one, we started a new solution containing an ASP.NET Web Application project. We examined the "Controllers" and "Models" folders. Then, we made minor changes to the code behind so we can do a simple proof of concept exchange with the client that we can expand on later. You can find the day one article here http://www.codeproject.com/Tips/1059338/Support-Desk-Day-of-n and download the completed project from CodePlex.com.
Day Two
On day two, we will open a separate Visual Studio solution for our WPF client. We'll make changes to the code behind for the MainWindow
by adding a single method. Our method will do a Get
request on our ValuesController
"api/values/" which will return our string
"Hello World!
" from the server.
Download the completed day two source code from CodePlex.
Create the Client
Open a second copy of Visual Studio. Then File>>New>>Project. Select Visual C# then WPF Application. Set the Name to "Support Desk Client". Click OK.
In Solution Explorer, right click on the project and select "Manage NuGet Packages".
Install the Newtonsoft.Json
NuGet package. We need this package to use "JsonConvert
" to Serialize and Deserialize our objects returned from the server.
Change the code in the MainWindow.xaml.cs file to the following:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Windows;
namespace Support_Desk_Client
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
GetRunAsync().Wait();
}
async Task GetRunAsync()
{
using (var client = new HttpClient())
{
string StringResponce;
client.BaseAddress = new Uri("http://localhost:57975/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add
(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync("api/Values/").Result;
if (response.IsSuccessStatusCode)
{
var GetString = await response.Content.ReadAsStringAsync();
StringResponce = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(GetString);
if (StringResponce != null)
{
MessageBox.Show(StringResponce);
}
}
}
}
}
}
GetRunAsync()
calls are method from the constructor as MainWindow
is loading. The Wait()
method signifies for the compiler to wait for our GetRunAsync()
method to complete. Communication with the server could be delayed or disrupted so declare GetRunAsync()
as async
. The whole exchange is wrapped in a using
statement to ensure the connection to the server will be closed when we've finished.
Next, we declare a new instance of HttpClient
, client. Then set its BaseAddress
to the Project Url from Support Desk day 1 "http://localhost:57975/". The next two lines are to request the json format from the server. Then declare response
equal to the result of our get
call to the server GetAsync("api/Values/")
. If we go to the server project and look at the ValuesController
will see that a Get
call returns the simple string
"Hello World!
"
public string Get()
{
return "Hello World!";
}
If the response is successful, the var
GetString
is set equal to the Content of response
read as a string
and then deserialized as object
type string
.
StringResponce = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(GetString)
If HttpClient
or MediaTypeWithQualityHeaderValue
has red squiggly lines under, then you left out the using
statements for System.Net.Http
and System.Net.Http.Headers
. Newtonsoft.Json.JsonConvert
requires the Newtonsoft.Json
NuGet package.
Passing a simple string
back and forth doesn't provide much functionality to our program, but later will pass our own custom objects. Now go back to the server project and run it. Give it enough time to load and come back to our client project and compile and run, and as you can see, we had a successful exchange with the server.
That's it for day two. Over the next few days, we will add the support tickets and customers to the server and use a bearer token on our client for authentication. http://www.codeproject.com/Tips/1065769/Support-Desk-Day-of-n.