WCF Service Application
ASP.NETでOData、データソースにAccessファイルとか、いろいろとやりたいことがあるのですが、環境がすぐに用意できなかったり、うまくいかなかったりしたため、とりあえず基本となるWCFの部分だけ、メモっておこうと思います。
といっても、デフォルトである程度動くと見込んだものの、ちょっと戸惑いました。
環境 : Visual Studio 2013 / Windows 8.1

WCFサービスアプリケーションで新規プロジェクトを作り、何も変更せずに実行しました。
WCFテストクライアントが立ち上がり、リクエスト、レスポンスのテストができます。

ブラウザから、GETリクエストでレスポンスを得るために、以下のようにGetData2メソッドを追加しました。
(この追加をすると、上記のWCFテストクライアントでのテストはできなくなりました。自動でたちあがらくなり、コマンドwcftestclient.exe直接実行でもエラーがでます)
Service1.csv.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService01
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
[WebGet(UriTemplate = "data/{value}")]
public string GetData2(String value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService01
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
string GetData2(String value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
まず以下のようにデフォルトの画面を確認して、

パラメータを追加してみます。

と表示するはずですが、以下のように設定を追加していないとBad Request となり白い画面になってしまいます。
Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<!--追加 -->
<services>
<service name="WcfService01.Service1">
<endpoint address=""
behaviorConfiguration="restBehavior"
binding="webHttpBinding"
contract="WcfService01.IService1" />
</service>
</services>
<behaviors>
<!-- 追加 -->
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
これにより、XML POSTリクエストも可能になります。
環境 : Cygwin / Windows 8.1

最初、レスポンスが得られないとき、URLが違うのか、このやり方が違うのか迷いました。
結局、うまくいっているサンプルを探し、そのWeb.configファイルと比較したら、上のような違いをみつけました。
(Microsoftのプロダクトは、権限とか設定とかデフォルトで無効になっているものがよくあるように思います。
セキュリティを高めるためだとは思いますが、ちょっと動かしてみたいというときにできないと、次に進めなくてあきらめてしまいがちです。)
できたところまで、とりあえずメモでした。
