好的 - 我为您提供了一个可能的解决方案,该解决方案并未因优雅而获奖,但我刚刚对其进行了测试,它确实有效。
您可以公开一个返回 object 并接受 params object[] 参数的 WebMethod,允许您将任何您喜欢的内容传递给它(或不传递任何内容)并返回您想要的任何内容。这使用“anyType”类型编译为合法的 WSDL。
如果您可以根据传递给此方法的参数的数量和数据类型确定要调用哪个实际方法,则可以调用适当的方法并返回您想要的任何值。
服务:-
[WebMethod]
public object Method(params object[] parameters)
{
object returnValue = null;
if (parameters != null && parameters.Length != 0)
{
if (parameters[0].GetType() == typeof(string) && parameters[1].GetType() == typeof(int))
{
return new ServiceImplementation().StringIntMethod(parameters[0].ToString(), Convert.ToInt32(parameters[1]));
}
else if (parameters[0].GetType() == typeof(string) && parameters[1].GetType() == typeof(string))
{
return new ServiceImplementation2().StringStringMethod(parameters[0].ToString(), parameters[1].ToString());
}
}
return returnValue;
}
我的测试服务实现类:-
public class ServiceImplementation
{
public string StringIntMethod(string someString, int someInt)
{
return "StringIntMethod called";
}
}
public class ServiceImplementation2
{
public float StringStringMethod(string someString, string someOtherString)
{
return 3.14159265F;
}
}
使用示例:-
var service = new MyTestThing.MyService.WebService1();
object test1 = service.Method(new object[] { "hello", 3 });
Console.WriteLine(test1.ToString());
object test2 = service.Method(new object[] { "hello", "there" });
Console.WriteLine(test2.ToString());
我已经对此进行了测试,并且可以正常工作。如果您有兴趣,“方法”生成的 WSDL:-
POST /test/WebService1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Method"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Method xmlns="http://tempuri.org/">
<parameters>
<anyType />
<anyType />
</parameters>
</Method>
</soap:Body>
</soap:Envelope>
如果你想知道,是的,我在工作中很无聊,我很想帮助别人:)