WCF Restful Service
对 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的更多相关文章
- 构建基于WCF Restful Service的服务
前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面的Dynamic Proxy,但是没有这种方式简便), ...
- WCF Restful Service的服务
构建基于WCF Restful Service的服务 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面 ...
- WCF Restful Service Get / Post请求
Rest 它是用于创建分布式超文本媒体的一种架构方式,我们可以通过标准的HTTP(GET,POST,PUT,DELETE)操作来构建基于面向资源的软件架构方式(Resource-Oriented Ar ...
- [转]构建基于WCF Restful Service的服务
本文转自:http://www.cnblogs.com/scy251147/p/3566638.html 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添 ...
- Wcf Restful Service服务搭建
目的 使用Wcf(C#)搭建一个Restful Service 背景 最近接到一个项目,客户要求使用Restful 方式接收到数据,并对数据提供对数据的统计显示功能,简单是简单,但必须要使用Restf ...
- [转]WCF RESTful service and WebGrid in ASP.NET MVC 5
使用WebClient调用WCF服务 流程:从View获取实体类-->序列化-->写入内存流中-->传给远端的WCF服务 Get.POST.PUT.DELETE,客户端以流的方式调用 ...
- 在IIS8.5的环境下配置WCF的Restful Service
今天在客户的环境中(Windows Server 2012 R2 + IIS 8.5)搭建Call WCF Restful Service的功能,发现了几个环境配置的问题,记录如下: 1):此环境先安 ...
- TinyFrame升级之十:WCF Rest Service注入IOC的心
由于在实际开发中,Silverlight需要调用WebService完成数据的获取,由于之前我们一直采用古老的ASMX方式,生成的代理类不仅难以维护,而且自身没有提供APM模式的调用方式,导致在Sin ...
- [经验] - JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案
最近在开发WSS RESTful服务的时候, 碰到了这些个纠结的问题. 在网上查找了半天, 找到n多种解决方案, 但是都是部分的, 要么是没有跨域的情况, 要么是没有post的情况, 要么不是用WCF ...
随机推荐
- wave数据集的回归曲线
wave数据集的回归曲线 import matplotlib.pyplot as pltimport mglearnfrom scipy import sparseimport numpy as np ...
- 【 argo 和 kubectl 】
argo submit --watch xxx.yaml [ --kubeconfig xxx.conf --namespace xxx ] argo list [ --kubeconfig xxx ...
- HTML布局排版之制作个人网站的文章列表
文章列表.博文列表,一般是有文章名字和时间构成的,文章名字后面是时间,点击文章的名字,可进入该文章.为了美观,一般文章名字都有一定的最大字数限制,长宽对齐,等长宽的统一格式比较美观,这种用表格来做比较 ...
- 删除SQL约束的方法
在SQL数据库中,如果需要删除表约束,应该如何操作呢?下面就将为您介绍删除SQL表约束的方法,供您参考,希望对您有所帮助. --1)禁止所有表约束的SQL select 'alter table '+ ...
- build时自动清除console
一.第一种方法 安装 babel-plugin-transform-remove-console 修改 babel.config.js 文件 let transformRemoveConsolePlu ...
- Redis与memached的区别
Redis与Memcached的区别 传统MySQL+ Memcached架构遇到的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都 ...
- Appium移动自动化测试-----(六)2.AppiumDesktop录制脚本生成极简脚本
AppiumDesktop启动页面: 启动AppiumDesktop以后点击该页面右上角的Start New Session按钮,就会启动一个新的会话窗口(如下图),在这个窗口我们需要配置一些Desi ...
- lua . 命令收集
io.popen()## 原型:io.popen ([prog [, mode]]) 解释:在额外的进程中启动程序prog,并返回用于prog的文件句柄.通俗的来说就是使用这个函数可以调用一个命令(程 ...
- 018 Android Activity界面移入与移出的动画效果
1.平移动画 上一页移入动画 (-屏幕宽度,y)------>(0,y) 上一页移出动画 (0,y)-------------->(屏幕宽度,y) 下一页移入动画 (屏幕宽度,y)---- ...
- Linux基础-09-磁盘分区、挂载及文件系统管理
1. 硬件设备与文件名的对应关系 1) 在Linux系统中,每个设备都被当初一个文件来对待. 2) 各种设备在Linux中的文件名 2. 硬盘的结构及硬盘分区 1) 为什么要进行硬盘分区: a) 更容 ...