【问题标题】:How to consume a json wcf service in C#如何在 C# 中使用 json wcf 服务
【发布时间】:2013-02-19 07:33:29
【问题描述】:

我有一个 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。我对此并不强。

谢谢。

【问题讨论】:

    标签: c# json wcf


    【解决方案1】:

    您实际上并没有 JSON WCF 服务。您有一个服务,它可能有也可能没有可以接收/发送 JSON 数据的端点。您显示的 WSDL 列出了使用基于 SOAP 的绑定 (BasicHttpBinding) 的端点 (wsdl:port)。可以“交谈”JSON 的端点是用 WebHttpBinding 定义的,并且应用了一种特定的行为 (WebHttpBehavior) - 它们是 do not show up in the WSDL

    因此,您不能通过添加服务引用或 svcutil.exe 等工具生成的客户端使用它。如果您在客户端代码中具有相同的合同,您可以使用诸如ChannelFactory&lt;T&gt;WebChannelFactory&lt;T&gt; 之类的类来创建与服务通信的代理,或者您可以手工处理请求并使用通用的方法将其发送到服务- 专用 HTTP 客户端。

    下面的示例代码显示了如何使用WebChannelFactory&lt;T&gt; 和“普通” 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();
        }
    }
    

    【讨论】:

    • 也许我错了,我的同事说这是一个 wcf json。但显然它可能不是那样的。我对他的错误陈述感到困惑。那么有没有示例代码?
    • 您应该确认您拥有什么,并可能向拥有该服务的人索取一些示例代码。
    • 代码很有帮助。还有一个问题,该服务不是自托管的。如何接近服务? intelisense 无法识别 ITest。
    • 需要将接口从服务复制到客户端。
    • 假设我无法访问接口(由第三方开发),我们只是从他们的服务器上使用它。我们知道 url,那么这个想法是什么?获取端点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多