문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top