[英]How to consume a json wcf service in C#
我有一个json wcf。 地址已给出。 wsdl中的代码具有以下内容:
<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>
如何在C#中使用它? 可以吗
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();
}
}
我需要将网址放在客户端吗? 该服务有两个DataContract和多个DataMember。 我对此不强。
谢谢。
您实际上没有JSON WCF服务。 您所拥有的服务可能有也可能没有可以接收/发送JSON数据的终结点。 您正在显示的WSDL列出了一个端点( wsdl:port
),该端点使用基于SOAP的绑定( BasicHttpBinding
)。 可以“交谈” JSON的端点是使用WebHttpBinding
定义的,并且WebHttpBehavior
应用了一种特定的行为( WebHttpBehavior
)-并且它们未显示在WSDL中 。
因此,您不能使用由“添加服务参考”或svcutil.exe之类的工具生成的客户端使用它。 如果客户端代码中具有相同的合同,则可以使用ChannelFactory<T>
或WebChannelFactory<T>
创建与服务进行对话的代理,也可以使用以下方法手工创建请求并将其发送给服务通用HTTP客户端。
下面的示例代码显示了如何使用WebChannelFactory<T>
和“常规” HTTP客户端( WebClient
)来使用JSON端点。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.