Using reflection to call an ASP.NET web service

前端 未结 8 1443
一向
一向 2021-02-04 17:25

Say I have an ASMX web service, MyService. The service has a method, MyMethod. I could execute MyMethod on the server side as follows:

MyService service = new          


        
8条回答
  •  遥遥无期
    2021-02-04 17:52

    Here's a quick answer someone can probably expand on.

    When you use the WSDL templating app (WSDL.exe) to genereate service wrappers, it builds a class of type SoapHttpClientProtocol. You can do it manually, too:

    public class MyService : SoapHttpClientProtocol
    {
        public MyService(string url)
        {
            this.Url = url;
            // plus set credentials, etc.
        }
    
        [SoapDocumentMethod("{service url}", RequestNamespace="{namespace}", ResponseNamespace="{namespace}", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public int MyMethod(string arg1)
        {
            object[] results = this.Invoke("MyMethod", new object[] { arg1 });
            return ((int)(results[0]));
        }
    }
    

    I haven't tested this code but I imagine it should work stand-alone without having to run the WSDL tool.

    The code I've provided is the caller code which hooks up to the web service via a remote call (even if for whatever reason, you don't actually want it to be remote.) The Invoke method takes care of packaging it as a Soap call. @Dave Ward's code is correct if you want to bypass the web service call via HTTP - as long as you are actually able to reference the class. Perhaps the internal type is not "MyService" - you'd have to inspect the control's code to know for sure.

提交回复
热议问题