WCF REST简单应用 编程初始化 help页面实现

时间:2022-01-26 20:51:06
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
using YFmk.Models.Web;

namespace SmartServiceBLL
{
    /// <summary>
    /// WCF 服务接口
    /// </summary>
    [ServiceContract]
    public interface ISmartService
    {
        #region 餐饮刷卡
        /// <summary>
        /// 获取所有餐厅区域
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(
            UriTemplate = "Refectory/GetRefectoryList",
            Method = "GET",
            ResponseFormat = WebMessageFormat.Json
        )]
        OPResult GetRefectoryList();
        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Newtonsoft.Json;
using YFmk.ORM;
using YFmk.Models.Web;

namespace SmartServiceBLL
{
    /// <summary>
    /// WCF服务
    /// </summary>
    internal class SmartService : ISmartService
    {
        #region 餐饮刷卡

        #endregion
        #region ISmartService 成员
        /// <summary>
        /// 获取所有餐厅区域
        /// </summary>
        /// <returns></returns>
        public OPResult GetRefectoryList()
        {
            OPResult nOPResult=new OPResult();
            List<MiniModels.Area> nList = new DAL<MiniModels.Area>().GetList("AreaType=2", "AreaName ASC", null);
            if (nList.Count > 0)
            {
                nOPResult.Result = true;
                nOPResult.rows = JsonConvert.SerializeObject(nList);
            }
            else
            {
                nOPResult.Result = false;
                nOPResult.Data = "服务端未配置任何餐厅信息";
            }
            return nOPResult;
        }
        #endregion
    }
}



编码实现WCF REST初始化,并提供help服务说明页面。

 Uri httpBaseAddress = new Uri("http://localhost:8000");
            WebServiceHost mServiceHost = new WebServiceHost(typeof(SmartService), httpBaseAddress);
            Binding nWebHttpBinding = new WebHttpBinding();
            mServiceHost.AddServiceEndpoint(typeof(ISmartService), nWebHttpBinding, "SmartService");
            WebHttpBehavior helpBehavior = new WebHttpBehavior();
            helpBehavior.HelpEnabled = true;
            mServiceHost.Description.Endpoints[0].Behaviors.Add(helpBehavior);
            mServiceHost.Open();

示例URL:http://localhost:8000/SmartService/help


WCF REST简单应用 编程初始化 help页面实现

WCF REST简单应用 编程初始化 help页面实现