现在的位置: 首页 > 综合 > 正文

简单五步创建一个WCF Rest Service

2018年01月25日 ⁄ 综合 ⁄ 共 2751字 ⁄ 字号 评论关闭

1、创建一个WCF Service Application

2、创建一个实体对象Student,用作数据传输的载体,下面是Student.cs的内容

using System.Runtime.Serialization;
namespace WCFRest
{
    /// <summary>
    /// DataContract 数据契约:服务端和客户端之间要传送的自定义数据类型
    /// </summary>
    [DataContract]
    public class Student
    {
        /// <summary>
        /// 在数据传送过程中,只有成员变量可以被传送而成员方法不可以。
        /// 并且只有当成员变量加上DataMember时才可以被序列进行数据传输,
        /// 如果不加DataMember,客户端将无法获得该属性的任何信息
        /// </summary>
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string Name { get; set; }
    }
}

同时我们创建一个类,用来模拟数据库的存储

using System.Collections.Generic;

namespace WCFRest
{
    public class UserList
    {
        private static readonly UserList _Instance = new UserList();
        private UserList() { }

        public static UserList Instance
        {
            get { return _Instance; }
        }

        public IList<Student> Users
        {
            get { return _Users; }
        }

        private IList<Student> _Users = new List<Student>{
            new Student {Id = 1, Name = "张三" },
            new Student {Id = 2, Name = "李四" },
            new Student {Id = 3, Name = "王五" }
        };
    }
}

3、创建服务契约

下面我们在项目添加一个WCF Service

首先修改IStudnetService接口,配置Rest的URL路径

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace WCFRest
{
    [ServiceContract]
    public interface IStudentService
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "GetStudentById/Id={Id}"
         )]
        Student GetStudentById(string Id);

        [OperationContract]
        [WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "GetStudentList"
         )]
        IList<Student> GetStudentList();
    }
}

4、修改StudentService类,实现Rest方法

using System.Collections.Generic;

namespace WCFRest
{
    public class StudentService : IStudentService
    {
        
        public Student GetStudentById(string Id)
        {
            return StudentList.Instance.Users[int.Parse(Id)];
        }

        public IList<Student> GetStudentList()
        {
            return StudentList.Instance.Users;
        }
    }
}

5、配置Service和Behavior

在Web.Config中配置我们的Rest服务

 <system.serviceModel>
    <services>
      <service name="WCFRest.StudentService" behaviorConfiguration="serviceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="WCFRest.IStudentService" 
                  behaviorConfiguration="web"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

OK,下面测试一下我们的程序,右击Student.svc文件,选择View In Browser,我们将会看到下面的运行结果

然后我们在url后面加上我们定义的Rest地址就可以访问Rest服务了

说明:本文绝大部分内容摘自下面文章

http://www.topwcftutorials.net/2013/09/simple-steps-for-restful-service.html

抱歉!评论已关闭.