对 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. 报错:MetaException(message:Version information not found in metastore. )

    报错背景: CDH安装完成hive后启动失败. 报错现象: [main]: Metastore Thrift Server threw an exception... MetaException(me ...

  2. Django model中的save后的return

    先给结论吧:在Django model的操作函数中,obj.save()后再执行return obj会返回obj的ID. 看例子: ... def create_session(self,bind_h ...

  3. Ubuntu18使用netplan设置多网口绑定

    Ubuntu18使用netplan设置网络参考:https://www.cnblogs.com/minseo/p/11325384.html 修改配置文件 /etc/netplan/50-cloud- ...

  4. Vue 项目中的ESlint语法报错问题

    在项目中的""和;经常会报错,真的很纠结,今天看到一个解决方法,可以不用卸载删除 在项目根目录中,新建一个.prettierrc文件,来移除分号,和替换为单引号. { " ...

  5. Swoole练习 UDP

    UDP 服务代码 <?php //创建Server对象,监听 127.0.0.1:9502端口,类型为SWOOLE_SOCK_UDP $serv = new swoole_server(&quo ...

  6. 02. xadmin的过滤器queryset()

    需求: 每个老师都只能看到自己的课程 # models.py from django.contrib.auth.models import AbstractUser class UserProfile ...

  7. 微信公众号使用vue,安卓端点击按钮404,ios访问正常问题

    情景:微信公众号使用vue开发的单页面,在安卓端点击按钮访问显示404,ios访问正常问题,能正常显示. 解决:将微信公众号菜单按钮设置的路径中把WWW去掉后,安卓.ios都能正常访问. 问题路径ww ...

  8. Flask 数据库连接

    Flask拥有丰富的扩展组件,数据库管理方面Flask-SQLAlchemy简化了数据库管理的操作.SQLAlchemy是一个很强大的关系型数据库框架,支持多种数据库后台.其不但提供了高层ORM,而且 ...

  9. LeetCode 112. 路径总和(Path Sum) 10

    112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...

  10. phaser三个学生做题目

    3个学生一起参加考试,一共有三道题,要求所有学生到齐才能开始考试,全部同学都做完第一题,学生才能继续做第二题,全部学生做完了第二题,才能做第三题,所有学生都做完的第三题,考试才结束 public cl ...