Last week I created an asmx web service which returns multiple rows using soap.
I now moving onto WCF and am wanting to do the same.
In my ASMX web service I am doing the following..
public class sample
{
public string Id { get; set; }
public string Name { get; set; }
public string NameOfFile { get; set; }
//public int Distance { get; set; }
}
[WebMethod]
public sample[] Test(int count, float lat, float lng)
{
DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng);
var samples = new List<sample>();
foreach (DataRow item in dt.Rows)
{
var s = new sample();
s.Id = item[0].ToString();
s.Name = item[1].ToString();
s.NameOfFile = item[2].ToString();
//s.Distance = (int)item[3];
samples.Add(s);
}
return samples.ToArray();
}
This code works great but now I want to do the same but using WCF.
My current WCF files look like this (I copied a tutorial but have setup the data contract (which i think is needed?))
GalleryWebService.cs
public class GalleryWebService : IGalleryWebService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
return composite;
}
public CompositeType GetTestData()
{
return new CompositeType();
}
}
IGalleryWebService.cs
[ServiceContract]
public interface IGalleryWebService
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "test/random")]
CompositeType GetTestData();
}
[DataContract]
public class CompositeType
{
[DataMember]
string _id;
public string Id
{
get { return _id; }
set { _id = value; }
}
[DataMember]
string _name = "Hello";
public string Name
{
get { return _name; }
set { _name = value; }
}
[DataMember]
string _nameoffile = "Hello";
public string NameOfFile
{
get { return _nameoffile; }
set { _nameoffile = value; }
}
}
What is the best way to go about doing it and how? Your help is greatly appreciated!
Thanks in advance.
WCF has not so many differences:
[DataContract]
public class sample
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string NameOfFile { get; set; }
}
DataMember
attributes are not set by default, so they are obligatory somehow.
[ServiceContract]
public interface IGalleryWebService
{
[OperationContract]
sample[] Test(int count, float lat, float lng);
}
Moreover, if you use basicHttpBinding
and add link using "Add Web reference" (instead of service reference) - you will receive the same result as you would have if you used asmx service.