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. [RxJS] Split an RxJS observable with window

    Mapping the values of an observable to many inner observables is not the only way to create a higher ...

  2. Android JNI--基础篇(二)

    编写一个可以与C代码交互的android工程需要如下步骤: 1.JAVA代码中写声明native 方法 2. 创建jni目录,编写c代码,方法名字要对应 3.编写Android.mk文件(交叉编译的规 ...

  3. 【u207】最小值

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] N个数排成一排,你可以任意选择连续的若干个数,算出它们的和.问该如何选择才能使得和的绝对值最小. 如: ...

  4. nuklear(A single-header ANSI C gui library,界面还不错)

    Nuklear This is a minimal state immediate mode graphical user interface toolkit written in ANSI C an ...

  5. js进阶 9-10 html控件如何实现点击自动选择控件内容

    js进阶 9-10  html控件如何实现点击自动选择控件内容 一.总结 一句话总结: 1.在click事件中,如果focus,那就select 2.blur 1.html中控件添加两种方式? 在表单 ...

  6. 学习鸟哥的Linux私房菜笔记(14)——硬件配置与管理

    一.设备文件 Linux沿袭了Unix的风格,将所有设备看成一个文件 设备文件分为两种: 块设备文件(b):比如硬盘.光驱 字符设备文件(c):比如串口.键盘 设备文件一般存放在/dev目录下 二.常 ...

  7. 文件控制 fcntl函数具体解释

    摘要:本文主要讨论文件控制fcntl函数的基本应用.dup函数能够拷贝文件描写叙述符,而fcntl函数与dup函数有着异曲同工之妙.而且还有更加强大的功能,能够获取或设置已打开文件的性质,操作文件锁. ...

  8. 【14.67%】【codeforces 615D】Multipliers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. MongoDB小结

    教程 MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.MongoDB 旨在为WEB应用提供可扩展的 ...

  10. Spring 4 MVC+Apache Tiles 3 Example

    In this post we will integrate Apache Tiles 3 with Spring MVC 4, using annotation-based configuratio ...