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. ios开发之坐标系转换

    1:坐标系转换最核心的问题就是:比较两个坐标是否包含,或者是重叠等,最主要的问题是先将两个坐标转换到同一个坐标系下再去比较.第一步先确定矩形框在某个view坐标系下的frame(该矩形框是以该view ...

  2. js进阶 11-14 jquery如何实现元素的替换和遍历

    js进阶  11-14  jquery如何实现元素的替换和遍历 一.总结 一句话总结:替换:replaceAll() 与 replaceWith().遍历:each(). 1.replaceAll() ...

  3. 深度学习代码注解(一)—— mnistdeepauto

    clear all close all %% 1:参数设置 maxepoch=10; %In the Science paper we use maxepoch=50, but it works ju ...

  4. VS(Visual Studio)自动创建的文件格式

    .sln:solution,解决方案文件: .vsxproj:解决方案下的项目文件: .vssettings:环境设置文件, 菜单栏 ⇒ [工具]⇒ [导入和导出设置]⇒ 进行环境设置的导入和导出操作 ...

  5. 编辑器sublime、终端运行python

    sublime编辑器 Sublime Text 是一个代码编辑器(Sublime Text 2是收费软件,但可以无限期试用) Sublime Text是由程序员Jon Skinner于2008年1月份 ...

  6. Git 常用命令总结(精问)

    Git 常用命令总结(精问) 一.总结 一句话总结:下次尝试做任何git的操作之前,把这个精看精问一遍,绝对会有意向不到的收获. 二.Git 常用命令总结 1.git常用命令 安装及配置: Ubunt ...

  7. erlang的Socket参数含义

    http://blog.csdn.net/pkutao/article/details/8572216 {ok, Listen} = gen_tcp:listen(?defPort, [binary, ...

  8. 【t007】棋盘放置指南车问题

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 按照国际象棋的规则,车可以攻击与之处在同一行或同一列上的棋子.指南车是有方向的车.横向指南车可以攻击与之 ...

  9. 【BZOJ 1018】 [SHOI2008]堵塞的交通traffic

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1018 [题意] [题解] 按照这里的题解写的http://blog.csdn.net/ ...

  10. 使用Akka、Kafka和ElasticSearch等构建分析引擎 -- good

    本文翻译自Building Analytics Engine Using Akka, Kafka & ElasticSearch,已获得原作者Satendra Kumar和网站授权. 在这篇文 ...