Когда я вызываю http://localhost/TestService.svc/GetColors, я получаю Http (400) Bad Запрос. Когда я запускаю это в скрипте, я также получаю неверный запрос. Когда я вызываю сервис через тестовый клиент 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
. Если я сделаю это через среду разработки, я увижу неверный запрос Http 400. Если я сделаю это через IIS, я просто увижу пустую страницу.
Вот InnerException:
<ExceptionString>System.Xml.XmlException: The body of the message cannot be read
because it is empty.</ExceptionString>
Еще один вопрос относительно моего CustomUserNameValidation:
Я реализую настраиваемую проверку с помощью метода проверки UserNamePasswordValidator, но когда я вызываю что-то вроде GetColors через клиент wcf, он не вызывает метод проверки. Единственный способ получить его для Invoke validate - это вызвать Validate (user, pass) непосредственно в GetColors. Я думал, что он будет вызываться автоматически при каждом вызове службы, если он правильно настроен в web.config.