• WCFhow to write a WCF client for a JSON REST service


    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

    Service interface

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

    Service data structures

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

    Service implementation

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

    Service markup

    <%@ 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>
    

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

    Have fun.

    shareimprove this answer

  • 相关阅读:
    PHP输出中文乱码的问题(转)
    phpmyadmin导出数据库为什么是php文件
    phpmyadmin登陆提示#2002 无法登录 MySQL 服务器和设置自增
    phpMyAdmin配置及 错误 缺少 mysqli 扩展。请检查 PHP 配置
    利用eclipse开发php<转>
    apache 2.4 You don't have permission to access / on this server
    (转)如果“打开方式”里面没有想要的打开方式,怎样创建一种文件打开方式?
    (转)安装 Apache 出现 <OS 10013> 以一种访问权限不允许的方式做了一个访问套接字的尝试
    关于ISAPI和CGI限制,这个要设为允许
    Sqlserver数据库日志太大如何快速删除
  • 原文地址:https://www.cnblogs.com/grj001/p/12223751.html
Copyright © 2020-2023  润新知