Asked by:
Accessing SOAP webservice

Question
-
User-1165400470 posted
Hi
I have an asmx web service StaffService.asmx, whose GetUpcomingJobs method returns below data;
<string xmlns="http://tempuri.org/">
<NewDataSet>
<UpcomingJobs>
<ID>100643</ID>
<DateFrom>24/11/2020</DateFrom>
<DateTo>24/11/2020</DateTo>
<Client>Test Client</Client>
</UpcomingJobs>
</NewDataSet>
</string>In my client app under connected services I have added a reference to the asmx web service called StaffService under which I get StaffWebServiceSoap, StaffWebServiceSoapClient and StaffWebServiceSoapChannel. I also have an UpcomingJobs model;
class UpcomingJobs { public int ID { get; set; } public string DateFrom { get; set; } public string DateTo { get; set; } public string Client { get; set; } }
How can I get all this together and get the incoming data into the UpcomingJobs model?
Thanks
Regards
Tuesday, November 24, 2020 4:54 AM
All replies
-
User475983607 posted
The code you've posted is confusing. The XML looks like you've designed a REST ASMX service not a SOAP service. Can you explain the application design and share code that reproduces this issue.
Keep in mind, ASMX is very old technology. If you are building a REST service then look into Web API.
Tuesday, November 24, 2020 1:08 PM -
User-1165400470 posted
Hi mgebhard
Could be that its REST, that part is remote and I am just picking up data from there. My issue is its client side consumption.
After adding reference of the service to connected services and allowing VS 2019 to auto generate proxy classes, I have tried below code to consume the service but I have no idea what to do as its my first time. First two lines in the method are really shot in the dark.
public async System.Threading.Tasks.Task<ObservableCollection<UpcomingJobs>> GetUpcomingJobsAsync() { ServiceReference1.StaffServiceSoapClient.EndpointConfiguration e = new ServiceReference1.StaffWebServiceSoapClient.EndpointConfiguration(); ServiceReference1.StaffWebServiceSoapClient client = new ServiceReference1.StaffWebServiceSoapClient(e); var content = await client.GetUpcomingJobsAsync(Username, Password); Items = JsonConvert.DeserializeObject<ObservableCollection<UpcomingJobs>>(content); return Items; }
Regards
Tuesday, November 24, 2020 1:28 PM -
User475983607 posted
Your code makes little sense.
REST services not do use a WSDL (service reference). SOAP services return XML behind the scenes. The Service Reference deserializes the XML to an actual C# object. It is not clear why you are using a JSON serializer unless the SOAP service returns a JSON string which is odd at best.
I recommend contacting the service owners for support using their service.
Tuesday, November 24, 2020 2:19 PM -
User-1165400470 posted
Hi
Here is the remote webservice and the web method, I just need to consume the data from this web service/web method in my client app. This is legacy code. Unfortunately until someone rewrites it this is all I got.
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ToolboxItem(False)> _ Public Class StaffService Inherits System.Web.Services.WebService <WebMethod()> _ Public Function GetUpcomingJobs(ByVal Username As String, ByVal Password As String) As String Dim ds As DataSet = Nothing If UserValid(Username, Password) <> "" Then Return "" End If Dim St As String = "SELECT ID, convert (varchar(10), Date,103) AS DateFrom, convert (varchar(10), DateTo,103) AS DateTo, Client " & _ "FROM UpcomingJobs " & _ "ORDER BY Date ASC" Dim Adapter As New System.Data.OleDb.OleDbDataAdapter(St, My.Settings.DBConnectionString1) ds = New System.Data.DataSet() Adapter.Fill(ds, "UpcomingJobs") Return ds.GetXml() End Function End Class
Regards
Tuesday, November 24, 2020 3:07 PM -
User475983607 posted
Yeah, that ASMX code is pretty bad... Visual Studio can create the object model; Copy the XML -> Edit -> Paste Special -> Paste XML as classes.
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0. /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/")] [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/", IsNullable = false)] public partial class @string { private stringNewDataSet newDataSetField; /// <remarks/> public stringNewDataSet NewDataSet { get { return this.newDataSetField; } set { this.newDataSetField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/")] public partial class stringNewDataSet { private stringNewDataSetUpcomingJobs upcomingJobsField; /// <remarks/> public stringNewDataSetUpcomingJobs UpcomingJobs { get { return this.upcomingJobsField; } set { this.upcomingJobsField = value; } } } /// <remarks/> [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/")] public partial class stringNewDataSetUpcomingJobs { private uint idField; private string dateFromField; private string dateToField; private string clientField; /// <remarks/> public uint ID { get { return this.idField; } set { this.idField = value; } } /// <remarks/> public string DateFrom { get { return this.dateFromField; } set { this.dateFromField = value; } } /// <remarks/> public string DateTo { get { return this.dateToField; } set { this.dateToField = value; } } /// <remarks/> public string Client { get { return this.clientField; } set { this.clientField = value; } } }
Stream stream = GenerateStreamFromString(xmlStirng); XmlSerializer serializer = new XmlSerializer(typeof(@string)); @string result = (@string)serializer.Deserialize(stream); Console.WriteLine(result.NewDataSet.UpcomingJobs.Client);
public static Stream GenerateStreamFromString(string s) { var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(s); writer.Flush(); stream.Position = 0; return stream; }
Tuesday, November 24, 2020 3:44 PM