从c#客户端调用带有对象参数的Java Soap Web服务方法

本文关键字:Java Soap Web 方法 服务 参数 对象 客户端 调用 | 更新日期: 2023-09-27 18:15:10

我想传递Object的列表作为web服务的输入。我开始意识到我们不能使用SSIS中内置的Web服务任务来实现这一点。所以我尝试通过使用c#代码的脚本任务调用它。我能够通过脚本Task调用Java Web服务(SOAP)。我可以通过将简单的参数(如string)传递给web服务方法来进行测试。现在我想将对象列表作为参数传递给Web服务方法。为了测试的目的,我首先尝试传递一个对象。c#客户端的类如下

  [Serializable]
        public class Person
        {
            public string _PersonName;
            public string _PersonNumber;
            public string _Password;
            public bool _isTrue;
            public List<string> _configs;
            public Person()
            {
            }
            public Person(string PersonName, string PersonNumber, string Password, bool val)
            {
                _PersonName = PersonName;
                _PersonNumber = PersonNumber;
                _Password = Password;
                _isTrue = val;
                // _configs = config;        
            }
        }

对应的代理客户端类如下

public partial class person {
        private string _PersonNameField;
        private string _PersonNumberField;
        private string _PasswordField;
        private bool _isTrueField;
        private string[] _configsField;
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string _PersonName {
            get {
                return this._PersonNameField;
            }
            set {
                this._PersonNameField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string _PersonNumber {
            get {
                return this._PersonNumberField;
            }
            set {
                this._PersonNumberField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string _Password {
            get {
                return this._PasswordField;
            }
            set {
                this._PasswordField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public bool _isTrue {
            get {
                return this._isTrueField;
            }
            set {
                this._isTrueField = value;
            }
        }
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("_configs", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
        public string[] _configs {
            get {
                return this._configsField;
            }
            set {
                this._configsField = value;
            }
        }
    }

代理类中的方法在

下面
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace="http://sample.xyz.abc.ext/", ResponseNamespace="http://sample.xyz.abc.ext/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        [return: System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string createPerson([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] person arg0) {
            object[] results = this.Invoke("createJigBoard", new object[] {
                        arg0});
            return ((string)(results[0]));
        }

我在客户端调用的方法如下

ServiceReference.TestService per = new ServiceReference.TestService();
 var testList=new List<string>();
 Person personOne = new Person("Manoj", "123456761", "Administrator", true,testList);
            NetworkCredential myCred = new NetworkCredential("person", "person");
            CredentialCache myCache = new CredentialCache();
            myCache.Add(new Uri("http://pcblr********:80/*******/servlet/TestService"), "Basic", myCred);            
            StringWriter textWriter = new StringWriter();
            XmlSerializer xmlSer = new XmlSerializer(typeof(Person));          
            xmlSer.Serialize(textWriter, personOne);
                       textWriter.Close();
            per.createPerson(personOne);

我得到错误

Argument 1: cannot convert from 'Client.Person' to 'Proxyclass.person'  ******'ScriptMain.cs    

从c#客户端调用带有对象参数的Java Soap Web服务方法

错误信息正确。该服务需要一个ProxyClass。但是你发送的是Client.Person。

代替这一行:

Person personOne = new Person("Manoj", "123456761", "Administrator", true,testList);
你应该创建一个ProxyClass。人-对象,手动映射参数或使用AutoMapper或类似的。

您还需要从

更改序列化
XmlSerializer xmlSer = new XmlSerializer(typeof(Person));

XmlSerializer xmlSer = new XmlSerializer(typeof(ProxyClass.person));