使用 WCF JSON Web 服务的客户端配置 [英] Client configuration to consume WCF JSON web service

查看:30
本文介绍了使用 WCF JSON Web 服务的客户端配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 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?

推荐答案

关于如何为 JSON REST 服务编写 WCF 客户端的示例似乎很缺乏.似乎每个人都使用 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.

服务接口

[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 和类Car.另外需要如下代码和配置.

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);
    }
}

玩得开心.

这篇关于使用 WCF JSON Web 服务的客户端配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆