您的位置:首页 > 编程语言

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

2015-10-13 11:17 323 查看
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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: