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

推荐订阅源

S
Security @ Cisco Blogs
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
C
CERT Recently Published Vulnerability Notes
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cisco Blogs
博客园 - 司徒正美
L
LINUX DO - 热门话题
D
Docker
M
MIT News - Artificial intelligence
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
Jina AI
Jina AI
Last Week in AI
Last Week in AI
Security Latest
Security Latest
The Hacker News
The Hacker News
L
Lohrmann on Cybersecurity
Y
Y Combinator Blog
A
Arctic Wolf
小众软件
小众软件
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
腾讯CDC
Google DeepMind News
Google DeepMind News
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 叶小钗
I
Intezer
NISL@THU
NISL@THU
A
About on SuperTechFans
爱范儿
爱范儿
C
Cybersecurity and Infrastructure Security Agency CISA
D
Darknet – Hacking Tools, Hacker News & Cyber Security
G
Google Developers Blog
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Cyberwarzone
Cyberwarzone
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news

博客园 - dragonpig

Html 5 Canvas绘制分形图Mandelbrot .net中反射、emit、expression和dynamic的性能比较 const string 和 static readonly string的区别 SqlServer: Top N per Group 微软的BinarySearch 通过CTE实现Split CSV 通过SQL CTE计算Fibonacci 当json.js遇见dynamic.net烂尾篇 .NET线程安全泛型Singleton 跨域访问Cookie Windows安装Memcached node.js初体验 教你如何制作Silverlight Visual Tree Inspector 一道非常有趣的概率题 教你30秒打造强类型ASP.NET数据绑定 当json.js遇见dynamic.net [0] 用Silverlight做雷达图 C#运算符重载不是没有用武之地 随机排列算法
WCF JSON和AspnetCompatibility的配置
dragonpig · 2011-02-27 · via 博客园 - dragonpig

在WCF Service Application中加入<enableWebScript>

<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ajaxbehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="JsonWCF.StudentService">
<endpoint binding="webHttpBinding" contract="JsonWCF.IStudentService" behaviorConfiguration="ajaxbehavior" />
</service>
</services>
</system.serviceModel>

定义Contract,添加[WebGet] Attribute允许HTTP Get访问

[ServiceContract]
public interface IStudentService
{
[OperationContract]
[WebGet]
Student RandomStudent();
}

[DataContract]

public class Student
{
[DataMember]
public string FirstName { get; set; }

[DataMember]

public string LastName { get; set; }

[DataMember]

public DateTime DOB { get; set; }
}

ASP.NET Compatibility

WCF可以以ASP.NET的方式host,这样就可以使用httpmodule httpcontext等一些asp.net特有的流程

在配置文件中添加

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
...

定义Module (Namespace = JsonWCF)

public class MyModule : IHttpModule
{
public void Dispose()
{

}

public void Init(HttpApplication context)
{
context.BeginRequest
+= context_BeginRequest;
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Application[
"key"] = "nothing is impossible";
}
}

web.config添加module

<system.web>
<httpModules>
<add name="mymodule" type="JsonWCF.MyModule"/>
...

实现中就可以访问HttpApplication,注意需要为service标记AspNetCompatibilityRequirements,表示允许通过asp.net的方式访问

[AspNetCompatibilityRequirements(
RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class StudentService : IStudentService
{
public Student RandomStudent()
{
string firstname = "null";
if (HttpContext.Current != null)
firstname
= HttpContext.Current.Application["key"] as string;
return new Student { DOB = DateTime.Parse("1983/10/14"), FirstName = firstname, LastName = "Zhu" };
}
}

Remarks
如果将配置中的aspnetcompatibilityenabled改为false,wcf将不再走aspnet管道,application始终为空