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

推荐订阅源

GbyAI
GbyAI
The GitHub Blog
The GitHub Blog
小众软件
小众软件
美团技术团队
博客园 - 司徒正美
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
J
Java Code Geeks
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
V
V2EX
C
Check Point Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog

博客园 - 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始终为空