WCF JSONとAspnetCompatibilityの構成
12960 ワード
WCFサービスアプリケーションにを追加
Contractを定義し、[WebGet]Attributeを追加してHTTP Getアクセスを許可する
ASP.NET Compatibility
WCFはASP.NET方式hostは、httpmodule httpcontextなどのaspを使用することができる.Net特有の流れ
プロファイルに追加
Moduleの定義(Namespace=JsonWCF)
web.config moduleの追加
実装ではHttpApplicationにアクセスすることができるが、サービスにAspNetCompatibilityRequirementsをマークし、aspを許可することを示す必要がある.Net方式アクセス
Remarks構成のaspnetcompatibilityenabledをfalseに変更すると、wcfはaspnetパイプラインを移動せず、アプリケーションは常に空になります.
<
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にアクセスすることができるが、サービスに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パイプラインを移動せず、アプリケーションは常に空になります.