Using reflection to call an ASP.NET web service

前端 未结 8 1437
一向
一向 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 18:00

    Although I cannot tell from your post:

    One thing to keep in mind is that if you use reflection, you need to create an instance of the autogenerated webservice class(the one created from your webservice's WSDL). Do not create the class that is responsbile for the server-side of the service.

    So if you have a webservice

        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class WebService1 : System.Web.Services.WebService
        {
         ...
        }
    

    you cannot reference that assembly in your client and do something like:

    WebService1  ws = new WebService1 ();
    ws.SomeMethod();
    
    0 讨论(0)
  • 2021-02-04 18:01

    // Try this ->

        Type t = System.Web.Compilation.BuildManager.GetType("MyServiceClass", true);
        object act = Activator.CreateInstance(t);                
        object o = t.GetMethod("hello").Invoke(act, null);
    
    0 讨论(0)
提交回复
热议问题