对 Web Services、WCF 和 Restful 的扫盲可参见:https://www.cnblogs.com/scy251147/p/3382436.html

关于之前对 WCF 的学习,可参见:WCF | wjcx_sqh

首先,对 Restful Service 作简单的了解

  • 创建分布式超文本媒体的一种架构方式
  • 独立于任何技术、平台,面向资源 Resource-Oriented Architecture (ROA)
  • 通过标准的HTTP(GET,POST,PUT,DELETE)操作来定义资源
  • 通过 Uniform Resource Identifier(URI)发布资源

首先,定义服务,简单之

[ServiceContract(Name = "user")]
public interface IServiceWCF
{
[OperationContract]
[WebInvoke(Method = "GET",
UriTemplate = "getUser/{name}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
UserData GetUserData(string name);
} [AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ServiceWCF : IServiceWCF
{
public UserData GetUserData(string name) {
//服务接口方法实现
}
}

其中,AspNetCompatibilityRequirements 指示该服务能否在 ASP.NET 兼容模式下运行,也可以加上

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

用于指示说明服务端只会存在这类的一个实例。服务定义完成后,需要更新配置文件 Web.Config

<system.serviceModel>
<services>
<service name="RestfulWcf.ServiceWCF" behaviorConfiguration="defaultServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="RestfulWcf.IServiceWCF"
behaviorConfiguration="defaultEndpointBehavior"></endpoint>
</service>
</services> <behaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="defaultServiceBehaviorHttps">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors> <endpointBehaviors>
<behavior name="defaultEndpointBehavior">
<webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="6553500"/>
</behavior>
</endpointBehaviors>
</behaviors> <protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

同时,新增 Global.asax 全局资源文件,用于定义注册路由

public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegistrRoutes();
} private void RegistrRoutes()
{
//ServiceRoute需要显式引用 System.ServiceModel.Activation.dll
RouteTable.Routes.Add(
new ServiceRoute("user", new WebServiceHostFactory(), typeof(ServiceWCF)));
}
}

最后,可 Service.svc直接右键运行,也可部署至 IIS

项目-属性,生成路径应为bin目录,IIS部署时,网站路径指向该路径即可

通过该路径可以查看该服务接口发布的方法

http://localhost:18800/user/help

若在调用PUT或DELETE方法时出现 Status:405 Method Not Allowed 问题,在 web.config中 system.webServer节点添加如下配置

<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
</handlers>

详细配置过程,参见(推荐): WCF RESTFul 服务搭建

参考

关于如何配置 https 的访问参见:http://www.cnblogs.com/mingmingruyuedlut/p/4236035.html

需要分别在 serviceModel和 services中添加 https配置

<bindings>
<webHttpBinding >
<binding name="SecureWebBinding" >
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</webHttpBinding>
</bindings> <endpoint address="" binding="webHttpBinding"
bindingConfiguration="SecureWebBinding"
contract="RestfulWcf.IServiceWCF"
behaviorConfiguration="defaultEndpointBehavior"/>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>

不要忘记在 IIS中为该服务绑定 443端口即可。

注:

Restful WCF Service 已经是过时的技术,推荐进一步学习 WebApi,具体参见:C# - MVC WebApi | wjcx_sqh

WCF Restful Service的更多相关文章

  1. 构建基于WCF Restful Service的服务

    前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面的Dynamic Proxy,但是没有这种方式简便), ...

  2. WCF Restful Service的服务

    构建基于WCF Restful Service的服务 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面 ...

  3. WCF Restful Service Get / Post请求

    Rest 它是用于创建分布式超文本媒体的一种架构方式,我们可以通过标准的HTTP(GET,POST,PUT,DELETE)操作来构建基于面向资源的软件架构方式(Resource-Oriented Ar ...

  4. [转]构建基于WCF Restful Service的服务

    本文转自:http://www.cnblogs.com/scy251147/p/3566638.html 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添 ...

  5. Wcf Restful Service服务搭建

    目的 使用Wcf(C#)搭建一个Restful Service 背景 最近接到一个项目,客户要求使用Restful 方式接收到数据,并对数据提供对数据的统计显示功能,简单是简单,但必须要使用Restf ...

  6. [转]WCF RESTful service and WebGrid in ASP.NET MVC 5

    使用WebClient调用WCF服务 流程:从View获取实体类-->序列化-->写入内存流中-->传给远端的WCF服务 Get.POST.PUT.DELETE,客户端以流的方式调用 ...

  7. 在IIS8.5的环境下配置WCF的Restful Service

    今天在客户的环境中(Windows Server 2012 R2 + IIS 8.5)搭建Call WCF Restful Service的功能,发现了几个环境配置的问题,记录如下: 1):此环境先安 ...

  8. TinyFrame升级之十:WCF Rest Service注入IOC的心

    由于在实际开发中,Silverlight需要调用WebService完成数据的获取,由于之前我们一直采用古老的ASMX方式,生成的代理类不仅难以维护,而且自身没有提供APM模式的调用方式,导致在Sin ...

  9. [经验] - JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案

    最近在开发WSS RESTful服务的时候, 碰到了这些个纠结的问题. 在网上查找了半天, 找到n多种解决方案, 但是都是部分的, 要么是没有跨域的情况, 要么是没有post的情况, 要么不是用WCF ...

随机推荐

  1. webpack 安装vue(两种代码模式compiler 和runtime)

    使用webpack安装vue,import之后,运营项目报错,如下: [Vue warn]: You are using the runtime-only build of Vue where the ...

  2. yarn 的常用命令

    初始化新项目yarn init添加依赖包yarn add [package]yarn add [package]@[version]yarn add [package]@[tag]将依赖项添加到不同依 ...

  3. Eclipse+TestNG搭建接口自动化测试框架

    一.环境安装 1.前提 安装好jdk 配置好Java环境变量 安装Eclips 这些网上都有,就不再详细介绍. 资源分享链接:http://pan.baidu.com/s/1v9Fw6 2.安装Tes ...

  4. Elasticsearch配置安装

    跨域  elasticsearch-head连接es时会提示连接失败,有可能就是没有开启跨域 http.cors.enabled 是否支持跨域,默认为false http.cors.allow-ori ...

  5. Python 装饰器执行顺序

    Python 装饰器执行顺序 之前同事问到两个装饰器在代码中使用顺序不同会不会有什么问题,装饰器是对被装饰的函数做了一层包装,然后执行的时候执行了被包装后的函数,例如: def decorator_a ...

  6. Java常用命令:jps、jstack、jmap、jstat(带有实例教程)

      版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013310517/article/details/80990924 查看Java进程:jps ...

  7. springmvc流程 struts2 spring Hibernate 优缺点 使用场景介绍

    为什么使用HandlerAdapter? SpringMVC使用一个Servlet(DispacherServlet)代理所有的请求 , SpringMVC中的处理器是方法级别的处理器,而非类级别的处 ...

  8. PAT(B) 1034 有理数四则运算(Java)

    题目链接:1034 有理数四则运算 (20 point(s)) 题目描述 本题要求编写程序,计算 2 个有理数的和.差.积.商. 输入格式 输入在一行中按照 a1/b1 a2/b2 的格式给出两个分数 ...

  9. IAR_EW_MSP430下载

    附带完整安装过程,来自本人下载截图. 附带四种花色的花样灯源码和仿真图(ps:不用担心是错的,有疑问欢迎博客留言) 链接:https://pan.baidu.com/s/1ShDRlEQLwkYNOu ...

  10. Python后台执行不启用缓存

    1.运行时加-u参数,如 # python3 -u test.py >> test.log & 用man查看python的-u参数,说明如下: Force stdin, stdou ...