zoukankan      html  css  js  c++  java
  • 60秒创建JSON WCF RESTful服务

    1、先配置App.Config文件:

     <?xml version="1.0"?>

    <configuration>
      <system.serviceModel>
    <services>
     <service name="WcfJsonRestService.Service1">
    <endpoint address="http://localhost:8732/service1" 
     binding="webHttpBinding" 
     contract="WcfJsonRestService.IService1"/>
     </service>
    </services>
    <behaviors>
     <endpointBehaviors>
    <behavior>
     <webHttp />
    </behavior>
     </endpointBehaviors>
    </behaviors>
      </system.serviceModel>
      <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
    </configuration>

    2、接口定义:

     using System.ServiceModel;

    namespace WcfJsonRestService
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            Person GetData(string id);
        }
    }
     3、接口实现:

    using System;
    using System.ServiceModel.Web;

    namespace WcfJsonRestService
    {
        public class Service1 : IService1
        {
            [WebInvoke(Method = "GET", 
                        ResponseFormat = WebMessageFormat.Json, 
                        UriTemplate = "data/{id}")]
            public Person GetData(string id)
            {
                // lookup person with the requested id 
                return new Person()
                           {
                               Id = Convert.ToInt32(id), 
                               Name = "Leo Messi"
                           };
            }
        }

        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }

     4、调试--启用实例,并通过浏览器访问http://localhost:8732/service1/data/10 即可看到运行效果!

  • 相关阅读:
    qt程序编译错误:could not exec ‘/usr/lib/x86_64-linux-gnu/qt4/bin/qmake’
    安装 yaml-cpp,MP4V2
    安装cmake 和 opencv 4.0.0
    windows系统,boost编译安装
    messageQ 消息队列
    fflush 和 fsync 的区别
    开源一个 PDF 小工具集软件【使用 PDFium 库实现】
    封装 libjpeg 库
    纯 C++ 代码实现的 INI 文件读写类
    C++11 —— 使用 thread 实现线程池
  • 原文地址:https://www.cnblogs.com/liyanggzy/p/1985529.html
Copyright © 2011-2022 走看看