Connecting a WCF service to an external API

2.6k views Asked by At

I need to connect a WCF service to an external WCF service via an API which is returning data in the JSON format.

I have been looking at wsdl and wadl to achieve this but I am not sure whether they were implemented on the external service or how to go about accessing them.

<serviceMetadata> has been enabled on the external service.

From what I have seen so far wsdl seems to be outdated and only compatible with SOAP, does that sound right? So this being correct, I would naturally prefer to use wadl.

Are these my only options and if so are there any good guides that walk through how to implement these?

Thanks.

1

There are 1 answers

5
Tim On BEST ANSWER

This is based on a stripped down version of something I implemented at work and modified (but not tested) to work with JSON (based on some other answers here and on the web):

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

HttpResponseMessage response = client.GetAsync("http://somedomain.com/serviceAddress").Result;

string responseContent = response.Content.ReadAsStringAsync().Result;

There are numerous ways to do this, but the above code demonstrates how easy it is (or should be, at least) to do this.

Note that I used the Result property for the async calls; if you're making this call from within a method marked as async, you could also do:

HttpResponseMessage response = await client.GetAsync("http://somedomain.com/serviceAddress");

string responseContent = await response.Content.ReadAsStringAsync();

HttpClient is in the System.Net.Http namespace.