TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Demo Cloud
NA
140
12.8k
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
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
namespace
ServiceLogin
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// 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.
public
class
Service1 : IService1
{
public
List<
string
> LoginUserDetails(UserDetails userInfo)
{
List<
string
> usr =
new
List<
string
>();
SqlConnection con =
new
SqlConnection(
"Data Source=Test; Initial Catalog=crm_db; User ID=sa; Password=*****"
);
con.Open();
SqlCommand cmd =
new
SqlCommand(
"select username,password from crm_tbl_admin where username=@UserName and password=@Password"
, con);
cmd.Parameters.AddWithValue(
"@UserName"
, userInfo.UserName);
cmd.Parameters.AddWithValue(
"@Password"
, userInfo.Password);
SqlDataReader dr = cmd.ExecuteReader();
if
(dr.Read() ==
true
)
{
usr.Add(dr[0].ToString());
}
con.Close();
return
usr;
}
}
}
Code for consuming the above service in my MainPage.Xaml.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
Xamarin.Forms;
using
ServiceReference1;
using
System.ServiceModel;
namespace
WCF_LoginApp
{
public
partial
class
MainPage : ContentPage
{
Service1Client obj =
new
Service1Client();
private
static
EndpointAddress endPoint =
new
EndpointAddress(
"http://localhost/ServiceLogin/Service1.svc"
);
private
static
NetTcpBinding binding;
public
MainPage()
{
InitializeComponent();
binding = CreateBasicHttpBinding();
}
private
static
NetTcpBinding CreateBasicHttpBinding()
{
NetTcpBinding binding =
new
NetTcpBinding
{
Name =
"netTcpBinding"
,
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
TimeSpan timeout =
new
TimeSpan(0, 0, 30);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
return
binding;
}
private
void
BtnLogin_Clicked(
object
sender, EventArgs e)
{
try
{
UserDetails userinfo =
new
UserDetails();
userinfo.UserName = usernameEntry.Text;
userinfo.Password = passwordEntry.Text;
List<
string
> msg = obj.LoginUserDetailsAsync(userinfo).ToList();
//var result = obj.LoginUserDetailsAsync(userinfo);
//string msg = result.ToString();
messageLabel.Text =
"Employee Name = "
+ msg.ElementAt(0);
}
catch
(Exception ex)
{
// messageLabel.Text = "Wrong Id Or Password";
throw
ex;
}
}
}
}
The code for Webconfig for WCF project is below
<?xml version=
"1.0"
?>
<configuration>
<appSettings>
<add key=
"aspnet:UseTaskFriendlySynchronizationContext"
value=
"true"
/>
</appSettings>
<system.web>
<compilation debug=
"true"
targetFramework=
"4.6.1"
/>
<httpRuntime targetFramework=
"4.6.1"
/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled=
"true"
httpsGetEnabled=
"true"
/>
<serviceDebug includeExceptionDetailInFaults=
"false"
/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name=
"ServiceLogin.Service1"
>
<endpoint address=
"Service1"
contract=
"ServiceLogin.IService1"
binding=
"wsHttpBinding"
/>
<endpoint address=
"Service1"
binding=
"netTcpBinding"
contract=
"ServiceLogin.IService1"
/>
<host>
<baseAddresses>
<add baseAddress=
"http://localhost:8080/"
/>
<add baseAddress=
"net.tcp://localhost:8090/"
/>
</baseAddresses>
</host>
</service>
</services>
<protocolMapping>
<add binding=
"wsHttpBinding"
scheme=
"http"
/>
<add binding=
"netTcpBinding"
scheme=
"net.tcp"
/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled=
"true"
multipleSiteBindingsEnabled=
"true"
/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests=
"true"
/>
<!--
To browse web app root directory during debugging, set the value below to
true
.
Set to
false
before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled=
"true"
/>
</system.webServer>
</configuration>
Please help
Thanks in advance
Reply
Answers (
7
)
Getting an error while consuming WCF Web service reference
Getting error after consuming a asmx webservice in Xamarin