zoukankan      html  css  js  c++  java
  • WCF REST服务开发

    1、新建项目,打开VS2010,WCF/WCF服务库

    2、确保引用System.ServiceModel.Web,.net 框架不要选择client profile

    3、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>

    4、IService1.cs

    [ServiceContract]
    public interface IService1
    {
    [OperationContract]
    Person GetData(string id);
    }

    5、Service1.cs

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

    6、F5执行,在浏览器输入

    http://localhost:8732/Service1/data/10

    {"Id":10,"Name":"Leo Messi"}
  • 相关阅读:
    服务器建设问题
    JDBC --反射(二)
    Cookies
    http和https区别
    springboot常用注解
    线程池
    悲观锁和乐观锁
    java高并发下的数据安全
    idea解决mybatis逆向工程
    spring Cloud
  • 原文地址:https://www.cnblogs.com/yanzhenan/p/3124074.html
Copyright © 2011-2022 走看看