客户端配置为使用WCF JSON Web服务客户端、WCF、Web、JSON

2023-09-02 11:49:46 作者:袖手今生

我已经配置了Web服务使用JSON作为描述在这个博客上:http://www.west-wind.com/weblog/posts/164419.aspx和其他各种博客,但我不能创建一个客户机来使用这项服务。我尝试过各种东西,但总是我毫无意义的异常。什么是执行(WCF我要补充)正确的做法客户端?

I have configured the web service to use Json as described on this blog: http://www.west-wind.com/weblog/posts/164419.aspx and various other blogs, but I couldn't create a client to consume this service. I tried various things, but invariably I got meaningless exceptions. What is the correct way to implement the (WCF I should add) client?

推荐答案

似乎有关于如何写一个WCF客户端的JSON REST服务的例子短缺。每个人似乎都使用WCF实现该服务,但几乎没有写一个客户端。因此,这里的服务(实现GET和POST请求)和客户端的一个比较完整的例子。

There seems to be a shortage of examples about how to write a WCF client for a JSON REST service. Everybody seems to use WCF for implementing the service but hardly ever for writing a client. So here's a rather complete example of the service (implementing a GET and a POST request) and the client.

Service接口

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/getcar/{id}")]
    Car GetCar(string id);

    [OperationContract]
    [WebInvoke(RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/updatecar/{id}")]
    Car UpdateCar(string id, Car car);
}

服务数据结构

[DataContract]
public class Car
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string Make { get; set; }
}

服务实施

public class Service1 : IService1
{
    public Car GetCar(string id)
    {
        return new Car { ID = int.Parse(id), Make = "Porsche" };
    }

    public Car UpdateCar(string f, Car car)
    {
        return car;
    }
}

服务标记

<%@ ServiceHost Language="C#" Service="JSONService.Service1"
    CodeBehind="Service1.svc.cs"
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

的Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>   
</configuration>

客户端

而现在的客户端。它重用接口 IService1 和类。此外,以下code和配置是必需的。

Client

And now the client. It reuses the interface IService1 and the class Car. In addition, the following code and configuration is required.

的App.config

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webby">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint address="http://localhost:57211/Service1.svc" name="Service1" binding="webHttpBinding" contract="JSONService.IService1" behaviorConfiguration="webby"/>
    </client>
  </system.serviceModel>
</configuration>

的Program.cs

public class Service1Client : ClientBase<IService1>, IService1
{
    public Car GetCar(string id)
    {
        return base.Channel.GetCar(id);
    }


    public Car UpdateCar(string id, Car car)
    {
        return base.Channel.UpdateCar(id, car);
    }
}


class Program
{
    static void Main(string[] args)
    {
        Service1Client client = new Service1Client();
        Car car = client.GetCar("1");
        car.Make = "Ferrari";
        car = client.UpdateCar("1", car);
    }
}

玩得开心。