我在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?任何想法?
精彩,我會給這個嘗試TBohnen。謝謝! – 2011-04-20 15:19:32
謝謝TBohnen!我能夠用你的示例代碼解決這個問題。大! – 2011-04-20 18:45:10
太棒了,很高興它的工作! – 2011-04-20 19:23:12