WCFのWebアクセス方式
8824 ワード
.NET 3.5以降、WCFではWebGet方式が提供され、url形式でWebサービスへのアクセスが許可される.以前のコードでは、似たような例を何度も書きましたが、構成方法を忘れがちで、設定手順を以下に記録します. endpoint通信プロトコルwebHttpBinding に設定 endpointの動作は に設定.インタフェースにWebGetのAttributes を追加
サンプルコードは次のとおりです.
web.configファイルの構成
WCFインタフェースの設定は、URIテンプレート(UriTemplate)とJSON(
WebMessageFormat.Json)のサポート:
テスト:
IEブラウザを開き、アドレスバーに入力:
http://localhost:3000/Services/ShowerService.svc/hello/abc、アクセス後の結果が表示されます.
デバッグ:
Web.configの
に変更
は、ブラウザページで使用可能なインタフェースを列挙し、コミットされたデータサンプルを提供します.
IEブラウザを開き、アドレスバーに入力:
http://localhost:3000/Services/ShowerService.svc/helpでいいです.
Siverlightアクセス:
SLのWebClientを使ってWebInvokeメソッドにアクセスする場合は、HttpRequestHeader.ContentTypeはアプリケーション/jsonに設定されており、コードは以下の通りです.
サンプルコードは次のとおりです.
web.configファイルの構成
<
system.serviceModel
>
<
services
>
<
service
name
="Services.ShowerService"
>
<
endpoint
binding
="webHttpBinding"
behaviorConfiguration
="WebBehavior"
contract
="Services.IShowerService"
/>
</
service
>
</
services
>
<
behaviors
>
<
endpointBehaviors
>
<
behavior
name
="WebBehavior"
>
<
webHttp
/>
</
behavior
>
</
endpointBehaviors
>
<
serviceBehaviors
>
<
behavior
name
=""
>
<
serviceMetadata
httpGetEnabled
="true"
/>
<
serviceDebug
includeExceptionDetailInFaults
="false"
/>
</
behavior
>
</
serviceBehaviors
>
</
behaviors
>
</
system.serviceModel
>
WCFインタフェースの設定は、URIテンプレート(UriTemplate)とJSON(
WebMessageFormat.Json)のサポート:
namespace
Services
{
[ServiceContract]
public
interface
ShowerService
{
[OperationContract]
[WebGet(UriTemplate
=
"
/Hello/{name}
"
, RequestFormat
=
WebMessageFormat.Json, ResponseFormat
=
WebMessageFormat.Json)]
string
Hello(
string
name);
}
}
テスト:
IEブラウザを開き、アドレスバーに入力:
http://localhost:3000/Services/ShowerService.svc/hello/abc、アクセス後の結果が表示されます.
デバッグ:
Web.configの
IEブラウザを開き、アドレスバーに入力:
http://localhost:3000/Services/ShowerService.svc/helpでいいです.
Siverlightアクセス:
SLのWebClientを使ってWebInvokeメソッドにアクセスする場合は、HttpRequestHeader.ContentTypeはアプリケーション/jsonに設定されており、コードは以下の通りです.
WebClient client
=
new
WebClient();
client.Headers[HttpRequestHeader.ContentType]
=
"
application/json
"
;