Demo Cloud

Demo Cloud

  • NA
  • 140
  • 12k

Getting error after calling WCF webservice in Xamarin forms

May 2 2020 4:19 AM
Hello Sir,
 
I'm new to development and WCF Web services.
 
I'm getting below error after consuming WCF web services in my Xamarin cross platform project.
 
Error CS1061 'Task<string[]>' does not contain a definition for 'ToList' and no accessible extension method 'ToList' accepting a first argument of type 'Task<string[]>' could be found (are you missing a using directive or an assembly reference?) WCF_LoginApp
 
Code for Service1.svc.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data.SqlClient;  
  4. using System.Linq;  
  5. using System.Runtime.Serialization;  
  6. using System.ServiceModel;  
  7. using System.ServiceModel.Web;  
  8. using System.Text;  
  9. namespace ServiceLogin  
  10. {  
  11. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.  
  12. // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.  
  13. public class Service1 : IService1  
  14. {  
  15. public List<string> LoginUserDetails(UserDetails userInfo)  
  16. {  
  17. List<string> usr = new List<string>();  
  18. SqlConnection con = new SqlConnection("Data Source=Test; Initial Catalog=crm_db; User ID=sa; Password=*****");  
  19. con.Open();  
  20. SqlCommand cmd = new SqlCommand("select username,password from crm_tbl_admin where username=@UserName and password=@Password", con);  
  21. cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);  
  22. cmd.Parameters.AddWithValue("@Password", userInfo.Password);  
  23. SqlDataReader dr = cmd.ExecuteReader();  
  24. if (dr.Read() == true)  
  25. {  
  26. usr.Add(dr[0].ToString());  
  27. }  
  28. con.Close();  
  29. return usr;  
  30. }  
  31. }  
  32. }  
Code for consuming the above service in my MainPage.Xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7. using ServiceReference1;  
  8. using System.ServiceModel;  
  9. namespace WCF_LoginApp  
  10. {  
  11. public partial class MainPage : ContentPage  
  12. {  
  13. Service1Client obj = new Service1Client();  
  14. private static EndpointAddress endPoint = new EndpointAddress("http://localhost/ServiceLogin/Service1.svc");  
  15. private static NetTcpBinding binding;  
  16. public MainPage()  
  17. {  
  18. InitializeComponent();  
  19. binding = CreateBasicHttpBinding();  
  20. }  
  21. private static NetTcpBinding CreateBasicHttpBinding()  
  22. {  
  23. NetTcpBinding binding = new NetTcpBinding  
  24. {  
  25. Name = "netTcpBinding",  
  26. MaxBufferSize = 2147483647,  
  27. MaxReceivedMessageSize = 2147483647  
  28. };  
  29. TimeSpan timeout = new TimeSpan(0, 0, 30);  
  30. binding.SendTimeout = timeout;  
  31. binding.OpenTimeout = timeout;  
  32. binding.ReceiveTimeout = timeout;  
  33. return binding;  
  34. }  
  35. private void BtnLogin_Clicked(object sender, EventArgs e)  
  36. {  
  37. try  
  38. {  
  39. UserDetails userinfo = new UserDetails();  
  40. userinfo.UserName = usernameEntry.Text;  
  41. userinfo.Password = passwordEntry.Text;  
  42. List<string> msg = obj.LoginUserDetailsAsync(userinfo).ToList();  
  43. //var result = obj.LoginUserDetailsAsync(userinfo);  
  44. //string msg = result.ToString();  
  45. messageLabel.Text = "Employee Name = " + msg.ElementAt(0);  
  46. }  
  47. catch (Exception ex)  
  48. {  
  49. // messageLabel.Text = "Wrong Id Or Password";  
  50. throw ex;  
  51. }  
  52. }  
  53. }  
  54. }  
The code for Webconfig for WCF project is below
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3. <appSettings>  
  4. <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
  5. </appSettings>  
  6. <system.web>  
  7. <compilation debug="true" targetFramework="4.6.1" />  
  8. <httpRuntime targetFramework="4.6.1"/>  
  9. </system.web>  
  10. <system.serviceModel>  
  11. <behaviors>  
  12. <serviceBehaviors>  
  13. <behavior>  
  14. <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  15. <serviceDebug includeExceptionDetailInFaults="false"/>  
  16. </behavior>  
  17. </serviceBehaviors>  
  18. </behaviors>  
  19. <services>  
  20. <service name="ServiceLogin.Service1" >  
  21. <endpoint address="Service1" contract="ServiceLogin.IService1" binding="wsHttpBinding" />  
  22. <endpoint address="Service1" binding="netTcpBinding" contract="ServiceLogin.IService1"/>  
  23. <host>  
  24. <baseAddresses>  
  25. <add baseAddress="http://localhost:8080/"/>  
  26. <add baseAddress="net.tcp://localhost:8090/"/>  
  27. </baseAddresses>  
  28. </host>  
  29. </service>  
  30. </services>  
  31. <protocolMapping>  
  32. <add binding="wsHttpBinding" scheme="http" />  
  33. <add binding="netTcpBinding" scheme="net.tcp"/>  
  34. </protocolMapping>  
  35. <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  36. </system.serviceModel>  
  37. <system.webServer>  
  38. <modules runAllManagedModulesForAllRequests="true"/>  
  39. <!--  
  40. To browse web app root directory during debugging, set the value below to true.  
  41. Set to false before deployment to avoid disclosing web app folder information.  
  42. -->  
  43. <directoryBrowse enabled="true"/>  
  44. </system.webServer>  
  45. </configuration>  
Please help
Thanks in advance

Answers (7)