简体   繁体   中英

Consume wcf hosted in web application in xamarin form

I have done googled ,but not got my requirements in consuming WCF in xamarin.I have a web application that is hosted at local IIS,that application also have wcf service. what I have done. 1.created wcf service with name Myservice.

2.in myservice.cs ,there are two methods, one validateUser,and saveData.

3.I have create another separate project as xamarin cross plateform shared.or pcl .and has added webservice refernces there.

my code is as follow.

Myservice.cs

public bool SaveChallenData()
{
throw new NotImplementedException();
}
public bool ValidateUser(string UserId, string Password)
{
FYP_CMSEntities1 ef = new FYP_CMSEntities1();
if (!string.IsNullOrEmpty(UserId) && !string.IsNullOrEmpty(Password))
{
var login = (from ul in ef.UserLogins where ul.UserName == UserId select ul).FirstOrDefault();
if (login != null && login.Password == Password)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

created a service class in xamarin project as below.

class Service
{
public static string SoapUrl = "http://localhost/FYP_Admin/webservices/cmsservice.svc";
ICMSService soapService;
public Service(ICMSService service)
{
soapService = service;
}
public Service()
{
soapService = new CMSServiceClient(
new BasicHttpBinding(),
new EndpointAddress(SoapUrl));
}
//public Task ValidateUser(string username,string password)
//{
// return soapService.BeginDoWork
//}
}

now i want on user click i validate user like below

private void Button_Clicked(object sender, EventArgs e)
{
Service svc = new Service()
var endpoint = new EndpointAddress("http://localhost/FYP_Admin/webservices/cmsservice.svc");
var binding = new BasicHttpBinding
{
Name = "basicHttpBinding",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
string username = usernameEntry.Text.Trim();
string password = passwordEntry.Text.Trim();
bool result = svc.ValidateUser(username, password);
if (result == true)
{
App.Navigation.PushAsync(new MainPage());
}
else
{
DisplayAlert("Oops","Credentials are incorrect","Cancel");
}
}

But i could not find validate method here.

It looks like you are trying to manually create the service client class with your 'Service' class but you haven't got any methods in that class so there are no methods to call. ValidateUser is not available because it's not exposed in the service class.

The easiest way to create that class is to use something like SLsvcUtil.exe to automatically create the class for you.

A walkthrough example can be found in this Xamarin walkthrough

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM