用 C# 实现一个简单的 Rest Service 供外部调用
2017-08-29 11:48
477 查看
用 C# 实现一个简单的 Restful Service 供外部调用,大体总结为4点:
The service contract (the methods it offers).
How do you know which one to access from the URL given (URL Routing).
The implementation of the service.
How you will host the service.
详细的基本步骤如下所示:
1):工程结构(Class Library Project)
2): IRestDemoService.cs
3):RestDemoService.cs
4):Host Service 工程结构 (Console Application)
5):Program.cs
6):运行Host程序,在浏览器中输入对应Service的Url
更多信息请看如下链接:
http://www.progware.org/Blog/post/A-simple-REST-service-in-C.aspx
The service contract (the methods it offers).
How do you know which one to access from the URL given (URL Routing).
The implementation of the service.
How you will host the service.
详细的基本步骤如下所示:
1):工程结构(Class Library Project)
2): IRestDemoService.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Web; namespace EricSunRestService { [ServiceContract(Name = "RestDemoServices")] public interface IRestDemoServices { [OperationContract] [WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)] string GetClientNameById(string Id); } public static class Routing { public const string GetClientRoute = "/Client/{id}"; } }
3):RestDemoService.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Activation; namespace EricSunRestService { [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class RestDemoServices : IRestDemoServices { public string GetClientNameById(string Id) { string ReturnString = "HaHa id is: " + Id; return ReturnString; } } }
4):Host Service 工程结构 (Console Application)
5):Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using EricSunRestService; using System.ServiceModel.Web; namespace EricSunHostService { class Program { static void Main(string[] args) { RestDemoServices demoServices = new RestDemoServices(); WebServiceHost _serviceHost = new WebServiceHost(demoServices, new Uri("http://localhost:8000/DemoService")); _serviceHost.Open(); Console.ReadKey(); _serviceHost.Close(); } } }
6):运行Host程序,在浏览器中输入对应Service的Url
更多信息请看如下链接:
http://www.progware.org/Blog/post/A-simple-REST-service-in-C.aspx
相关文章推荐
- 用 C# 实现一个简单的 Rest Service 供外部调用
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- Windows 编程中用Service开启一个外部进程的两种语言实现方式(C++,C#)。
- [转载]C#如何实现对外部程序的动态调用
- 一个用C#实现的简单http server(转)
- 一个简单的oracle分页存储过程的实现和调用
- 一个简单的AJAX实现,基于C#的ASP.Net,包括服务器端的程序代码
- 一个C#写的调用外部进程类
- Castle AOP 系列(四):实现一个简单的基于上下文调用的权限校验机制
- 一个C#写的调用外部进程类
- C#调用外部程序实现压缩和解压缩[原]
- C#调用外部程序实现压缩和解压缩[原]
- 一个简单的oracle分页存储过程的实现和调用
- C#实现一个简单的HTTP服务器
- 一个C#写的调用外部进程类
- c#简单实现一个螺旋数组
- 一个简单的AJAX实现,基于C#的ASP.Net,包括服务器端的程序代码
- 一个简单的oracle分页存储过程的实现和调用
- 怎样写一个存储过程类来实现C#中对存储过程的调用?
- 一个C#实现的最简单的委托例子