如何从 C# 中的 URL 读取 WCF 服务的 WSDL

How to read the WSDL of a WCF service from a URL in C#

我正在尝试从 URL 中读取 WSDL 以动态生成 W​​CF 服务的代理。 这是我的代码:

XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(description)) 
if (ServiceDescription.CanRead(xmlTextReader))
{
    ...
}

我从方法 ServiceDescription.CanRead 得到一个 XmlException
错误消息是“根级别的数据无效。第 1 行,位置 1".

在 IE 中浏览 WDSL URL,我可以在标签 <wsdl:definitions ...> ... </wsdl:definitions> 之前的开头看到以下标签,这在 chrome.

中没有出现

<?xml version="1.0" encoding="UTF-8"?>

会不会是这个问题?但我想 ServiceDescription.CanRead 应该能够认识到这一点。任何提示将不胜感激。

尝试在问题的第一行之前添加:

var byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (description.StartsWith(byteOrderMarkUtf8))
{
    var lastIndexOfUtf8 = byteOrderMarkUtf8.Length - 1;
    description = description.Remove(0, lastIndexOfUtf8);
}

借自here