利用WCF创建简单的Restful Service

使用工具:VS2013

第一步:创建一个WCF的工程

利用WCF创建简单的Restful Service_第1张图片

第二步:新建一个User.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace Test_One
{
    [DataContract]
    public class User
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string Email { get; set; }
    }
}

第三步:创建接口(IUserService.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Test_One
{
    [ServiceContract]
    public interface IUser
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "getuser/{name}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        User GetUserData(string name);

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "checkuser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        User CheckUserData(User user);
    }
}

第四步:实现接口(UserService.svc.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Test_One
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class UserService : IUserService
    {
        public User GetUserData(string name)
        {
            User user = new User();
            user.Name = name;
            user.Email = "[email protected]";
            return user;
        }

        public User CheckUserData(User user)
        {
            user.Name += "-test";
            return user;
        }
    }
}

第四步 修改配置文件




  
    
  
  
    
    
  
  
    
      
        
      
    
    
      
        
          
          
        

        
          
          
          
          
        
      
      
        
          
        
      
    
    
      
    
    
  
  
    
    
    
    
      
    
    
      
    
  


第五步:部署(应用程序池中给为4.0)

利用WCF创建简单的Restful Service_第2张图片

第六步:测试接口

利用WCF创建简单的Restful Service_第3张图片

学习网址: http://www.cnblogs.com/mingmingruyuedlut/p/4223116.html
利用WCF创建简单的Restful Service_第4张图片
公众号.png

你可能感兴趣的:(利用WCF创建简单的Restful Service)