博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用 C# 实现一个简单的 Rest Service 供外部调用
阅读量:6416 次
发布时间:2019-06-23

本文共 2193 字,大约阅读时间需要 7 分钟。

用 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

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 

 

 

转载地址:http://tfpra.baihongyu.com/

你可能感兴趣的文章
fastscript增加三方控件之二
查看>>
Windows Vista RTM 你准备好了么?
查看>>
Tensorflow Serving 模型部署和服务
查看>>
Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet 3.0+JSP 2.2深入剖析与实例应用
查看>>
topcoder srm 680 div1 -3
查看>>
具体数学第二版第四章习题(1)
查看>>
高效前端优化工具--Fiddler入门教程
查看>>
【翻译】我钟爱的HTML5和CSS3在线工具
查看>>
Java多线程学习(吐血超详细总结)
查看>>
css3 变形
查看>>
Win7 64bit 安装Mysql5 出错 无法启动服务。
查看>>
嵌入式 H264参数语法文档: SPS、PPS、IDR以及NALU编码规律
查看>>
初识Opserver,StackExchange的监控解决方案
查看>>
给大家讲解一下JavaScript与后台Java天衣无缝相结合
查看>>
探索HTML5之本地文件系统API - File System API
查看>>
PHP实现人人OAuth登录和API调用
查看>>
redis源码笔记 - initServer
查看>>
FindBugs工具常见问题
查看>>
ECSHOP报错误Deprecated: preg_replace(): The /e modifier is depr
查看>>
【iOS】iOS之Button segue弹出popOver消除(dismiss)问题
查看>>