Re: Web service & ArrayList
Ah... found this old thread:
http://forum.springframework.net/showthread.php?t=5274
I will list my solution here if other people have the same problem.
In short: ArrayList can't be used, one must use List<type> instead,
thus...
Server code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Collections;
namespace ClassLibrary1
{
[ServiceContract(Namespace="http://somesite.com")]
public interface IMainClass
{
[OperationContract]
string Hello();
[OperationContract]
[System.Xml.Serialization.XmlInclude(typeof(CustomersDataClass))]
List<CustomersDataClass> GetData();
}
public class MainClass : IMainClass
{
public string Hello()
{
return "Hello!";
}
public List<CustomersDataClass> GetData()
{
List<CustomersDataClass> a = new List<CustomersDataClass>();
a.Clear();
CustomersDataClass temp = new CustomersDataClass();
temp.CompanyName = "1";
temp.ContactName = "2";
temp.CustomerID = "3";
a.Add(temp);
temp = new CustomersDataClass();
temp.CompanyName = "4";
temp.ContactName = "5";
temp.CustomerID = "6";
a.Add(temp);
return a;
}
}
[Serializable]
public class CustomersDataClass
{
private string _CustomerID, _CompanyName, _ContactName;
public string CustomerID
{
get { return _CustomerID; }
set { _CustomerID = value; }
}
public string CompanyName
{
get { return _CompanyName; }
set { _CompanyName = value; }
}
public string ContactName
{
get { return _ContactName; }
set { _ContactName = value; }
}
}
}
Client code
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using System.Collections;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MainClassClient client =
new MainClassClient();
client.Open();
string s = client.Hello();
TextBox1.Text = s;
ClassLibrary1.CustomersDataClass[] arr = client.GetData();
client.Close();
}
}
}
Autogenerated proxy class (or whatever it is called) here:
Code:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.3082
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace ClassLibrary1
{
using System.Runtime.Serialization;
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="CustomersDataClass", Namespace="http://schemas.datacontract.org/2004/07/ClassLibrary1")]
public partial class CustomersDataClass : object, System.Runtime.Serialization.IExtensibleDataObject
{
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private string _CompanyNameField;
private string _ContactNameField;
private string _CustomerIDField;
public System.Runtime.Serialization.ExtensionDataObject ExtensionData
{
get
{
return this.extensionDataField;
}
set
{
this.extensionDataField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public string _CompanyName
{
get
{
return this._CompanyNameField;
}
set
{
this._CompanyNameField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public string _ContactName
{
get
{
return this._ContactNameField;
}
set
{
this._ContactNameField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public string _CustomerID
{
get
{
return this._CustomerIDField;
}
set
{
this._CustomerIDField = value;
}
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://somesite.com", ConfigurationName="IMainClass")]
public interface IMainClass
{
[System.ServiceModel.OperationContractAttribute(Action="http://somesite.com/IMainClass/Hello", ReplyAction="http://somesite.com/IMainClass/HelloResponse")]
string Hello();
[System.ServiceModel.OperationContractAttribute(Action="http://somesite.com/IMainClass/GetData", ReplyAction="http://somesite.com/IMainClass/GetDataResponse")]
ClassLibrary1.CustomersDataClass[] GetData();
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface IMainClassChannel : IMainClass, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class MainClassClient : System.ServiceModel.ClientBase<IMainClass>, IMainClass
{
public MainClassClient()
{
}
public MainClassClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public MainClassClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public MainClassClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public MainClassClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public string Hello()
{
return base.Channel.Hello();
}
public ClassLibrary1.CustomersDataClass[] GetData()
{
return base.Channel.GetData();
}
}
Re: Web service & ArrayList
Or return a raw array.
Code:
public CustomersDataClass[] GetData()
{
List<CustomersDataClass> a = new List<CustomersDataClass>();
a.Clear();
CustomersDataClass temp = new CustomersDataClass();
temp.CompanyName = "1";
temp.ContactName = "2";
temp.CustomerID = "3";
a.Add(temp);
temp = new CustomersDataClass();
temp.CompanyName = "4";
temp.ContactName = "5";
temp.CustomerID = "6";
a.Add(temp);
return a.ToArray( );
}
A reason to return a raw array would be for better compatibility with clients that don't know about List<>.