Reputation:
I have a json wcf. The address is given. The code in wsdl has the following:
<wsdl:binding name="BasicHttpBinding_iBOER" type="tns:iBOER">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="PhoneCall">
<soap:operation soapAction="http://tempuri.org/iBOER/PhoneCall" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:service name="BOER">
- <wsdl:port name="BasicHttpBinding_iBOER" binding="tns:BasicHttpBinding_iBOER">
<soap:address location="http://wsvc01/BOER/BOER.svc" />
</wsdl:port>
</wsdl:service>
How to consume it in C#? Just is it okay?
class Test
{
static void Main()
{
iBOERClient client = new iBOERClient();
// Use the 'client' variable to call operations on the service.
// Always close the client.
client.Close();
}
}
Do I need put the url in client side? The service has two DataContract and many DataMember. I am not strong on it.
Thanks.
Upvotes: 1
Views: 5880
Reputation: 87308
You don't really have a JSON WCF service. You have a service which may or may not have an endpoint which can receive / send JSON data. The WSDL you're showing lists an endpoint (wsdl:port
) which uses a SOAP-based binding (BasicHttpBinding
). Endpoints which can "talk" JSON are defined with the WebHttpBinding
, and have one specific behavior (WebHttpBehavior
) applied to it - and they do not show up in the WSDL.
So, you cannot consume it with a client generated by a tool such as Add Service Reference or svcutil.exe. If you have the same contract in the client code, you can use a class such as ChannelFactory<T>
or WebChannelFactory<T>
to create a proxy to talk to the service, or you can handcraft the requests and send it to the service using a general-purpose HTTP client.
The sample code below shows how to consume a JSON endpoint with both the WebChannelFactory<T>
and a "normal" HTTP client (WebClient
).
public class StackOverflow_14945653
{
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public Address Address { get; set; }
}
[DataContract]
public class Address
{
[DataMember]
public string Street;
[DataMember]
public string City;
[DataMember]
public string Zip;
}
[ServiceContract]
public interface ITest
{
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void RegisterPerson(Person p);
[WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Person FindPerson(string name);
}
public class Service : ITest
{
private static List<Person> AllPeople = new List<Person>();
public void RegisterPerson(Person p)
{
AllPeople.Add(p);
}
public Person FindPerson(string name)
{
return AllPeople.Where(p => p.Name == name).FirstOrDefault();
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
Console.WriteLine("Accessing via WebChannelFactory<T>");
WebChannelFactory<ITest> factory = new WebChannelFactory<ITest>(new Uri(baseAddress));
ITest proxy = factory.CreateChannel();
proxy.RegisterPerson(new Person
{
Name = "John Doe",
Age = 32,
Address = new Address
{
City = "Springfield",
Street = "123 Main St",
Zip = "12345"
}
});
Console.WriteLine(proxy.FindPerson("John Doe").Age);
Console.WriteLine();
Console.WriteLine("Accessing via \"normal\" HTTP client");
string jsonInput = "{'Name':'Jane Roe','Age':30,'Address':{'Street':'1 Wall St','City':'Springfield','Zip':'12346'}}".Replace('\'', '\"');
WebClient c = new WebClient();
c.Headers[HttpRequestHeader.ContentType] = "application/json";
c.UploadString(baseAddress + "/RegisterPerson", jsonInput);
c = new WebClient();
Console.WriteLine(c.DownloadString(baseAddress + "/FindPerson?name=Jane Roe"));
Console.WriteLine();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
Upvotes: 7