博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[WCF REST] WebServiceHost 不依赖配置文件启动简单服务
阅读量:7119 次
发布时间:2019-06-28

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

最近用WPF启动 WCF REST 服务,发现app.config 配置好烦,简单一个exe 可以到处搬动,还非得带一个累赘配置,不小心丢了程序就跑不起来。

最后决定,砍去WCF配置项,用WebServiceHost 简单启动服务,监听端口与路径写在注册表中。WPF程序给一个配置项配置端口与路径项。

1 [ServiceContract] 2 public interface IHomeService 3 { 4     [OperationContract] 5     [WebGet(UriTemplate = "Get/{id}", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)] 6     string Get(string id); 7  8     [OperationContract] 9     [WebInvoke(Method = "POST", UriTemplate = "Add", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]10     string Add(string stu);11 }12 13 public class HomeService : IHomeService14 {15     public string Get(string id)16     {17         return "Get " + id;18     }19 20     public string Add(string id)21     {22         return "Add " + id;23     }24 }25 26 public class WebApi27 {28     private static WebServiceHost _host;29 30     /// 31     /// 开启服务32     /// 33     /// 监听路径(http://127.0.0.1:3721/abc)34     public static void Open(string url)35     {36         if (_host==null)37         {38             Uri baseAddress = new Uri(url);39             _host = new WebServiceHost(typeof(HomeService), baseAddress);40             _host.Open();41         }42     }43 }

更多详细内容 可以看 Artech 的详细分解

https://www.cnblogs.com/artech/archive/2012/02/15/wcf-rest.html

 

转载于:https://www.cnblogs.com/craze/p/10878781.html

你可能感兴趣的文章
搜索框,输入关键字过滤对象数组
查看>>
Dart语言——45分钟快速入门(下)
查看>>
iOS safari浏览器上overflow: scroll元素无法滚动bug深究
查看>>
extract-text-webpack-plugin
查看>>
Sequelize Unknown column 'createdAt' in 'field list'?
查看>>
面试题
查看>>
大快HanLP自然语言处理技术介绍
查看>>
centos7 svn自动更新至web目录
查看>>
小米9.0系统最简单激活xposed框架的教程
查看>>
全栈开发工程师微信小程序-上(中)
查看>>
spring boot2 整合(三)JOOQ工具
查看>>
【实战】颠覆银行基础架构的区块链
查看>>
第十六章:SpringCloud Config 配置自动刷新
查看>>
iOS APP内弹窗推送版本更新信息(实现跳转、强制更新等)
查看>>
Flutter 系列文章:Flutter Text 控件介绍
查看>>
二、SpringBoot配置文件讲解
查看>>
HTML基础:web前端建站流程
查看>>
http
查看>>
导航栏与scrollerview(或scrollerview的子类)
查看>>
建立个人Maven仓库
查看>>