SoapHttpClientProtocol动态调用webservice
1.参考连接:https://docs.microsoft.com/zh-cn/dotnet/api/system.web.services.protocols.soaphttpclientprotocol?view=netframework-4.7.2
2.webservice
namespace WS { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] //[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string Sum(int a, int b,out int c) { Stream sm = Context.Request.InputStream; StreamReader sr = new StreamReader(sm, System.Text.Encoding.UTF8); string requestXml = sr.ReadToEnd(); c = 3; return $"{a}+{b}={a + b}"+requestXml; } [WebMethod] public string Sum1(int a) { return a.ToString(); } [WebMethod] public Result Test(string step,Test e) { return new Result { A = e.A, B = e.B, C=e.A+e.B }; } } public class Test { public string A { get; set; } public string B { get; set; } } public class Result { public string A { get; set; } public string B { get; set; } public string C { get; set; } } }
3.调用
注意:方法和参数,需要wz一致。
namespace ConsoleApp3 { [WebServiceBinding(Namespace = "http://tempuri.org/")] public class SoapTest : SoapHttpClientProtocol { public SoapTest() { Url = "http://localhost:58271/WebService1.asmx"; } [SoapDocumentMethod] public Result Test(string step, Test e) { var a = Invoke("Test", new object[] { step, e })[0] as Result; return a; } [SoapDocumentMethod] public string Sum1(int a) { var b = Invoke("Sum1", new object[] { a })[0] as string; return b; } } public class Test { public string A { get; set; } public string B { get; set; } } public class Result { public string A { get; set; } public string B { get; set; } public string C { get; set; } } }
4.方法调用
static void Main(string[] args) { string _url = "http://localhost:58271/WebService1.asmx"; var soap = new SoapTest(); var b = soap.Sum1(1); var a = soap.Test("1",new Test { A="1",B="2"}); }