0

I'm trying to connect my WCF service in Xamarine Forms, but in runtime I am getting this error

For security reasons DTD is prohibited in this XML document. To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into XmlReader.Create method.

LoginWCFService.svc.cs

public class LoginWCFService : ILoginWCFService
    {   
        public string LoginUserDetails(UserDetails userInfo)
        {
            string result = string.Empty;
            bool res = false;

            if (userInfo.uName!="" && userInfo.pWord != "" || userInfo.uName != null && userInfo.pWord != null)
            {   
                    result = "Login Successfull...";                
            }
            else
            {
                res = false;
                result = "Empty username or password";
            }
            return result.ToString();
        }
    }

ILoginWCFService.cs

 [ServiceContract]
    public interface ILoginWCFService
    {
        [OperationContract]        
        string LoginUserDetails(UserDetails UserInfo);
    }

    public class UserDetails
    {
        string UserName = string.Empty;
        string Password = string.Empty;

        [DataMember]
        public string uName
        {
            get { return UserName; }
            set { UserName = value; }
        }

        [DataMember]
        public string pWord
        {
            get { return Password; }
            set { Password = value; }
        }
    }

web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>     
      <serviceBehaviors>
        <behavior>
           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>        
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
      <bindings>
          <basicHttpBinding>
              <binding name="BasicHttpBinding_ILoginWCFService" />        
          </basicHttpBinding>
      </bindings>
      <client>
          <endpoint address="http://wcfapi.local/LoginWCFService.svc"
              binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ILoginWCFService"
              contract="WcfService_Omss.ILoginWCFService" name="BasicHttpBinding_ILoginWCFService" />
      </client>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

Consuming WCF in Xamarine Form

LoginPage.xaml.cs

WCFServiceReference.LoginWCFServiceClient client = new LoginWCFServiceClient();
                    client.Open();
                    UserDetails user = new UserDetails();
                    user.uName = Uname.Text.ToString();
                    user.pWord = Pass.Text.ToString();

                    var res =  client.LoginUserDetails(user);  //Getting error here

                    if (res == "Login Successfull...")
                        await DisplayAlert("Message", "Login Successfull...", "Cancel");
                    else
                        await DisplayAlert("Message", "Login failed...", "Cancel");

                    client.Close();

Can anyone please tell me where I have to do changes to resolve this error?

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196

1 Answers1

1

This error has nothing to do with the wcf service.
As the message says, set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings to the XmlReader.Create method.
You can refer to the code below.
https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreadersettings.dtdprocessing?view=net-6.0

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

public class Sample {

  public static void Main() {

    // Set the validation settings.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.DtdProcessing = DtdProcessing.Parse;
    settings.ValidationType = ValidationType.DTD;
    settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

    // Create the XmlReader object.
    XmlReader reader = XmlReader.Create("itemDTD.xml", settings);

    // Parse the file.
    while (reader.Read());
  }

  // Display any validation errors.
  private static void ValidationCallBack(object sender, ValidationEventArgs e) {
    Console.WriteLine("Validation Error: {0}", e.Message);
  }
}
Lan Huang
  • 613
  • 2
  • 5
  • Thanks Lan Huang for your answer, But I already implemented this still getting error. Can you please tell me where exactly I have to put this code? – sarthkee waghchaure Apr 19 '22 at 12:44
  • I checked posts with similar problems, this error may be caused by other problems, you can check.https://social.msdn.microsoft.com/Forums/en-US/22e3555a-3f37-4599-8310-bf8edc383fe2/error-for-security-reasons-dtd-is-prohibited-in-this-xml-document?forum=xamarinforms and https://stackoverflow.com/questions/54231007/change-xmlreadersettings-for-autogenerated-wcf-client – Lan Huang Apr 20 '22 at 06:03
  • Ok sure. Thanks Lan Huang – sarthkee waghchaure Apr 21 '22 at 07:07