6

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で正しく設定されていれば、サービス呼び出しごとに自動的に呼び出されると思いました。

4

2 に答える 2

5

サービス契約とサービスの実装が一致していないようです....

stringサービス コントラクトは、から返される単一の を定義しGetColors()ます。

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    string GetColors();
}

ただし、実装はList<Color>代わりに a を返します。

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class TestService : ITestService
{
    public List<Color> GetColors()
    {

サービスの実装を変更して、サービス契約を更新するのを忘れた可能性はありますか??

また、これは を含むSOAPサービスであるためwsHttpBinding、ブラウザからそのアドレスを参照してメソッドを呼び出すことはできません.WCFテストクライアントを使用する必要があります(あなたが言うように動作します)。また、Fiddler から呼び出すこともできません。ヘッダーと本文を含む SOAP エンベロープ全体を手動で作成する必要があり、簡単な作業ではありません。

試してみることができるのはhttp://localhost/TestService.svc?wsdl、サービスの適切な WSDL が返されるかどうかを確認することです。

于 2011-05-13T18:38:58.300 に答える
0

多くのことが原因である可能性があります。最初に WCF トレースを有効にすることをお勧めします。実際に何が起こっているかをサーバー側で出力できるため、通常は非常に役立ちます。ここに役立つリンクがあります:WCFトレースを有効にする方法

編集: WCF で注意すべき点が 1 つあります。自動生成された ole' ASMX Web サービスとは異なり、標準のブラウザーでベース アドレスを参照することはできません。メタデータ アドレス (mex アドレス) を参照できますが、ルートは参照できません。これは設計によるものです (HTTP メソッドはサポートされていないと思います)。

于 2011-05-13T17:14:10.773 に答える