REST(Representational State Transfer)即 表述性状态传递 ,简称REST,通俗来讲就是:资源在网络中以某种表现形式进行状态转移。

RESTful是一种软件架构风格、设计风格,而不是标准,只是提供了一组设计原则和约束条件。

在三种主流的Web服务实现方案中,因为REST模式的Web服务与复杂的SOAP和XML-RPC对比来讲明显的更加简洁,越来越多的web服务开始采用REST风格设计和实现。

1.首先建立解决方案创建WCF项目,删除默认生成文件,创建Interface和Services文件夹。

2.创建ITestService.cs文件;

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text; namespace WCFSERVICE.Interface
{ [ServiceContract]
public interface ITestService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Test1", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
string Test1(string userName, string password); [OperationContract]
[WebGet(UriTemplate = "Test/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] string Test(string id); }
}

3.创建TestService.cs文件

namespace WCFSERVICE.Services
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TestService : ITestService
{ public string Test1(string userName, string password)
{
if (!Validate.IsValidate(userName, password))
{ } return "";
} public string Test(string id)
{ return id;
} }
}

4.在Web.config文件中添加配置。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<appSettings>
<add key="SqlConnectString" value="data source=192.168.1.2;initial catalog=TEST;user id=sa;password=123456;"/>
</appSettings>
<system.serviceModel> <service name="LWCFSERVICE.Services.TestService" >
<endpoint address="" behaviorConfiguration="webbehavioree"
binding="webHttpBinding" contract="WCFSERVICE.Interface.ITestService" /> </service> </services>
<behaviors>
<endpointBehaviors>
<behavior name="webbehavior">
<webHttp />
</behavior> </endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" >
<serviceActivations>
<add relativeAddress="./Service/TestService.svc" service="WCFSERVICE.Service.TestService"/>
</serviceActivations> </serviceHostingEnvironment> </system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/>
</system.webServer> </configuration>

5.在Global.asax的Application_Start方法中添加路由规则。

using WCFSERVICE.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.SessionState; namespace WCFSERVICE
{
public class Global : System.Web.HttpApplication
{ protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
System.Web.Routing.RouteTable.Routes.Add(new System.ServiceModel.Activation.ServiceRoute("TestService", new System.ServiceModel.Activation.WebServiceHostFactory(), typeof(TestService))); } protected void Session_Start(object sender, EventArgs e)
{ } protected void Application_BeginRequest(object sender, EventArgs e)
{ } protected void Application_AuthenticateRequest(object sender, EventArgs e)
{ } protected void Application_Error(object sender, EventArgs e)
{ } protected void Session_End(object sender, EventArgs e)
{ } protected void Application_End(object sender, EventArgs e)
{ }
}
}

6.访问方式:Get:  http://localhost:63980/TestService/Test/as

Post:

$.ajax({
type: "post",
url: "http://localhost:61756/TestService/Test1",
contentType: "application/json",
dataType: "application/json",
data: { username: "test", password: "test" },
success: function (data, status) {
console.log(data);
console.log(status);
}
});

注:项目实战编写WEBAPI方式更好,RESTful的WCF我只是学习而已。

c# 编写REST的WCF的更多相关文章

  1. 编写WsHttpBinding的WCF通信方式

    这个通信方式本人实验了好久,需要一个重要的条件是服务端和客户端的发送内容方式都是相同的声明,需要在配置文件写入,客户端: <system.serviceModel> <binding ...

  2. 1 学习wcf 编写简单的WCF服务流程 并发布在IIS上

    学习笔记 学习大佬的博客 https://www.cnblogs.com/iamlilinfeng/archive/2012/09/25/2700049.html  写一遍加深印象 图片看不清楚的 可 ...

  3. Log4net入门(WCF篇)

    在上一篇Log4net入门(ASP.NET MVC 5篇)中,我们讲述了如何在ASP.NET MVC 5项目中使用log4net.在这一篇中,我们将讲述如何在WCF应用中使用log4net,为了讲述这 ...

  4. Topshelf + ServiceModelEx + Nlog 从头构建WCF

    前言 Topshelf可以很方便的构建windows service,而且在本地开发时也可以构建Console宿主,因此很方便WCF的开发. ServiceModelEx则提供了很多便利的方法来配置w ...

  5. WCF SOA服务应用

    WCF是微软官方推出的一个基于服务的整合框架,它整合了以前的Web Service.MSMQ.Remoting等通信技术,通过灵活的配置,让服务编程更加容易.可扩展.这篇文章主要目的就是带领大家从开发 ...

  6. WCF 配置服务 (02)

    配置服务概述 • 在设计和实现服务协定后,即可配置服务. 在其中可以定义和自定义如何向客户端公开服务,包括指定可以找到服务的地址.服务用于发送和接收消息的传输和消息编码,以及服务需要的安全类型. • ...

  7. WCF、.Net Remoting、Web Service概念及区别

    此文章主要参考http://www.cnblogs.com/weiweibtm/archive/2013/06/21/3148583.html 参考书籍<WCF全面解析上册>.<WC ...

  8. WCF服务端调用client.

    wcf服务端 1,新建一个"windows窗口程序"名称为WCFServer2. 2.然后加入一个"WCF服务"名称为Service1. 详细步骤为:解决方式试 ...

  9. 用浏览器访问WCF

    在开发的时候,为客户端编写代码访问WCF的方式,我们应该比较熟悉,主要是添加一个代理,然后通过这个代理进行访问. 如果用浏览器访问WCF呢?(其实最终是想在JS里面访问)用浏览器访问.测试Web Se ...

随机推荐

  1. Cocos2d-x学习笔记(16)(常见22种特效)

    1.CCShaky3D::create(int range.bool shakeZ,const ccGridSize& gridSize,float duration)//创建一个3D晃动的特 ...

  2. [Javascript] Combine Objects with Object.assign and Lodash merge

    Learn how to use Object.assign to combine multiple objects together. This pattern is helpful when wr ...

  3. Yii学习笔记之四(表单验证 api 翻译)

    1.表单验证 对于用户输入的全部数据,你不能信任,必须加以验证. 全部框架如此.对于yii 能够使用函数  yii\base\Model::validate()  进行验证 他会返回boolean值的 ...

  4. echarts怎么使用(最最最最简单版)(本质canvas)

    echarts怎么使用(最最最最简单版)(本质canvas) 一.总结 一句话总结:外部扩展插件肯定要写js啊,不然数据怎么进去,不然宽高怎么设置.本质都是canvas嵌套在页面上,比如div中. 1 ...

  5. springmvc-Controller类的方法返回String不跳转

    买了本书,打算系统的学习一下spring,做了一下书中的练习,出现了一个问题,Controller类的方法返回String,但是页面不跳转,而是直接把字符串的内容显示到页面上. @RequestMap ...

  6. VS2013 Qt5显示中文字符

    VS2013上建立的Qt5project中显示中文字符的两种方式: 1. QStringLiteral("開始") 2. QString::fromLocal8Bit(" ...

  7. Jquery前端分页插件pagination同步加载和异步加载

    上一篇文章介绍了Jquery前端分页插件pagination的基本使用方法和使用案例,大致原理就是一次性加载所有的数据再分页.https://www.jianshu.com/p/a1b8b1db025 ...

  8. 小强的HTML5移动开发之路(36)——jQuery中的DOM操作

    1.查询 利用选择器查找节点 使用 html() / text() / attr() 输出节点文本和属性值. 注意:下拉列表使用 val() <html> <head> < ...

  9. cocos2d-x之道~制作第一款文字游戏(二)

    在 cocos2d-x之道~制作第一款文字游戏(一)中,使用cocos2d-x把主界面显示出来.分别有每一个级别提供的初始短语TileView,和目标短语TargetView.初步接触了cocos2d ...

  10. 监控Nginx服务的Shell脚本

    Nginx 虽然处理并发量比 apache 确实要强点,但它这种 php-cgi 模式不是太稳定,这点网上也有朋友总结了,我在实现项目中也感受到了. 我们一台支付机,偶尔会出现以下情况的:php-cg ...