好的 - 我有一个可能的解决方案,它不是屡获殊荣的优雅,但我刚刚测试过它,它的工作原理。
您可以公开一个WebMethod,该WebMethod返回对象并获取params对象[]参数,从而允许您将任何喜欢的东西传递给它(或者什么都不),并返回任何你想要的东西。这使用'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>
柜面你想知道的,是我在工作无聊,我的心情帮助人们:)
如何你知道你的客户端运行的是什么版本吗?典型的REST API将使用URL方案来区分版本。 – Alexandre
在这种情况下,将会有一个配置文件,在安装过程中,用户将被要求输入产品版本(与API版本或WSDL绑定)。也就是说,我试图通过拥有一个EndPoint来实现这一点。主要的问题是版本之间的WSDL有巨大的变化。 –
ASMX是一项传统技术,不应该用于新开发。 WCF或ASP.NET Web API应该用于Web服务客户端和服务器的所有新开发。一个暗示:微软已经在MSDN上退役了[ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads)。 –