惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
爱范儿
爱范儿
小众软件
小众软件
K
Kaspersky official blog
P
Proofpoint News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
V
Vulnerabilities – Threatpost
博客园_首页
Microsoft Security Blog
Microsoft Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX
C
Check Point Blog
S
Schneier on Security
P
Palo Alto Networks Blog
IT之家
IT之家
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
Project Zero
Project Zero
Y
Y Combinator Blog
V
Visual Studio Blog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
S
Securelist
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理

博客园 - rosanshao

使用sc 命令写脚本 添加和删除服务 简单应用 Win 10激活 couchbase map reduce StartWith 测试 笔记,转 Couchbase II( View And Index) Couchbase I SqlIO优化 死锁检测 MemCached 安装笔记 Asp.Net异步编程-使用了异步,性能就提升了吗? Autofac Mvc Webapi注入笔记 Sql Server 2005/2008 SqlCacheDependency查询通知的使用总结 WCF HelpPage 和自动根据头返回JSON XML .net 4.0 新特性 Linq 并行化处理 IIS 假死状态处理 gmap jQuery插件开发 StatusCode - rosanshao - 博客园
WCF .NET REST调用方式
rosanshao · 2012-04-15 · via 博客园 - rosanshao

1通过WebClient或者Request实现,此方法略去

2通过WCF Client调用

  a通过WebChannelFactory建Channel

  b通过Channel就可以直接调用服务端方法

3代码

WCF服务端和客户端都引用Contact库

 [ServiceContract]
    public interface IUserService : IBaseWCFService
    {
        [WebGet(UriTemplate = "userID={userID}")]
        [Description("根据UserID获取用户信息")]
        UserInfo GetUserInfo(string userID);
    }

Client调用方式

using (WebChannelFactory<IUserService> wcf = new WebChannelFactory<IUserService>(new Uri("http://ipv4.fiddler:30760/Services/UserService")))
                {  
                    var channel = wcf.CreateChannel();
                    IClientChannel clientchanel = channel as IClientChannel;                   
                    channel.GetUserInfo(new Random().Next(1, 1000).ToString());
                }

WCF请求头设置,ContentType,Accept,这个的设置很让人头痛,查询了很多资料才找到实现方式

通过OperationContextScope来实现

直接代码

 using (WebChannelFactory<IUserService> wcf = new WebChannelFactory<IUserService>(endpoint))
                {
                    var channel = wcf.CreateChannel();
                    IClientChannel clientchanel = channel as IClientChannel;
                    using (new OperationContextScope(channel as IContextChannel))
                    {
                        WebOperationContext.Current.OutgoingRequest.Headers.Add("APIKey", Guid.NewGuid().ToString());
                        WebOperationContext.Current.OutgoingRequest.ContentType = "json"; 

         WebOperationContext.Current.OutgoingRequest.Accept                      
                        channel.GetUserInfo(new Random().Next(1, 1000).ToString());
                    }                   
                }