2011-04-20 36 views
1

我在C#中有一個代碼,基本上讀取傳遞的WSDL來動態生成程序集,然後我們訪問該服務的所有方法和屬性。ServiceDescription沒有讀取安全的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; 
} 

這段代碼適用於除受保護或受保護的所有wsdls外的所有wsdls。由於代碼無法訪問傳遞的服務wsdl,因此代碼在if (!ServiceDescription.CanRead(xmlreader))行失敗。當我嘗試訪問瀏覽器中的url時,我得到500:服務器錯誤。當我用適當的身份驗證登錄到我們的Web應用程序,然後在同一會話中複製網址時 - 我可以訪問wsdl。 Fyi,在另一個應用程序中,我們通過向服務用戶ID /密碼傳遞SM Cookies來動態調用此服務。

說了那麼,我該怎麼辦,動態訪問受保護的wsdl?我需要做些什麼才能傳遞cookie信息來訪問wsdl?任何想法?

回答

3
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

+0

精彩,我會給這個嘗試TBohnen。謝謝! – 2011-04-20 15:19:32

+0

謝謝TBohnen!我能夠用你的示例代碼解決這個問題。大! – 2011-04-20 18:45:10

+0

太棒了,很高興它的工作! – 2011-04-20 19:23:12

1

這是非常好的,但BuildAssemblyFromWSDL後,當我嘗試調用它再次給我一個錯誤消息像任何方法之前憑據傳遞..

HTTP狀態401請求失敗:未經授權。 at(T)type.InvokeMember(methodName,BindingFlags.InvokeMethod,null,obj,args);

我的代碼來調用一個方法就是像...

public T InvokeMethod<T>(string serviceName, string methodName, params object[] args) 
    { 
     // create an instance of the specified service 
     // and invoke the method 
     object obj = this.webServiceAssembly.CreateInstance(serviceName); 

     Type type = obj.GetType(); 

     return (T)type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, obj, args); 
    } 

現在,我不知道如何通過這裏的憑據。即使我想,通過其他的方法,但仍然得到聯合國通過 - 授權錯誤..

真的非常感謝如果你可以幫助...