Skip to main content
 首页 » 编程设计

c#之ServiceDescription 未读取 protected wsdl

2024年02月23日12dudu

我有一个 C# 代码,基本上读取传递的 WSDL 以动态生成程序集,然后我们访问该服务的所有方法和属性。

/// <summary> 
/// Builds an assembly from a web service description. 
/// The assembly can be used to execute the web service methods. 
/// </summary> 
/// <param name="webServiceUri">Location of WSDL.</param> 
/// <returns>A web service assembly.</returns> 
private Assembly BuildAssemblyFromWSDL(Uri webServiceUri) 
{ 
    if (String.IsNullOrEmpty(webServiceUri.ToString())) 
        throw new Exception("Web Service Not Found"); 
 
    XmlTextReader xmlreader = new XmlTextReader(webServiceUri.ToString() + "?wsdl"); 
 
    ServiceDescriptionImporter descriptionImporter = BuildServiceDescriptionImporter(xmlreader); 
 
    return CompileAssembly(descriptionImporter); 
} 
 
/// <summary> 
/// Builds the web service description importer, which allows us to generate a proxy class based on the  
/// content of the WSDL described by the XmlTextReader. 
/// </summary> 
/// <param name="xmlreader">The WSDL content, described by XML.</param> 
/// <returns>A ServiceDescriptionImporter that can be used to create a proxy class.</returns> 
private ServiceDescriptionImporter BuildServiceDescriptionImporter(XmlTextReader xmlreader) 
{ 
    // make sure xml describes a valid wsdl 
    if (!ServiceDescription.CanRead(xmlreader)) 
        throw new Exception("Invalid Web Service Description"); 
 
    // parse wsdl 
    ServiceDescription serviceDescription = ServiceDescription.Read(xmlreader); 
 
    // build an importer, that assumes the SOAP protocol, client binding, and generates properties 
    ServiceDescriptionImporter descriptionImporter = new ServiceDescriptionImporter(); 
    descriptionImporter.ProtocolName = "Soap"; 
    descriptionImporter.AddServiceDescription(serviceDescription, null, null); 
    descriptionImporter.Style = ServiceDescriptionImportStyle.Client; 
    descriptionImporter.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties; 
 
    return descriptionImporter; 
} 

这段代码适用于所有 wsdl, protected 或安全的除外。代码在 if (!ServiceDescription.CanRead(xmlreader)) 行失败,因为它无法访问传递的服务 wsdl。当我尝试在浏览器中访问 url 时,出现 500:服务器错误。当我使用适当的身份验证登录到我们的 Web 应用程序时,如果我复制 url,则使用相同的 session - 我可以访问 wsdl。仅供引用,在不同的应用程序中,我们通过传递带有服务用户 ID/密码的 SM Cookie 来动态调用此服务。

话虽如此,我该怎么做,动态访问 protected wsdl?传递cookie信息访问wsdl需要做哪些改动?有什么想法吗?

请您参考如下方法:

private Assembly BuildAssemblyFromWSDL(Uri webServiceUri) 
{     
    if (String.IsNullOrEmpty(webServiceUri.ToString()))         
        throw new Exception("Web Service Not Found");     
 
    HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(webServiceUri.OriginalString + "?wsdl"); 
    wr.Credentials = new NetworkCredential("username", "password"); //replace with your credentials 
    HttpWebResponse wres = (HttpWebResponse)wr.GetResponse(); 
 
    XmlTextReader xmlreader = new XmlTextReader(wres.GetResponseStream());        
 
    ServiceDescriptionImporter descriptionImporter = BuildServiceDescriptionImporter(xmlreader);     
    return CompileAssembly(descriptionImporter); 
} 

这将允许您在获取 wsdl 之前传递凭据