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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
月光博客
月光博客
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LangChain Blog
博客园 - 聂微东
G
Google Developers Blog
博客园 - Franky

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