当前位置:编程学习 > 网站相关 >>

化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)

作者:webabcd


介绍
WCF(Windows Communication Foundation) - Web编程模型:使用WCF创建REST服务,使用asp.net ajax调用WCF服务
    ·System.ServiceModel.Activation.WebServiceHostFactory - 用于承载使用 WCF Web 编程模型的服务
    ·System.ServiceModel.Activation.WebScriptServiceHostFactory  - 能够向服务中自动添加 ASP.NET AJAX 终结点而无需进行配置


示例(使用WCF创建REST服务)
1、服务
User.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.Web
{
    /**//// <summary>
    /// User实体类
    /// </summary>
    [DataContract]
    public class User
    {
        /**//// <summary>
        /// 用户名
        /// </summary>
        [DataMember(Order = 0)]
        public string Name { get; set; }

        /**//// <summary>
        /// 生日
        /// </summary>
        [DataMember(Order = 1)]
        public DateTime DayOfbirth { get; set; }
    }

}


IREST.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Web;

namespace WCF.ServiceLib.Web
{
    /**//// <summary>
    /// 演示REST(Representational State Transfer)的接口
    /// </summary>
    /// <remarks>
    /// HTTP方法中:
    /// PUT相当于Create
    /// GET相当于Read
    /// POST相当于Update
    /// DELETE相当于Delete
    /// </remarks>
    [ServiceContract]
    public inte易做图ce IREST
    {
        /**//// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="dayOfbirth">生日</param>
        /// <remarks>
        /// WebInvoke - 指示服务操作在逻辑上就是调用操作,而且可由 Web 编程模型调用
        /// UriTemplate - 用于服务操作的统一资源标识符 (URI) 模板。URI模板可以将一个 URI 或一组 URI 映射到服务操作。有关 URI 模板的更多信息,请参见 UriTemplate 和 UriTemplateTable
        /// Method - 与操作关联的协议方法,默认为 POST
        /// ResponseFormat - 指定从服务操作发出的响应的格式。Xml 或 Json
        /// </remarks>
        [OperationContract]
        [WebInvoke(
            UriTemplate = "User/{name}/{dayOfbirth}",
            Method = "PUT",
            ResponseFormat = WebMessageFormat.Json)]
        User CreateUser(string name, string dayOfbirth);

        /**//// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="name">用户名</param>
        /// <remarks>
        /// WebGet - 指示服务操作在逻辑上就是检索操作,而且可由 Web 编程模型调用
        /// </remarks>
        [OperationContract]
        [WebGet(
            UriTemplate = "User/{name}",
            ResponseFormat = WebMessageFormat.Json)]
        User GetUser(string name);

        /**//// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="dayOfbirth">生日</param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(UriTemplate = "User/{name}/{dayOfbirth}",
            Method = "POST",
            ResponseFormat = WebMessageFormat.Json)]
        bool UpdateUser(string name, string dayOfbirth);

        /**//// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(
            UriTemplate = "User/{name}",
            Method = "DELETE",
            ResponseFormat = WebMessageFormat.Json)]
        bool DeleteUser(string name);
    }
}


REST.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.Web
{
    /**//// <summary>
    /// 演示REST(Representational State Transfer)的类
    /// </summary>
    public class REST : IREST
    {
        public User CreateUser(string name, string dayOfbirth)
        {
            return new User { Name = name, DayOfbirth = DateTime.Parse(dayOfbirth) };
        }

        public User GetUser(string name)
        {
            return new User { Name = name, DayOfbirth = new DateTime(1980, 2, 14) };
        }

        public

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,