[英]How to Solve DTD is prohibited in this xml document in WCF while connecting with Xamarin forms
I'm trying to connect my WCF service in Xamarine Forms, but in runtime I am getting this error我正在尝试在 Xamarine Forms 中连接我的 WCF 服务,但在运行时出现此错误
For security reasons DTD is prohibited in this XML document.出于安全原因,此 XML 文档中禁止使用 DTD。 To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into XmlReader.Create method.要启用 DTD 处理,请将 XmlReaderSettings 上的 DtdProcessing 属性设置为 Parse,并将设置传递给 XmlReader.Create 方法。
LoginWCFService.svc.cs登录WCFService.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 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 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以 Xamarine 形式消费 WCF
LoginPage.xaml.cs登录页面.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?谁能告诉我必须在哪里进行更改才能解决此错误?
This error has nothing to do with the wcf service.此错误与 wcf 服务无关。
As the message says, set the DtdProcessing property on XmlReaderSettings
to Parse
and pass the settings to the XmlReader.Create
method.如消息所述,将XmlReaderSettings
上的 DtdProcessing 属性设置为Parse
,并将设置传递给XmlReader.Create
方法。
You can refer to the code below.您可以参考下面的代码。
https://learn.microsoft.com/en-us/do.net/api/system.xml.xmlreadersettings.dtdprocessing?view.net-6.0 https://learn.microsoft.com/en-us/do.net/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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.