Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Error when calling wcf service in Xamarin Forms

relzerelze Member ✭✭

I'm trying to call my wcf service connected to my local database from an Android Emulator API 25 Nougat using xamarin forms and I get this error. I'm new to this so probably I'm missing some configuration.

Unhandled Exception:

System.ServiceModel.EndpointNotFoundException: System error. occurred

Web.config

<?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>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>

    <security>
      <requestFiltering>
        <fileExtensions allowUnlisted="true">
          <remove fileExtension="." />
          <add fileExtension="." allowed="true" />
        </fileExtensions>
      </requestFiltering>
    </security>
    <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>

Service1.svc

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;

    namespace MyWcfService
    {
        // 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
        {

            private string conStr = "Data Source=(LocalDb)\\LocalDBDemo; Initial Catalog=MyDatabase; Integrated Security=true";

            public List<UserName> GetUserNames()
            {
                List<UserName> userList = new List<UserName>();
                SqlConnection connection = new SqlConnection(this.conStr);
                connection.Open();
                SqlCommand cmd = new SqlCommand("SELECT Names FROM UserNames", connection);
                cmd.CommandType = CommandType.Text;
                SqlDataReader sdr = cmd.ExecuteReader();

                while (sdr.Read())
                {
                    UserName name = new UserName();
                    name.Name = sdr["Names"].ToString();
                    userList.Add(name);
                }
                return userList.ToList();
            }

        }
    }

Iservice1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebGet(UriTemplate = "GetUserNames")]
        List<UserName> GetUserNames();

    }
    [DataContract]



        public class UserName
        {
            string name;

            [DataMember]
            public string Name
            {
            get { return name; }
            set { name = value; }
         }            

        }

}

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace XAmwithWcf
{
    public partial class MainPage : ContentPage
    {
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        public MainPage()
        {
            InitializeComponent();

        }

        async void MyButton_Clicked(object sender, EventArgs e) {

            var result = await client.GetUserNamesAsync();
            MyEntry.Text = result.ToString();
           await  client.CloseAsync();

        }
    }
}

Best Answer

Answers

Sign In or Register to comment.