我正在使用反射和WSDL通过动态构造的代理类动态调用Web服务,我刚刚将一些重载的Web方法添加到我正在调用的其中一个Web服务中。现在,在尝试调用(通过反射)方法时,我遇到了“Ambigious match”错误。
以下是构建服务代理的类,并且有一个方法可以按名称调用该代理中的任何给定Web方法:
public class ServiceProxy
{
public ServiceMetadata Metadata { get; private set; }
public RemoteServiceElement Element { get; private set; }
public string IpAddress { get; private set; }
private object serviceProxy;
private string serviceAsmx;
public ServiceProxy(RemoteServiceElement element)
{
IpAddress = element.IpAddress;
Element = element;
serviceAsmx = "http://" + element.IpAddress + ":" + element.Port + "xxxx.asmx"
Build(serviceAsmx, "xxxx");
}
public ServiceProxy(string ip, string _asmx, string _serviceName)
{
IpAddress = ip;
serviceAsmx = _asmx;
Build(_asmx, _serviceName);
}
private void Build(string webServiceAsmx, string serviceName)
{
WebClient client = new WebClient();
Metadata = ServiceMetadata.OpenWsdl(webServiceAsmx);
Stream stream = client.OpenRead(webServiceAsmx + "?wsdl");
ServiceDescription description = ServiceDescription.Read(stream);
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ProtocolName = "Soap12";
importer.AddServiceDescription(description, null, null);
importer.Style = ServiceDescriptionImportStyle.Client;
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
CodeNamespace nmspace = new CodeNamespace();
CodeCompileUnit unit1 = new CodeCompileUnit();
unit1.Namespaces.Add(nmspace);
ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);
if (warning == 0)
{
CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");
string[] assemblyReferences = new string[5] { "System.dll", "System.Web.Services.dll", "System.Web.dll",
"System.Xml.dll","System.Data.dll" };
CompilerParameters param = new CompilerParameters(assemblyReferences);
CompilerResults results = provider1.CompileAssemblyFromDom(param, unit1);
if (results.Errors.Count > 0)
{
foreach (CompilerError err in results.Errors)
{
Logger.Write("Compiler error assembling " + webServiceAsmx + " - " + err.ErrorText);
}
throw new Exception("Compiler error occurred calling the web service. Check log for details.");
}
serviceProxy = results.CompiledAssembly.CreateInstance(serviceName);
Logger.Write("Proxy service at + " + serviceAsmx + " assembled successfully");
}
}
public object Invoke(string methodName, object[] args = null)
{
MethodInfo info = serviceProxy.GetType().GetMethod(methodName);
object asmxResults = default(object);
try
{
asmxResults = info.Invoke(serviceProxy, args);
Logger.Write("Remote proxy at " + serviceAsmx + " - " + methodName + " - " + "invoked successfully");
}
catch (Exception e)
{
Logger.Write("Error invoking proxy class at " + serviceAsmx + " - " + e.InnerException);
}
return asmxResults;
}
}
在我添加任何重载之前,这工作正常。所以我猜测使用反射+重载可能会导致问题。
以下是导致问题的其中一个WebMethods的模拟示例:
[WebMethod (MessageName="GetFoos")]
public List<Foo> GetFoos(DateTime dt)
{
// performs linq query
}
[WebMethod (MessageName = "GetFoosDynamic")]
public List<Foo> GetFoos(Expression exp)
{
// linq query
}
相同的方法名称,不同的参数+不同的'MessageName',它应该适用于Web服务。
感谢您的帮助。
答案 0 :(得分:3)
SOAP不支持方法重载,但看起来您已使用WebMethod
属性覆盖了方法名称,因此在创建SOAP时应该调用在该属性中定义的名称调用