I currently have a WPF application with a GUI, which essentially uses the ASP.NET web API backend to make web requests. I have implemented multithreading and am using the RestSharp library to deal with HTTP requests. The ASP.NET Project has a controller which interacts with a database server to return a searched item from the database. The controller looks like this:
public class SearchController : ApiController
{
DataServerInterface foob;
public SearchController()
{
ChannelFactory<DataServerInterface> foobFactory;
NetTcpBinding tcp = new NetTcpBinding();
string URL = "net.tcp://localhost:8100/DataService";
foobFactory = new ChannelFactory<DataServerInterface>(tcp, URL);
foob = foobFactory.CreateChannel();
}
[System.Web.Http.Route("api/search/{name}")]
[System.Web.Http.HttpGet]
public DataIntermed Search(string name)
{
DataIntermed intermed = new DataIntermed(); ;
for (int i =0; i<foob.GetNumEntries(); i++)
{
foob.GetValuesForEntry(i, out uint inAccNo, out uint inPin, out int inBal, out string infName, out string inlName, out Bitmap inIcon);
if (infName.Contains(name))
{
intermed.fName = infName;
intermed.accNo = inAccNo;
intermed.pin = inPin;
intermed.bal = inBal;
intermed.lName = inlName;
intermed.image = inIcon;
break;
}
return intermed;
}
}
}
The WPF app is simply just a GUI so the user doesn't have to manually call the service controller in the URL of the web API. The user types the name to be searched in a textbox, then presses a button which executes the web controller service. The web API must also be running before the WPF application can be started. I have implemented it as follows:
public partial class MainWindow : Window
{
RestClient restClient;
SearchData search;
public MainWindow()
{
InitializeComponent();
string url = "http://localhost:57049/";
restClient = new RestClient(url);
RestRequest restRequest = new RestRequest("api/values");
RestResponse restResponse = restClient.Get(restRequest);
NumberOfItems.Text = restResponse.Content;
}
private async void SearchButton_Click(object sender, RoutedEventArgs e)
{
Task<DataIntermed> task = new Task<DataIntermed>(SearchDB);
search = new SearchData();
search.searchString = SearchNameBox.Text;
task.Start();
DataIntermed dataIntermed = await task;
UpdateGUI(dataIntermed);
}
private DataIntermed SearchDB()
{
RestRequest restRequest = new RestRequest("api/search/");
restRequest.AddJsonBody(search);
RestResponse restResponse = restClient.Post(restRequest);
DataIntermed dataIntermed = JsonConvert.DeserializeObject<DataIntermed>(resp.Content);
return dataIntermed;
}
My issue is that the POST Request in the 'SearchDB' function of the WPF app always seems to return null. When I manually call the search service in the URL of the web API, it correctly returns the searched object (for example I type the URL 'http://localhost:57049/api/search/[name]'). However, when searching from the WPF app, the returned object is null. I also have a separate controller which uses GET to return a particular index of the database (for example the first or second element in the database), and that works fine both in the web API and the WPF application. But when using POST, the WPF does not work properly. I have tried setting the attribute of the SearchController to [HTTPPost], but when I do this and call the search controller from the URL of the web API, I get an error message saying "The requested resource does not support http method 'GET'".
Does anyone know how I could retrieve the searched data in the WPF GUI using POST? Thanks