0

I have an existing WPF client, and I am building new standalone WebServices using ASP.NET 5 WebAPI. I want to expose some metadata, like WebApiProxy or .wsdl/Mex, so I can auto generate a Proxy class in my WPF client.

jpgrassi
  • 5,482
  • 2
  • 36
  • 55
jenspo
  • 504
  • 4
  • 11

2 Answers2

2

You don't need to create proxy's in your WPF client to communicate with your WEB API. In a simplest scenario, just use an HTTP Client to call your Web API endpoint:

Something like this would do:

// This should come from a factory or something. 
// Try to reuse as much as possible the HttpClient instance
var client = new HttpClient();

//Api Base address
client.BaseAddress = new Uri("http://localhost:9000/");

client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

//Sending a GET request to endpoint api/products/1
HttpResponseMessage response = await client.GetAsync("api/person/1");
if (response.IsSuccessStatusCode)
{
    //Getting the result and mapping to a Product object
    Person person = await response.Content.ReadAsAsync<Person>();
}

Edit: I'm editing the original answer because I wrote it using the HtppClient around a using statement which is very bad. To avoid people copy 'n pasting this into their solution and helping to propagate bad software out there I decided to modify it.

jpgrassi
  • 5,482
  • 2
  • 36
  • 55
  • Without the metadata from the serverside. Where does "Person" in your example come from? I want the metadata so I can define my types serverside. Otherwise "Person" has to be maintained on all clients when changed serverside. I prefer auto-geneated types for this. – jenspo Jan 08 '16 at 12:29
  • Well this is sort of a downside. I have no knowledge of how could you do it. Think about public API's. I've never seem a REST API exposing in any way its entities. What you are looking for is a documentation exposing your DTO's so your clients know what to expect. Another approach is build an SDK where you expose the classes your API uses. Either way, you'll end up having a duplication. – jpgrassi Jan 08 '16 at 12:38
  • If you are interested to see an example that does this with REST (kinda) , take a look https://github.com/faniereynders/WebApiProxy. It just does not work with the new ASP.NET 5 WebAPI - yet! It IS offcourse duplication. But since the clientside "clone" is auto-generated from serverside metadata, there is no maintaince. – jenspo Jan 08 '16 at 13:47
  • This looks helpful but I wouldn't rely on it in a robust API scenario. I think you would be in a better "standard" if you expose a good documentation or SDK to developers. But if it's an "inside" usage API, that would do the job. – jpgrassi Jan 08 '16 at 15:32
  • Which puts you in the nice Rest bind share code and be strongly coupled or be untyped .. Implementing from a document is crap for a large api (eg 200 structures/ messages which gets updated say 4-5 times a year) fine for a trivial one. – user1496062 Mar 24 '16 at 11:07
-1

You don't need create Proxy classes like Legacy Web Services instead you can directly access Web API using Endpoints/Url of your Web Api (as you configured it's route in WebApiConfig.cs class) To Access those Endpoints you can use HttpClient object , you dont need to configured in your client application

Hardik
  • 228
  • 1
  • 6