4

WCFを使い始めようとしています。非常にシンプルなはずですが、SOAP :: LiteのようなシンプルなAPIを使用すると、何かが機能するのに問題があります。googlesphereには時代遅れの例がたくさんあり、私はそれらの多くを無駄に試しました。だから今、私は問題を最低レベルに分解しようとしています。Visual Web Developer2008Expressテンプレートによって生成されたWCFアプリを作成しました。これはそのサービス契約です:

[ServiceContract]
public interface IService1
{

    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[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; }
    }
}

デバッグモードで起動し、連絡を試みました。デバッグURLhttp://localhost:59315/Service1.svcを使用してブラウザでこれを行うと、次のようなページが表示されます。

サービスを作成しました。

このサービスをテストするには、クライアントを作成し、それを使用してサービスを呼び出す必要があります。これは、コマンドラインからsvcutil.exeツールを使用して次の構文で実行できます。

svcutil.exehttp://localhost:59315/Service1.svc?wsdl

それで、私はそのwsdlにアクセスして、これを取得します。

<wsdl:definitions name="Service1" targetNamespace="http://tempuri.org/">
  <wsp:Policy wsu:Id="WSHttpBinding_IService1_policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <sp:SymmetricBinding>
          <wsp:Policy>
            <sp:ProtectionToken>
              <wsp:Policy>
                <sp:SecureConversationToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
                  <wsp:Policy>
                    <sp:RequireDerivedKeys/>
                    <sp:BootstrapPolicy>
                      <wsp:Policy>
                        <sp:SignedParts>
                          <sp:Body/>
                          <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/>
                          <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/>
                          <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/>
                          <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/>
                          <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/>
                          <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/>
                          <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/>
                        </sp:SignedParts>
                        <sp:EncryptedParts>
                          <sp:Body/>
                        </sp:EncryptedParts>
                        <sp:SymmetricBinding>
                          <wsp:Policy>
                            <sp:ProtectionToken>
                              <wsp:Policy>
                                <sp:SpnegoContextToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
                                  <wsp:Policy>
                                    <sp:RequireDerivedKeys/>
                                  </wsp:Policy>
                                </sp:SpnegoContextToken>
                              </wsp:Policy>
                            </sp:ProtectionToken>
                            <sp:AlgorithmSuite>
                              <wsp:Policy>
                                <sp:Basic256/>
                              </wsp:Policy>
                            </sp:AlgorithmSuite>
                            <sp:Layout>
                              <wsp:Policy>
                                <sp:Strict/>
                              </wsp:Policy>
                            </sp:Layout>
                            <sp:IncludeTimestamp/>
                            <sp:EncryptSignature/>
                            <sp:OnlySignEntireHeadersAndBody/>
                          </wsp:Policy>
                        </sp:SymmetricBinding>
                        <sp:Wss11>
                          <wsp:Policy>
                            <sp:MustSupportRefKeyIdentifier/>
                            <sp:MustSupportRefIssuerSerial/>
                            <sp:MustSupportRefThumbprint/>
                            <sp:MustSupportRefEncryptedKey/>
                          </wsp:Policy>
                        </sp:Wss11>
                        <sp:Trust10>
                          <wsp:Policy>
                            <sp:MustSupportIssuedTokens/>
                            <sp:RequireClientEntropy/>
                            <sp:RequireServerEntropy/>
                          </wsp:Policy>
                        </sp:Trust10>
                      </wsp:Policy>
                    </sp:BootstrapPolicy>
                  </wsp:Policy>
                </sp:SecureConversationToken>
              </wsp:Policy>
            </sp:ProtectionToken>
            <sp:AlgorithmSuite>
              <wsp:Policy>
                <sp:Basic256/>
              </wsp:Policy>
            </sp:AlgorithmSuite>
            <sp:Layout>
              <wsp:Policy>
                <sp:Strict/>
              </wsp:Policy>
            </sp:Layout>
            <sp:IncludeTimestamp/>
            <sp:EncryptSignature/>
            <sp:OnlySignEntireHeadersAndBody/>
          </wsp:Policy>
        </sp:SymmetricBinding>
        <sp:Wss11>
          <wsp:Policy>
            <sp:MustSupportRefKeyIdentifier/>
            <sp:MustSupportRefIssuerSerial/>
            <sp:MustSupportRefThumbprint/>
            <sp:MustSupportRefEncryptedKey/>
          </wsp:Policy>
        </sp:Wss11>
        <sp:Trust10>
          <wsp:Policy>
            <sp:MustSupportIssuedTokens/>
            <sp:RequireClientEntropy/>
            <sp:RequireServerEntropy/>
          </wsp:Policy>
        </sp:Trust10>
        <wsaw:UsingAddressing/>
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>
  <wsp:Policy wsu:Id="WSHttpBinding_IService1_GetData_Input_policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <sp:SignedParts>
          <sp:Body/>
          <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/>
        </sp:SignedParts>
        <sp:EncryptedParts>
          <sp:Body/>
        </sp:EncryptedParts>
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>
  <wsp:Policy wsu:Id="WSHttpBinding_IService1_GetData_output_policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <sp:SignedParts>
          <sp:Body/>
          <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/>
        </sp:SignedParts>
        <sp:EncryptedParts>
          <sp:Body/>
        </sp:EncryptedParts>
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>
  <wsp:Policy wsu:Id="WSHttpBinding_IService1_GetDataUsingDataContract_Input_policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <sp:SignedParts>
          <sp:Body/>
          <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/>
        </sp:SignedParts>
        <sp:EncryptedParts>
          <sp:Body/>
        </sp:EncryptedParts>
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>
  <wsp:Policy wsu:Id="WSHttpBinding_IService1_GetDataUsingDataContract_output_policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <sp:SignedParts>
          <sp:Body/>
          <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/>
          <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/>
        </sp:SignedParts>
        <sp:EncryptedParts>
          <sp:Body/>
        </sp:EncryptedParts>
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>
  <wsdl:types>
    <xsd:schema targetNamespace="http://tempuri.org/Imports">
      <xsd:import schemaLocation="http://localhost:59315/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
      <xsd:import schemaLocation="http://localhost:59315/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
      <xsd:import schemaLocation="http://localhost:59315/Service1.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/RossDominosService"/>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="IService1_GetData_InputMessage">
    <wsdl:part name="parameters" element="tns:GetData"/>
  </wsdl:message>
  <wsdl:message name="IService1_GetData_OutputMessage">
    <wsdl:part name="parameters" element="tns:GetDataResponse"/>
  </wsdl:message>
  <wsdl:message name="IService1_GetDataUsingDataContract_InputMessage">
    <wsdl:part name="parameters" element="tns:GetDataUsingDataContract"/>
  </wsdl:message>
  <wsdl:message name="IService1_GetDataUsingDataContract_OutputMessage">
    <wsdl:part name="parameters" element="tns:GetDataUsingDataContractResponse"/>
  </wsdl:message>
  <wsdl:portType name="IService1">
    <wsdl:operation name="GetData">
      <wsdl:input wsaw:Action="http://tempuri.org/IService1/GetData" message="tns:IService1_GetData_InputMessage"/>
      <wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataResponse" message="tns:IService1_GetData_OutputMessage"/>
    </wsdl:operation>
    <wsdl:operation name="GetDataUsingDataContract">
      <wsdl:input wsaw:Action="http://tempuri.org/IService1/GetDataUsingDataContract" message="tns:IService1_GetDataUsingDataContract_InputMessage"/>
      <wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataUsingDataContractResponse" message="tns:IService1_GetDataUsingDataContract_OutputMessage"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="WSHttpBinding_IService1" type="tns:IService1">
    <wsp:PolicyReference URI="#WSHttpBinding_IService1_policy"/>
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="GetData">
      <soap12:operation soapAction="http://tempuri.org/IService1/GetData" style="document"/>
      <wsdl:input>
        <wsp:PolicyReference URI="#WSHttpBinding_IService1_GetData_Input_policy"/>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <wsp:PolicyReference URI="#WSHttpBinding_IService1_GetData_output_policy"/>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetDataUsingDataContract">
      <soap12:operation soapAction="http://tempuri.org/IService1/GetDataUsingDataContract" style="document"/>
      <wsdl:input>
        <wsp:PolicyReference URI="#WSHttpBinding_IService1_GetDataUsingDataContract_Input_policy"/>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <wsp:PolicyReference URI="#WSHttpBinding_IService1_GetDataUsingDataContract_output_policy"/>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Service1">
    <wsdl:port name="WSHttpBinding_IService1" binding="tns:WSHttpBinding_IService1">
      <soap12:address location="http://localhost:59315/Service1.svc"/>
      <wsa10:EndpointReference>
        <wsa10:Address>http://localhost:59315/Service1.svc</wsa10:Address>
        <Identity>
          <Dns>localhost</Dns>
        </Identity>
      </wsa10:EndpointReference>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

(SOAP自体以外は)クレイジーなことは何もありません...とにかく、今は単純なSOAP::Liteアプリを試してみます。

#!/cygdrive/C/Perl64/bin/perl.exe -w

use strict;
use warnings;

use Data::Dumper;
use SOAP::Lite +trace => [ transport => sub {
    my ($in) = @_;
    if ( ref($in) eq "HTTP::Request") {
        print( "REQUEST\n" . $in->content() . "\nEND REQUEST\n" );
    } 
    elsif ( ref($in) eq "HTTP::Response") {
        print( "RESPONSE\n" . $in->content() . "\nEND RESPONSE\n" );
    }
} ];


my $server = 'http://localhost:59315/Service1.svc';
my $url = "$server?wsdl";
my $xmlns = 'http://schemas.datacontract.org/2004/07/RossDominosService';

# Setup Network Connection
my $service = SOAP::Lite
    ->ns( $url )
    ->proxy( $server )
    ->on_fault( sub {
        my ($soap, $res) = @_;
        die ref $res ? $res->faultstring() : $soap->transport()->status(), "\n";
    } );
;

print( Dumper( $service ) );

eval {
    print( "making request\n" );
    my $response = $service->GetData( 
        SOAP::Data->new( name => 'value', value => 5 ) 
    );
    print( "got response:\n$response\n" );
};
if ( $@ ) {
    print( "failed:\n**************************\n$@\n*****************************\n" );
}

デバッグからこのSOAP要求を発行しているのはどれですか。

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:namesp1="http://localhost:59315/Service1.svc?wsdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <namesp1:GetData>
      <value xsi:type="xsd:int">5</value>
    </namesp1:GetData>
  </soap:Body>
</soap:Envelope>

しかし、応答は次のように返されます。

failed:
**************************
415 Unsupported Media Type

*****************************

何が起こっている?これが「Hello、World!」です。のSOAPアプリと私はそれを理解することはできません。

- - - - - - - - - - - -アップデート - - - - - - - - - - - - - -----

wsHttpBindingバインディングをデフォルトのから次のように変更する必要があることがわかりました。

    <endpoint address="" binding="basicHttpBinding" 
              contract="RossDominosService.IService1">

接続しているように見えますが、次の新しいエラーが発生します。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <s:Fault>
      <faultcode xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</faultcode>
      <faultstring xml:lang="en-US">The message with Action 'http://tempuri.org/GetData' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).</faultstring>
    </s:Fault>
  </s:Body>
</s:Envelope>

これはそれほど難しいことではないようです...

- - - - - - - - - - - -アップデート - - - - - - - - - - - - - -----

とった!さらにいじくり回し、 soapUIからの支援を受けた後、アクションを次のように変更する必要があることがわかりました。

<namespace>/<serviceContract>/<method>

この場合:

http://tempuri.org/IService1/GetData

SOAP :: Liteを使用する場合、デフォルトのSOAPActionは

<namespace>#<method>

したがって、SOAPオブジェクトを次のように変更する必要があります。

$soap->on_action( sub { sprintf( '%sIService1/%s', @_ ) } );

この質問を閉じることができるように、以下に回答を書きます...

4

1 に答える 1

2

これは、Perl SOAP::Lite クライアントで WCF Web サービスを起動して実行する方法です。 私は Visual Web Developer Express 2008 と perl 5.14 と SOAP::Lite 0.715 を使用していることに注意してください。

  1. Visual Web Developer を開く
  2. ファイル -> 新しいプロジェクト...
    1. ビジュアル C# -> Web
    2. WCF サービス アプリケーション
    3. 名前: WcfService1
    4. 位置:
    5. 解決策: 新しい解決策
    6. ソリューション名:
  3. Web.config を開く
    • <compilation debug="false">に変更<compilation debug="true">
    • <endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1">に変更<endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1">
  4. Service1.svc.cs を開く
    • f5 を押します (デバッグを開始します)。

これで、ブラウザ ウィンドウが開き、サービス ページが表示されます。URL のポートに注意してください。クライアントで必要になります。次に、このスクリプトを作成します。

#!/cygdrive/C/Perl64/bin/perl.exe -w

use strict;
use warnings;

use Data::Dumper;
use SOAP::Lite +trace => [ transport => sub {
    my ($in) = @_;
    if ( ref($in) eq "HTTP::Request") {
        print( "**** REQUEST ****\n" . $in->content() . "\n**** END REQUEST ****\n" );
    } 
    elsif ( ref($in) eq "HTTP::Response") {
        print( "**** RESPONSE ****\n" . $in->content() . "\n**** END RESPONSE ****\n" );
    }
} ];

my $port = 63181;
my $server = "http://localhost:$port/Service1.svc";
my $namespace = 'http://tempuri.org/';

# Setup Network Connection
my $service = SOAP::Lite
    ->ns( $namespace, 'my' )
    ->proxy( $server )
    ->on_action( sub { 
        my $action = sprintf( '%sIService1/%s', @_ );
        print( "action: '$action'\n" ); 
        return $action;
    } );

print( Dumper( $service ) );

eval {
    print( "making request\n" );
    my $response = $service->GetData( 
        SOAP::Data->new( prefix => 'my', name => 'value', value => 5 ) 
    );
    print( "got response:\n$response\n" );
};
if ( $@ ) {
    print( "failed:\n**************************\n$@\n*****************************\n" );
}

ポート番号を一致するようにポート番号を変更する必要があります。また、何が起こっているのかを確認できるように、デバッグ コードを残しました。

これが他の誰かに役立つことを願っています。

于 2012-09-04T00:34:14.570 に答える