1):用VS2013创建一个WCF的工程,如下图所示:

2):我们来看一下默认状态下的config文件内容,这里的内容我们会再后续的步骤中进行修改

  1. <?xml version="1.0"?>
  2. <configuration>
  3.  
  4. <appSettings>
  5. <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  6. </appSettings>
  7. <system.web>
  8. <compilation debug="true" targetFramework="4.5" />
  9. <httpRuntime targetFramework="4.5"/>
  10. </system.web>
  11. <system.serviceModel>
  12. <behaviors>
  13. <serviceBehaviors>
  14. <behavior>
  15. <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
  16. <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
  17. <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
  18. <serviceDebug includeExceptionDetailInFaults="false"/>
  19. </behavior>
  20. </serviceBehaviors>
  21. </behaviors>
  22. <protocolMapping>
  23. <add binding="basicHttpsBinding" scheme="https" />
  24. </protocolMapping>
  25. <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  26. </system.serviceModel>
  27. <system.webServer>
  28. <modules runAllManagedModulesForAllRequests="true"/>
  29. <!--
  30. To browse web app root directory during debugging, set the value below to true.
  31. Set to false before deployment to avoid disclosing web app folder information.
  32. -->
  33. <directoryBrowse enabled="true"/>
  34. </system.webServer>
  35.  
  36. </configuration>

3):我们对工程文件及其内容做一下修改,具体代码如下所示:

3.1):UserData class

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.Web;
  6.  
  7. namespace EricSunWcfService
  8. {
  9. [DataContract]
  10. public class UserData
  11. {
  12. [DataMember]
  13. public string Name { get; set; }
  14. [DataMember]
  15. public string Password { get; set; }
  16. [DataMember]
  17. public string Email { get; set; }
  18. }
  19. }

3.2):IDataService,这个接口是从默认的IService1修改而来,并且这里提供了两种方法,一个是GET,另外是POST,都是简单的返回UserData对象的Json字符串

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8.  
  9. namespace EricSunWcfService
  10. {
  11. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
  12. [ServiceContract]
  13. public interface IUserService
  14. {
  15. [OperationContract]
  16. [WebInvoke(Method = "GET", UriTemplate = "getuser/{name}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
  17. UserData GetUserData(string name);
  18.  
  19. [OperationContract]
  20. [WebInvoke(Method = "POST", UriTemplate = "checkuser", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
  21. UserData CheckUserData(UserData user);
  22. }
  23. }

3.3):UserService,这个文件名是从默认的Service1修改过来的

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8.  
  9. namespace EricSunWcfService
  10. {
  11. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
  12. // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
  13. public class UserService : IUserService
  14. {
  15. public UserData GetUserData(string name)
  16. {
  17. UserData user = new UserData();
  18. user.Name = name;
  19. user.Email = "test@123.com";
  20. return user;
  21. }
  22.  
  23. public UserData CheckUserData(UserData user)
  24. {
  25. user.Name += "-test";
  26. return user;
  27. }
  28. }
  29. }

3.4):我们可以点击对应的Service的‘View Markup’来修改ServiceHost的信息,如下图所示

4):我们在IIS中创建一个Site来Host我们所提供的WCF Service,用http协议并且将端口绑定为8089,与此同时制定好Physical path,如下图所示

【注:请将创建的Application Pool的.Net Framework Version修改成为4.0】

5):在目前这种状态下还不能成功的访问对应的WCF Service的,我们需要对web.config进行修改

  1. <?xml version="1.0"?>
  2. <configuration>
  3.  
  4. <appSettings>
  5. <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  6. </appSettings>
  7. <system.web>
  8. <compilation debug="true" targetFramework="4.5" />
  9. <httpRuntime targetFramework="4.5"/>
  10. </system.web>
  11. <system.serviceModel>
  12. <services>
  13. <service name="EricSunWcfService.UserService" behaviorConfiguration="RESTBehaviour">
  14. <endpoint address=""
  15. binding="webHttpBinding"
  16. contract="EricSunWcfService.IUserService"
  17. behaviorConfiguration="ESEndPointBehavior"/>
  18. </service>
  19. </services>
  20. <behaviors>
  21. <serviceBehaviors>
  22. <behavior name="RESTBehaviour">
  23. <serviceMetadata httpGetEnabled="true"/>
  24. <serviceDebug includeExceptionDetailInFaults="true"/>
  25. </behavior>
  26.  
  27. <behavior>
  28. <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
  29. <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
  30. <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
  31. <serviceDebug includeExceptionDetailInFaults="false"/>
  32. </behavior>
  33. </serviceBehaviors>
  34. <endpointBehaviors>
  35. <behavior name="ESEndPointBehavior">
  36. <webHttp/>
  37. </behavior>
  38. </endpointBehaviors>
  39. </behaviors>
  40. <protocolMapping>
  41. <add binding="basicHttpsBinding" scheme="https" />
  42. </protocolMapping>
  43. <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  44. </system.serviceModel>
  45. <system.webServer>
  46. <!--
  47. To browse web app root directory during debugging, set the value below to true.
  48. Set to false before deployment to avoid disclosing web app folder information.
  49. -->
  50. <directoryBrowse enabled="true"/>
  51. <modules runAllManagedModulesForAllRequests="true"/>
  52. </system.webServer>
  53.  
  54. </configuration>

6):config文件配置完毕后,我们就可访问此URL:http://localhost:8089/UserService.svc 来判断我们Service提供的正确与否,若是看到下面的截图则表明Service无误

7):若在访问 http://localhost:8089/UserService.svc 的时候出现500.19【HTTP Error 500.19 - Internal Server Error】错误请参考如下链接解决

http://www.cnblogs.com/mingmingruyuedlut/archive/2011/11/04/2235630.html

8):访问GET方法我们可以直接在浏览器地址栏中输入对应的service地址即可访问

例如输入 http://localhost:8089/UserService.svc/getuser/eric

会给我们返回: {"Email":"test@123.com","Name":"eric","Password":null}

9):若是访问POST方法,单纯的在浏览器中输入地址则无法完成正确的调用,这里我们使用浏览器的插件poster (https://addons.mozilla.org/en-US/firefox/addon/poster/)

如上图所示,在poster中填入正确的配置信息,并且传入Json的参数值{"Email":"test@123.com","Name":"eric","Password":"123"}

点击POST按钮之后变回得到如下返回结果:

10):若是我们发现在调用PUT或者DELETE方法时出现Status:405 Method Not Allowed的问题,请在web.config文件中的system.webServer节点中添加如下配置

  1. <modules runAllManagedModulesForAllRequests="true">
  2. <remove name="WebDAVModule" />
  3. </modules>
  4. <handlers>
  5. <remove name="WebDAV" />
  6. </handlers>

至此我们就可以通过WCF向外提供REST的Service了~~

如何配置来完成HTTPS的访问请看如下链接:

http://www.cnblogs.com/mingmingruyuedlut/p/4236035.html

利用WCF创建简单的RESTFul Service的更多相关文章

  1. WCF创建简单程序

    1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序.建立完成后如下图所示: 2.删除系统生成的两个文件IService1.cs与Service1.svc,当然你也可以直接 ...

  2. Eclipse下利用Maven创建SpringBoot的Restful风格程序

    参考文章:https://spring.io/guides/gs/rest-service/ 中文翻译:https://blog.dubby.cn/detail.html?id=9040 1.目标是什 ...

  3. SpringBoot IntelliJ创建简单的Restful接口

    使用SpringBoot快速建服务,和NodeJS使用express几乎一模一样,主要分为以下: 1.添加和安装依赖  2.添加路由(即接口) 3.对路由事件进行处理 同样坑的地方就是,祖国的防火墙太 ...

  4. 利用HttpListener创建简单的HTTP服务

    using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using ...

  5. WCF Restful Service

    对 Web Services.WCF 和 Restful 的扫盲可参见:https://www.cnblogs.com/scy251147/p/3382436.html 关于之前对 WCF 的学习,可 ...

  6. 基于.Net FrameWork的 RestFul Service

    关于本文 这篇文章的目的就是向大家阐述如何在.net framework 4.0中创建RestFul Service并且使用它. 什么是web Services,什么是WCF 首先讲到的是web Se ...

  7. [转]使用WCF 4.0 构建 REST Service

    本文转自:http://www.cnblogs.com/lanvige/archive/2010/12/03/set_up_rest_service_with_wcf_4.html 用过一段时间的Ru ...

  8. 用 C# 实现一个简单的 Rest Service 供外部调用

    用 C#  实现一个简单的 Restful Service 供外部调用,大体总结为4点: The service contract (the methods it offers). How do yo ...

  9. Wcf Restful Service服务搭建

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

随机推荐

  1. ReactNative 当前url和cookies的获取

    前面大概介绍了react-native的运行helloword级别的入门,所以之后简单的东西就不写了,毕竟官网上都能够找到. reactnative官网:https://facebook.github ...

  2. 一、Daily Scrum Meeting【Alpha】------Clover

    [Alpha]Daily Scrum Meeting 第一次 [Alpha]Daily Scrum Meeting 第二次 [Alpha]Daily Scrum Meeting 第三次 [Alpha] ...

  3. oracle---jdbctest--laobai

    import java.sql.CallableStatement; import java.sql.Connection; import java.sql.ResultSet; import ora ...

  4. 高版本api在低版本中的兼容

    直接上例子,看如何避免crash. eg:根据给出路径,获取此路径所在分区的总空间大小. 文档说明:获取文件系统用量情况,在API level 9及其以上的系统,可直接调用File对象的相关方法,以下 ...

  5. LoadRunner 函数之lr_xml_find

    实例如: char *xml_input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>&qu ...

  6. html传参数

    var request = { QueryString: function (paramName) { var url = window.location.search; paramValue = & ...

  7. 基于dom的xss漏洞原理

    原文:http://www.anying.org/thread-36-1-1.html转载必须注明原文地址最近看到网络上很多人都在说XSS我就借着暗影这个平台发表下自己对这一块的一些认识.其实对于XS ...

  8. CentOS 7 上安装 redis3.2.3安装与配置

    前一段时间写过一篇codis集群的文章,写那篇文章主要是因为当时的项目不支持redis自身集群的功能. 而现在最新的项目是需要redis集群的,这篇文章我们就来介绍下有关redis的安装与配置. 一. ...

  9. 百度地图API简单应用

    在做移动端应用时经常用到百度地图API,百度API有强大的示例和文档,开发之前去百度相关网站注册密钥,很块博主只花了几分钟 百度地图API范例 百度地图API文档说明 例子1:输入特定关键字绘制地图标 ...

  10. 深入理解javascript原型和闭包(9)——简述【执行上下文】下

    继续上一篇文章(http://www.cnblogs.com/wangfupeng1988/p/3986420.html)的内容. 上一篇我们讲到在全局环境下的代码段中,执行上下文环境中有如何数据: ...