无法使用WSDL定义

时间:2009-03-21 17:55:28

标签: c# java web-services wsdl svcutil.exe

问题的系统分解如下。 [重写!]

客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// This is a "sanitized" version of the real deal, of course. In reality I also require to
// sign all incomming and outgoing messages and com. over SSL. The basic model is the same
// though and the sanitized WSDL captures the problem in its minimal form.

namespace MissileDefenseSystem
{
    using MissileDefenseSystemServiceReference;

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var client = new TestPortTypeClient();
                var req = "just do it";
                Console.WriteLine("request>" + req + "<");
                var rsp = client.LaunchMissiles(req);
                Console.WriteLine("response>" + rsp + "<");
            }
            catch (Exception e)
            {
                Console.WriteLine("exception>" + e.Message + "<");
                Console.WriteLine(e.StackTrace);
            }
        }
    }
}

WSDL

<?xml version="1.0" encoding="ISO-8859-1"?>
<definitions targetNamespace="java:bla.bla.bla.bla"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:tns="java:bla.bla.bla.bla"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <types>
    <schema targetNamespace='java:bla.bla.bla.bla' xmlns='http://www.w3.org/2001/XMLSchema'/>
  </types>
  <message name="TestRequest">
    <part name="arg0" type="xsd:string"/>
  </message>
  <message name="TestResponse">
    <part name="return" type="xsd:string"/>
  </message>
  <portType name="TestPortType">
    <operation name="LaunchMissiles">
      <input message="tns:TestRequest"/>
      <output message="tns:TestResponse"/>
    </operation>
    <operation name="AbortMission">
      <input message="tns:TestRequest"/>
      <output message="tns:TestResponse"/>
    </operation>
  </portType>
  <binding name="TestBinding" type="tns:TestPortType">
    <soap:binding style="rpc"  transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="LaunchMissiles">
      <soap:operation soapAction="urn:LaunchMissiles"/>
      <input>
        <soap:body use="encoded" namespace='urn:Test' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </input>
      <output>
        <soap:body use="encoded" namespace='urn:Test' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </output>
    </operation>
    <operation name="AbortMission">
      <soap:operation soapAction="urn:AbortMission"/>
      <input>
        <soap:body use="encoded" namespace='urn:Test' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </input>
      <output>
        <soap:body use="encoded" namespace='urn:Test' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </output>
    </operation>
  </binding>
  <service name="Test">
    <documentation>todo</documentation>
    <port name="TestPort" binding="tns:TestBinding">
      <soap:address location="https://demo.blablablablablabla.com:123/Bla"/>
    </port>
  </service>
</definitions>

错误跟踪

request>just do it<
exception>RPC Message LaunchMissilesRequest in operation AbortMission has an invalid body name LaunchMissiles. It must be AbortMission<
   at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.OperationReflector.EnsureMessageInfos()
   at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.EnsureMessageInfos()
   at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.OperationReflector.get_Request()
   at System.ServiceModel.Description.XmlSerializerOperationBehavior.CreateFormatter()
   at System.ServiceModel.Description.XmlSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
   at System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch)
   at System.ServiceModel.Description.DispatcherBuilder.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime)
   at System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint serviceEndpoint, BindingParameterCollection& parameters)
   at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint)
   at System.ServiceModel.ChannelFactory.CreateFactory()
   at System.ServiceModel.ChannelFactory.OnOpening()
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ChannelFactory.EnsureOpened()
   at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannelInternal()
   at System.ServiceModel.ClientBase`1.get_Channel()
   at MissileDefenseSystem.MissileDefenseSystemServiceReference.TestPortTypeClient.MissileDefenseSystem.MissileDefenseSystemServiceReference.TestPortType.LaunchMissiles(LaunchMissilesRequest request) in C:\Users\bra\Documents\Visual Studio 2008\Projects\MissileDefenseSystem\MissileDefenseSystem\Service References\MissileDefenseSystemServiceReference\Reference.cs:line 90
   at MissileDefenseSystem.MissileDefenseSystemServiceReference.TestPortTypeClient.LaunchMissiles(String arg0) in C:\Users\bra\Documents\Visual Studio 2008\Projects\MissileDefenseSystem\MissileDefenseSystem\Service References\MissileDefenseSystemServiceReference\Reference.cs:line 96
   at MissileDefenseSystem.Program.Main(String[] args) in C:\Users\bra\Documents\Visual Studio 2008\Projects\MissileDefenseSystem\MissileDefenseSystem\Program.cs:line 22

生成的代理代码,使用Visual Studio 2008(SvcUtil.exe)。

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.3074
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------



[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="java:bla.bla.bla.bla", ConfigurationName="TestPortType")]
public interface TestPortType
{

    // CODEGEN: Generating message contract since the wrapper namespace (urn:Test) of message LaunchMissilesRequest does not match the default value (java:bla.bla.bla.bla)
    [System.ServiceModel.OperationContractAttribute(Action="urn:LaunchMissiles", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute(Style=System.ServiceModel.OperationFormatStyle.Rpc, Use=System.ServiceModel.OperationFormatUse.Encoded)]
    LaunchMissilesResponse LaunchMissiles(LaunchMissilesRequest request);

    // CODEGEN: Generating message contract since the wrapper namespace (urn:Test) of message AbortMissionRequest does not match the default value (java:bla.bla.bla.bla)
    [System.ServiceModel.OperationContractAttribute(Action="urn:AbortMission", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute(Style=System.ServiceModel.OperationFormatStyle.Rpc, Use=System.ServiceModel.OperationFormatUse.Encoded)]
    LaunchMissilesResponse AbortMission(LaunchMissilesRequest request);
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName="LaunchMissiles", WrapperNamespace="urn:Test", IsWrapped=true)]
public partial class LaunchMissilesRequest
{

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]
    public string arg0;

    public LaunchMissilesRequest()
    {
    }

    public LaunchMissilesRequest(string arg0)
    {
        this.arg0 = arg0;
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName="LaunchMissilesResponse", WrapperNamespace="urn:Test", IsWrapped=true)]
public partial class LaunchMissilesResponse
{

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]
    public string @return;

    public LaunchMissilesResponse()
    {
    }

    public LaunchMissilesResponse(string @return)
    {
        this.@return = @return;
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface TestPortTypeChannel : TestPortType, System.ServiceModel.IClientChannel
{
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class TestPortTypeClient : System.ServiceModel.ClientBase<TestPortType>, TestPortType
{

    public TestPortTypeClient()
    {
    }

    public TestPortTypeClient(string endpointConfigurationName) : 
            base(endpointConfigurationName)
    {
    }

    public TestPortTypeClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public TestPortTypeClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public TestPortTypeClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    LaunchMissilesResponse TestPortType.LaunchMissiles(LaunchMissilesRequest request)
    {
        return base.Channel.LaunchMissiles(request);
    }

    public string LaunchMissiles(string arg0)
    {
        LaunchMissilesRequest inValue = new LaunchMissilesRequest();
        inValue.arg0 = arg0;
        LaunchMissilesResponse retVal = ((TestPortType)(this)).LaunchMissiles(inValue);
        return retVal.@return;
    }

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    LaunchMissilesResponse TestPortType.AbortMission(LaunchMissilesRequest request)
    {
        return base.Channel.AbortMission(request);
    }

    public string AbortMission(string arg0)
    {
        LaunchMissilesRequest inValue = new LaunchMissilesRequest();
        inValue.arg0 = arg0;
        LaunchMissilesResponse retVal = ((TestPortType)(this)).AbortMission(inValue);
        return retVal.@return;
    }
}

这是使用 SvcUtil.exe 生成的代理代码。

在此处下载完整的Visual Studio项目文件:

http://dl.getdropbox.com/u/797094/MissileDefenseSystem.zip

现在,我刚刚尝试再次使用WSDL.exe。这样做效果更好,但现在下一个问题就是丑陋的头脑。未使用WCF的app.config。所以我需要配置代理类来使用证书。对于WCF我只是说

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
              <binding name="TestBinding"/>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://demo.blablablablablabla.com:123/Bla"
                binding="basicHttpBinding" bindingConfiguration="TestBinding"
                contract="MissileDefenseSystemServiceReference.TestPortType"
                name="TestPort">
                <identity>
                    <certificateReference storeLocation="CurrentUser" x509FindType="FindByThumbprint"
                        findValue="the thumbprint to be used" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

好的,现在使用一段代码来提供签名

    proxy.ClientCertificates.Add(cert);

现在这几乎可以正常工作,除了它无法解码响应。

1 个答案:

答案 0 :(得分:2)

这取决于你期望得到什么。这反映了您如何定义wsdl。例如,如果用java:bla.bla.bla.bla替换所有“urn:Test”,你会得到一个更简单的定义,只接收/返回字符串。

如果您仍想获得2种不同的请求/响应类型,则可以使用以下定义:

<?xml version="1.0" encoding="ISO-8859-1"?>
<definitions targetNamespace="java:bla.bla.bla.bla"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:tns="java:bla.bla.bla.bla"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <types>
    <schema targetNamespace='java:bla.bla.bla.bla' xmlns='http://www.w3.org/2001/XMLSchema'/>
  </types>
  <message name="AbortTestRequest">
    <part name="arg0" type="xsd:string"/>
  </message>
  <message name="AbortTestResponse">
    <part name="return" type="xsd:string"/>
  </message>
  <message name="LaunchTestRequest">
    <part name="arg0" type="xsd:string"/>
  </message>
  <message name="LaunchTestResponse">
    <part name="return" type="xsd:string"/>
  </message>
  <portType name="TestPortType">
    <operation name="LaunchMissiles">
      <input message="tns:LaunchTestRequest"/>
      <output message="tns:LaunchTestResponse"/>
    </operation>
    <operation name="AbortMission">
      <input message="tns:AbortTestRequest"/>
      <output message="tns:AbortTestResponse"/>
    </operation>
  </portType>
  <binding name="TestBinding" type="tns:TestPortType">
    <soap:binding style="rpc"  transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="LaunchMissiles">
      <soap:operation soapAction="urn:LaunchMissiles"/>
      <input>
        <soap:body use="encoded" namespace='java:bla.bla.bla.bla/launch' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </input>
      <output>
        <soap:body use="encoded" namespace='java:bla.bla.bla.bla/launch' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </output>
    </operation>
    <operation name="AbortMission">
      <soap:operation soapAction="urn:AbortMission"/>
      <input>
        <soap:body use="encoded" namespace='java:bla.bla.bla.bla/launch' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </input>
      <output>
        <soap:body use="encoded" namespace='java:bla.bla.bla.bla/launch' encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
      </output>
    </operation>
  </binding>
  <service name="Test">
    <documentation>todo</documentation>
    <port name="TestPort" binding="tns:TestBinding">
      <soap:address location="https://demo.blablablablablabla.com:123/Bla"/>
    </port>
  </service>
</definitions>

聚苯乙烯。因为您似乎想要所有操作/方法的请求/响应文档,您可能希望完全切换到文档样式。


关于评论无法进行重大更改。更改消息元素的名称空间或名称是重大更改。

那就是说,如果你只对它的工作感兴趣(并且不关心你所获得的课程的形式),你可以使用你拥有的版本。生成的类对两种方法都使用LaunchMissileResponse并不重要,因为底层XML将是相同的(TestResponse)。同样对于调用代码,请记住您有一个接收/发送简单字符串的版本:

public string AbortMission(string arg0)