Hamid Heydarian
Hamid Heydarian

Reputation: 932

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

I'm trying to read a WSDL from a URL to dynamically generate the proxy for the WCF service. This is my code:

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

I get an XmlException from method ServiceDescription.CanRead.
The error massage is "Data at the root level is invalid. Line 1, position 1".

Browsing the WDSL URL in IE, I can see the following tag at the start before tag <wsdl:definitions ...> ... </wsdl:definitions> which doesn't appear in chrome.

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

Could that be the issue? but I suppose ServiceDescription.CanRead should be able to recognise that. Any hints would be appreciated.

Upvotes: 0

Views: 376

Answers (1)

Scott Hannen
Scott Hannen

Reputation: 29312

Try adding this before the first line included in your question:

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

Borrowed from here.

Upvotes: 1

Related Questions