Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带Java客户端null返回的SOAP C#服务器_C#_Java_Web Services_Soap - Fatal编程技术网

带Java客户端null返回的SOAP C#服务器

带Java客户端null返回的SOAP C#服务器,c#,java,web-services,soap,C#,Java,Web Services,Soap,我想检查不同平台之间的SOAP互操作性;我是一名Java程序员,不太懂C语言 我在C#中做了些什么: 传输对象: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml.Serialization; [XmlRoot] public class Pack { private int id; private string name;

我想检查不同平台之间的SOAP互操作性;我是一名Java程序员,不太懂C语言

我在C#中做了些什么:

传输对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

[XmlRoot]
public class Pack
{
    private int id;
    private string name;

    public Pack()
    {   
    }

    public Pack(int anId, string aName)
    {
        this.id = anId;
        this.name = aName;
    }

    public void setId(int anId)
    {
        this.id = anId;
    }

    public int getId()
    {
        return id;
    }

    public void setName(string aName)
    {
        this.name = aName;
    }

    public string getName()
    {
        return name;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PackWS : System.Web.Services.WebService
{
    private Dictionary<int, Pack> dictionary;

    public PackWS()
    {
        dictionary = new Dictionary<int, Pack>();
    }

    [WebMethod]
    [XmlInclude(typeof(String))]
    public String getName()
    {
        return "Goodbye world!";
    }

    [WebMethod]
    [XmlInclude(typeof(Pack))]
    public void addPackage(Pack package)
    {
       dictionary.Add(package.getId(), package);
    }

    [WebMethod]
    [XmlInclude(typeof(Pack))]
    public Pack getPackage(int id)
    {
        return dictionary[id];
    }
}
package user;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Pack {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="getName">
<s:complexType/>
</s:element>
<s:element name="getNameResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getNameResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="addPackage">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="package" nillable="true" type="tns:Pack"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="Pack"/>
<s:element name="addPackageResponse">
<s:complexType/>
</s:element>
<s:element name="getPackage">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="id" type="s:int"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getPackageResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="getPackageResult" nillable="true" type="tns:Pack"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="getNameSoapIn">
<wsdl:part name="parameters" element="tns:getName"/>
</wsdl:message>
<wsdl:message name="getNameSoapOut">
<wsdl:part name="parameters" element="tns:getNameResponse"/>
</wsdl:message>
<wsdl:message name="addPackageSoapIn">
<wsdl:part name="parameters" element="tns:addPackage"/>
</wsdl:message>
<wsdl:message name="addPackageSoapOut">
<wsdl:part name="parameters" element="tns:addPackageResponse"/>
</wsdl:message>
<wsdl:message name="getPackageSoapIn">
<wsdl:part name="parameters" element="tns:getPackage"/>
</wsdl:message>
<wsdl:message name="getPackageSoapOut">
<wsdl:part name="parameters" element="tns:getPackageResponse"/>
</wsdl:message>
<wsdl:portType name="PackWSSoap">
<wsdl:operation name="getName">
<wsdl:input message="tns:getNameSoapIn"/>
<wsdl:output message="tns:getNameSoapOut"/>
</wsdl:operation>
<wsdl:operation name="addPackage">
<wsdl:input message="tns:addPackageSoapIn"/>
<wsdl:output message="tns:addPackageSoapOut"/>
</wsdl:operation>
<wsdl:operation name="getPackage">
<wsdl:input message="tns:getPackageSoapIn"/>
<wsdl:output message="tns:getPackageSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PackWSSoap" type="tns:PackWSSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getName">
<soap:operation soapAction="http://tempuri.org/getName" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addPackage">
<soap:operation soapAction="http://tempuri.org/addPackage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPackage">
<soap:operation soapAction="http://tempuri.org/getPackage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="PackWSSoap12" type="tns:PackWSSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getName">
<soap12:operation soapAction="http://tempuri.org/getName" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addPackage">
<soap12:operation soapAction="http://tempuri.org/addPackage" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPackage">
<soap12:operation soapAction="http://tempuri.org/getPackage" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PackWS">
<wsdl:port name="PackWSSoap" binding="tns:PackWSSoap">
<soap:address location="http://localhost:57265/Service.asmx"/>
</wsdl:port>
<wsdl:port name="PackWSSoap12" binding="tns:PackWSSoap12">
<soap12:address location="http://localhost:57265/Service.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
网络服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

[XmlRoot]
public class Pack
{
    private int id;
    private string name;

    public Pack()
    {   
    }

    public Pack(int anId, string aName)
    {
        this.id = anId;
        this.name = aName;
    }

    public void setId(int anId)
    {
        this.id = anId;
    }

    public int getId()
    {
        return id;
    }

    public void setName(string aName)
    {
        this.name = aName;
    }

    public string getName()
    {
        return name;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PackWS : System.Web.Services.WebService
{
    private Dictionary<int, Pack> dictionary;

    public PackWS()
    {
        dictionary = new Dictionary<int, Pack>();
    }

    [WebMethod]
    [XmlInclude(typeof(String))]
    public String getName()
    {
        return "Goodbye world!";
    }

    [WebMethod]
    [XmlInclude(typeof(Pack))]
    public void addPackage(Pack package)
    {
       dictionary.Add(package.getId(), package);
    }

    [WebMethod]
    [XmlInclude(typeof(Pack))]
    public Pack getPackage(int id)
    {
        return dictionary[id];
    }
}
package user;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Pack {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="getName">
<s:complexType/>
</s:element>
<s:element name="getNameResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getNameResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="addPackage">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="package" nillable="true" type="tns:Pack"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="Pack"/>
<s:element name="addPackageResponse">
<s:complexType/>
</s:element>
<s:element name="getPackage">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="id" type="s:int"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getPackageResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="getPackageResult" nillable="true" type="tns:Pack"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="getNameSoapIn">
<wsdl:part name="parameters" element="tns:getName"/>
</wsdl:message>
<wsdl:message name="getNameSoapOut">
<wsdl:part name="parameters" element="tns:getNameResponse"/>
</wsdl:message>
<wsdl:message name="addPackageSoapIn">
<wsdl:part name="parameters" element="tns:addPackage"/>
</wsdl:message>
<wsdl:message name="addPackageSoapOut">
<wsdl:part name="parameters" element="tns:addPackageResponse"/>
</wsdl:message>
<wsdl:message name="getPackageSoapIn">
<wsdl:part name="parameters" element="tns:getPackage"/>
</wsdl:message>
<wsdl:message name="getPackageSoapOut">
<wsdl:part name="parameters" element="tns:getPackageResponse"/>
</wsdl:message>
<wsdl:portType name="PackWSSoap">
<wsdl:operation name="getName">
<wsdl:input message="tns:getNameSoapIn"/>
<wsdl:output message="tns:getNameSoapOut"/>
</wsdl:operation>
<wsdl:operation name="addPackage">
<wsdl:input message="tns:addPackageSoapIn"/>
<wsdl:output message="tns:addPackageSoapOut"/>
</wsdl:operation>
<wsdl:operation name="getPackage">
<wsdl:input message="tns:getPackageSoapIn"/>
<wsdl:output message="tns:getPackageSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PackWSSoap" type="tns:PackWSSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getName">
<soap:operation soapAction="http://tempuri.org/getName" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addPackage">
<soap:operation soapAction="http://tempuri.org/addPackage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPackage">
<soap:operation soapAction="http://tempuri.org/getPackage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="PackWSSoap12" type="tns:PackWSSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getName">
<soap12:operation soapAction="http://tempuri.org/getName" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addPackage">
<soap12:operation soapAction="http://tempuri.org/addPackage" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPackage">
<soap12:operation soapAction="http://tempuri.org/getPackage" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PackWS">
<wsdl:port name="PackWSSoap" binding="tns:PackWSSoap">
<soap:address location="http://localhost:57265/Service.asmx"/>
</wsdl:port>
<wsdl:port name="PackWSSoap12" binding="tns:PackWSSoap12">
<soap12:address location="http://localhost:57265/Service.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
界面:

package user;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(targetNamespace = "http://tempuri.org/",
        portName = "PackWSSoap",
        wsdlLocation = "http://localhost:57265/Service.asmx?wsdl",
        serviceName = "PackWS")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface PackWSSoap {
    void addPackage(Pack pack);
    Pack getPackage(int id);
    String getName();
}
服务

package user;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL;

public class PackageClient {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://localhost:57265/Service.asmx?wsdl");
            QName qName = new QName("http://tempuri.org/", "PackWS");

            Service service = Service.create(url, qName);

            PackWSSoap packWS = service.getPort(PackWSSoap.class);

            System.out.println(packWS.getName());//null here

            packWS.addPackage(getPack());//C# exception here

            System.out.println(packWS.getPackage(getPack().getId()));//unreachable 


        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    static Pack getPack() {
        return new Pack(){{
            setId(2);
            setName("name");
        }
        };
    }
}
调用
getName()
方法时,它应该返回一个简单字符串,但返回null。当试图通过调用
addPackage(pack)
来添加包时,我从客户机上的C#接收到一个异常,使用Java:

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at PackWS.addPackage(Pack package) in f:\DS Projects\ds5\NET\App_Code\PackWS.cs:line 38
   --- End of inner exception stack trace ---
    at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:163)
    at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:94)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:236)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:206)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:103)
    at $Proxy18.addPackage(Unknown Source)
    at user.PackageClient.main(PackageClient.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
我不认为沟通有任何问题,因为例外情况通过得很好;但我相信我没有向.NET服务器添加任何内容。如果您有任何帮助,我们将不胜感激,并为这篇冗长的文章感到抱歉

以下是WSDL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

[XmlRoot]
public class Pack
{
    private int id;
    private string name;

    public Pack()
    {   
    }

    public Pack(int anId, string aName)
    {
        this.id = anId;
        this.name = aName;
    }

    public void setId(int anId)
    {
        this.id = anId;
    }

    public int getId()
    {
        return id;
    }

    public void setName(string aName)
    {
        this.name = aName;
    }

    public string getName()
    {
        return name;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PackWS : System.Web.Services.WebService
{
    private Dictionary<int, Pack> dictionary;

    public PackWS()
    {
        dictionary = new Dictionary<int, Pack>();
    }

    [WebMethod]
    [XmlInclude(typeof(String))]
    public String getName()
    {
        return "Goodbye world!";
    }

    [WebMethod]
    [XmlInclude(typeof(Pack))]
    public void addPackage(Pack package)
    {
       dictionary.Add(package.getId(), package);
    }

    [WebMethod]
    [XmlInclude(typeof(Pack))]
    public Pack getPackage(int id)
    {
        return dictionary[id];
    }
}
package user;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Pack {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="getName">
<s:complexType/>
</s:element>
<s:element name="getNameResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="getNameResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="addPackage">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="package" nillable="true" type="tns:Pack"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="Pack"/>
<s:element name="addPackageResponse">
<s:complexType/>
</s:element>
<s:element name="getPackage">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="id" type="s:int"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="getPackageResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="getPackageResult" nillable="true" type="tns:Pack"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="getNameSoapIn">
<wsdl:part name="parameters" element="tns:getName"/>
</wsdl:message>
<wsdl:message name="getNameSoapOut">
<wsdl:part name="parameters" element="tns:getNameResponse"/>
</wsdl:message>
<wsdl:message name="addPackageSoapIn">
<wsdl:part name="parameters" element="tns:addPackage"/>
</wsdl:message>
<wsdl:message name="addPackageSoapOut">
<wsdl:part name="parameters" element="tns:addPackageResponse"/>
</wsdl:message>
<wsdl:message name="getPackageSoapIn">
<wsdl:part name="parameters" element="tns:getPackage"/>
</wsdl:message>
<wsdl:message name="getPackageSoapOut">
<wsdl:part name="parameters" element="tns:getPackageResponse"/>
</wsdl:message>
<wsdl:portType name="PackWSSoap">
<wsdl:operation name="getName">
<wsdl:input message="tns:getNameSoapIn"/>
<wsdl:output message="tns:getNameSoapOut"/>
</wsdl:operation>
<wsdl:operation name="addPackage">
<wsdl:input message="tns:addPackageSoapIn"/>
<wsdl:output message="tns:addPackageSoapOut"/>
</wsdl:operation>
<wsdl:operation name="getPackage">
<wsdl:input message="tns:getPackageSoapIn"/>
<wsdl:output message="tns:getPackageSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PackWSSoap" type="tns:PackWSSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getName">
<soap:operation soapAction="http://tempuri.org/getName" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addPackage">
<soap:operation soapAction="http://tempuri.org/addPackage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPackage">
<soap:operation soapAction="http://tempuri.org/getPackage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="PackWSSoap12" type="tns:PackWSSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getName">
<soap12:operation soapAction="http://tempuri.org/getName" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addPackage">
<soap12:operation soapAction="http://tempuri.org/addPackage" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getPackage">
<soap12:operation soapAction="http://tempuri.org/getPackage" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PackWS">
<wsdl:port name="PackWSSoap" binding="tns:PackWSSoap">
<soap:address location="http://localhost:57265/Service.asmx"/>
</wsdl:port>
<wsdl:port name="PackWSSoap12" binding="tns:PackWSSoap12">
<soap12:address location="http://localhost:57265/Service.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


NET中的Web服务不在请求之间维护会话状态。当您初始化字典时,它仅在该特定请求期间存在。在随后的请求中,它再次为null,因此您将获得异常

如果您想使用在请求之间持久化的对象实现这样的服务,您可以将应用程序用作共享存储。不过,它看起来有点难看,因为访问它时必须键入cast

初始化:

Application["dictionary"] = new Dictionary<int, Pack>();
Application[“dictionary”]=newdictionary();
添加:

((Dictionary<int, Pack>)Application["dictionary"]).Add(package.getId(), package);
((Dictionary)应用程序[“Dictionary”])。添加(package.getId(),package);
阅读:

return ((Dictionary<int, Pack>)Application["dictionary"])[id];
return((Dictionary)应用程序[“Dictionary”])[id];

有关应用程序存储区(HttpApplicationState的一个实例)的更多信息,请访问

,如果没有严格必要,我将永远不会使用soap。我建议您使用RESTWeb服务,将JSON对象放入主体中

您的.asmx文件(ASP.NET web服务)在哪里

您得到的异常来自PackWS.cs:第38行,它应该是getPackage()web方法

当我运行PackageClient时,我得到

Dec 22, 2012 7:13:50 AM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
INFO: Creating Service {http://tempuri.org/}PackWS from WSDL: http://10.0.1.32:49232/PackWS.asmx?wsdl
Dec 22, 2012 7:13:51 AM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
INFO: Creating Service {http://tempuri.org/}PackWS from WSDL: http://10.0.1.32:49232/PackWS.asmx?wsdl
Hello world!
在.NETWeb服务上,我得到了

System.Collections.Generic.KeyNotFoundException was unhandled by user code
  HResult=-2146232969
  Message=The given key was not present in the dictionary.
  Source=mscorlib
  StackTrace:
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at PackWS.getPackage(Int32 id) in c:\Users\Vineet\Documents\Visual Studio 2012\Projects\PackWS\PackWS\PackWS.asmx:line 39
  InnerException: 
要解决这个问题,请遵循Scott上面提到的方法

在签出.NET web服务的整个源代码

和Java客户端


谢谢你的回答;将信息保存到文件似乎是最简单的方法。您还知道为什么在调用[WebMethod][xmlclude(typeof(String))]public String getName(){返回“再见,世界!”}时会出现null。您是否在更新代码后重新部署了服务以仅返回字符串?我刚刚尝试了您的更新代码,我知道从大多数角度来看REST更好,但这是为了学习目的…只是一个小小的建议,不是为了解决您的问题,只是为了以后的维护,在您的WebService注释中,您使用的是远程wsdl位置,最好使用本地uri(示例:WEB-INF/WebContent/wsdl/Service.wsdl)看起来像是wsdl中的问题。
xmlns:soap12=”http://schemas.xmlsoap.org/wsdl/soap12/"
表示您正在使用soap 1.2。您的java客户端是soap 1.1。请尝试使用
wsimport
工具生成客户端,它通常会显示错误。您的问题不是您接收的
字符串不为null。C#web服务正在抛出错误。您应该在C#端获取堆栈跟踪,以查看null引用的位置发生异常。此外,从浏览器转到此处应该会提供一个表单,您可以在其中直接调用WebMethods。您可以使用该表单查看是否发生异常,以及堆栈跟踪是什么。