http://localhost/TestService.svc/GetColorsを呼び出すと、Http(400)BadRequestが表示されます。これをフィドラーで実行すると、BadRequestも取得します。ただし、wcfテストクライアントを介してサービスを呼び出すと、正常に機能します。何が原因でしょうか?
サービス契約:
[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetColors();
}
ITestServiceの実装:
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class TestService : ITestService
{
public List<Color> GetColors()
{
List<Color> colors= new List<Color>();
colors.Add(new Color { Name = "Red", Code = "123" });
colors.Add(new Color { Name = "Blue", Code = "323" });
colors.Add(new Color { Name = "Green", Code = "3394" });
return colors;
}
}
これが私のweb.configです。
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="CustomAuthentication">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<services>
<service name="TestService.TestService"
behaviorConfiguration="CustomValidator" >
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="CustomAuthentication"
contract="TestService.ITestService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/TestService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CustomValidator">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="TestService.CustomUserNameValidator, TestService"/>
<serviceCertificate findValue="Test" storeLocation="LocalMachine"
storeName="My" x509FindType="FindBySubjectName"/>
</serviceCredentials>
<serviceMetadata httpGetEnabled="True"/>
</behavior>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
それを呼び出すときは、任意のブラウザを開いてURLを入力するだけhttp://localhost/TestService.svc/GetColors
です。開発環境でこれを行うと、Http400の不正なリクエストが表示されます。IISを介して実行すると、空白のページが表示されます。
InnerExceptionは次のとおりです。
<ExceptionString>System.Xml.XmlException: The body of the message cannot be read
because it is empty.</ExceptionString>
CustomUserNameValidationに関する別の質問:
UserNamePasswordValidatorのValidateメソッドを使用してカスタム検証を実装していますが、wcfクライアントを介してGetColorsなどを呼び出すと、Validateメソッドが呼び出されません。Invoke validateを取得する唯一の方法は、GetColorsでValidate(user、pass)を直接呼び出すことです。web.configで正しく設定されていれば、サービス呼び出しごとに自動的に呼び出されると思いました。