我有一些代码,允许我在运行时为给定的URL,服务名称,方法名称和参数动态调用Web服务。当我尝试编译的WSDL包含<import namespace="..." location="..."/>
标记时,问题出现了。它会引发以下错误:
缺少名称空间的服务描述...
据推测,我需要首先从import标签编译引用的WSDL。我应该如何理解这一点,请记住引用的WSDL也可以引用另一个WSDL?以下是我正在使用的代码。
public static object CallWebService(string url, string serviceName, string methodName, string userName, string password, ServiceDescription wsdl, ILogger logger, object[] args)
{
var client = new WebClient();
if (userName.Length > 0)
{
client.Credentials = new NetworkCredential(userName, password);
}
var stream = client.OpenRead(url + "?wsdl");
if (stream != null)
{
var description = ServiceDescription.Read(stream);
wsdl = description;
var importer = new ServiceDescriptionImporter
{
ProtocolName = "Soap12",
Style = ServiceDescriptionImportStyle.Client,
CodeGenerationOptions = CodeGenerationOptions.GenerateProperties
};
importer.AddServiceDescription(wsdl, null, null);
var nameSpace = new CodeNamespace();
var unit = new CodeCompileUnit();
unit.Namespaces.Add(nameSpace);
**var warning = importer.Import(nameSpace, unit);** // Error occurs here
if (warning == 0)
{
var provider = CodeDomProvider.CreateProvider("CSharp");
var assemblyReferences = new[] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll" };
var parameters = new CompilerParameters(assemblyReferences);
var results = provider.CompileAssemblyFromDom(parameters, unit);
if (results.Errors.Count > 0)
{
logger.Info("Compiler errors: " + results.Errors.Count);
foreach (var error in results.Errors)
{
logger.Info(error.ToString());
}
throw new Exception("Compile Error Occured calling webservice.");
}
var service = results.CompiledAssembly.CreateInstance(serviceName);
if (service != null)
{
var methods = service.GetType().GetMethods();
var method = service.GetType().GetMethod(methodName);
if (method == null)
{
var message = "Method: " + methodName + " is invalid. Valid Methods are: ";
message = methods.Aggregate(message, (current, methodInfo) => current + methodInfo.Name + "; ");
throw new Exception(message);
}
return method.Invoke(service, args);
}
logger.Info("Service invocation error. ServiceName provided: " + serviceName);
return null;
}
return null;
}
return null;
}
答案 0 :(得分:0)
可能有点晚了但谷歌搜索者还不错。 你必须压扁你的xsd文件。解析它们并寻找导入部分。 获取此导入部分并添加在导入的位置路径中找到的文件。 这应该让你开始: http://arstechnica.com/civis/viewtopic.php?f=20&t=180943