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

<pre><code>MyService service = new MyService();
service.MyMethod();
</code></pre>

<p>I need to do similar, with service and method not known until runtime.  </p>

<p>I'm assuming that reflection is the way to go about that.  Unfortunately, I'm having a hard time making it work.  When I execute this code:</p>

<pre><code>Type.GetType("MyService", true);
</code></pre>

<p>It throws this error:</p>

<blockquote>
  <p>Could not load type 'MyService' from assembly 'App_Web__ktsp_r0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.</p>
</blockquote>

<p>Any guidance would be appreciated.</p>

[{'answer_id': 62381, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 3, 'selected': True, 'text': '<p>I\'m not sure if this would be the best way to go about it.  The most obvious way to me, would be to make an HTTP Request, and call the webservice using an actual HTTP GET or POST. Using your method, I\'m not entirely sure how you\'d set up the data you are sending to the web service. I\'ve added some sample code in VB.Net</p>\n\n<pre><code>Dim HTTPRequest As HttpWebRequest\nDim HTTPResponse As HttpWebResponse\nDim ResponseReader As StreamReader\nDim URL AS String\nDim ResponseText As String\n\nURL = "http://www.example.com/MyWebSerivce/MyMethod?arg1=A&amp;arg2=B"\n\nHTTPRequest = HttpWebRequest.Create(URL)\nHTTPRequest.Method = "GET"\n\nHTTPResponse = HTTPRequest.GetResponse()\n\nResponseReader = New StreamReader(HTTPResponse.GetResponseStream())\nResponseText = ResponseReader.ReadToEnd()\n</code></pre>\n'}, {'answer_id': 62461, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 1, 'selected': False, 'text': "<p>Although I don't know why Reflection is not working for you there (I assume the compiler might be creating a new class from your <code>[WebService]</code> annotations), here is some advice that might solve your problem:</p>\n\n<p><strong>Keep your WebService simple, shallow, in short: An implementation of the Facade Pattern</strong>.</p>\n\n<p>Make your service delegate computation to an implementation class, which should easily be callable through Reflection. This way, your WebService class is just a front for your system - you can even add an email handler, XML-RPC frontend etc., since your logic is not coupled to the WebService, but to an actual business layer object.</p>\n\n<p>Think of WebService classes as UI layer objects in your Architecture.</p>\n"}, {'answer_id': 62587, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 0, 'selected': False, 'text': '<p>@Kibbee:  I need to avoid the HTTP performance hit.  It won\'t be a remote call, so all of that added overhead <em>should</em> be unnecessary.</p>\n\n<p>@Daren:  I definitely agree with that design philosophy.  The issue here is that I\'m not going to be in control of the service or its underlying business logic.</p>\n\n<p>This is for <a href="http://codeplex.com/UsernameAvailability" rel="nofollow noreferrer">a server control</a> that will need to execute against an arbitrary service/method, orthogonally to how the web service itself is implemented.</p>\n'}, {'answer_id': 62687, 'author': 'Radu094', 'author_id': 3263, 'author_profile': 'https://Stackoverflow.com/users/3263', 'pm_score': 0, 'selected': False, 'text': '<p>Although I cannot tell from your post:</p>\n\n<p>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.</p>\n\n<p>So if you have a webservice     </p>\n\n<pre><code>    [WebService(Namespace = "http://tempuri.org/")]\n    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]\n    [ToolboxItem(false)]\n    public class WebService1 : System.Web.Services.WebService\n    {\n     ...\n    }\n</code></pre>\n\n<p>you cannot reference that assembly in your client and do something like:</p>\n\n<pre><code>WebService1  ws = new WebService1 ();\nws.SomeMethod();\n</code></pre>\n'}, {'answer_id': 62774, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 0, 'selected': False, 'text': '<p>@Radu:  I\'m able to create an instance and call the method exactly like that.  For example, if I have this ASMX:</p>\n\n<pre>[WebService(Namespace = "http://tempuri.org/")]\n[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]\n[ScriptService]\npublic class MyService : System.Web.Services.WebService\n{\n  [WebMethod]\n  public string HelloWorld()\n  {\n    return "Hello World";\n  }\n}</pre>\n\n<p>I\'m able to call it from an ASPX page\'s codebehind like this:</p>\n\n<pre>MyService service = new MyService();\nResponse.Write(service.HelloWorld());</pre>\n\n<p>Are you saying that shouldn\'t work?</p>\n'}, {'answer_id': 62794, 'author': 'Steve Eisner', 'author_id': 7104, 'author_profile': 'https://Stackoverflow.com/users/7104', 'pm_score': 1, 'selected': False, 'text': '<p>Here\'s a quick answer someone can probably expand on.</p>\n\n<p>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:</p>\n\n<pre><code>public class MyService : SoapHttpClientProtocol\n{\n    public MyService(string url)\n    {\n        this.Url = url;\n        // plus set credentials, etc.\n    }\n\n    [SoapDocumentMethod("{service url}", RequestNamespace="{namespace}", ResponseNamespace="{namespace}", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]\n    public int MyMethod(string arg1)\n    {\n        object[] results = this.Invoke("MyMethod", new object[] { arg1 });\n        return ((int)(results[0]));\n    }\n}\n</code></pre>\n\n<p>I haven\'t tested this code but I imagine it should work stand-alone without having to run the WSDL tool.</p>\n\n<p>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.</p>\n'}, {'answer_id': 87636, 'author': 'Steve Eisner', 'author_id': 7104, 'author_profile': 'https://Stackoverflow.com/users/7104', 'pm_score': 0, 'selected': False, 'text': '<p>I looked back at this question and I think what you\'re facing is that the ASMX code will be built into a DLL with a random name as part of the dynamic compilation of your site.  Your code to look up the type will, by default, only search its own assembly (another App_Code DLL, by the looks of the error you received) and core libraries.  You could provide a specific assembly reference "TypeName, AssemblyName" to GetType() but that\'s not possible in the case of the automatically generated assemblies, which have new names after each recompile.</p>\n\n<p>Solution.... I haven\'t done this myself before but I believe that you should be able to use something like this:</p>\n\n<pre><code>System.Web.Compilation.BuildManager.GetType("MyService", true)\n</code></pre>\n\n<p>as the BuildManager is aware of the DLLs it has created and knows where to look.</p>\n\n<p>I guess this really doesn\'t have to do with Web Services but if it were your own code, Daren\'s right about Facade patterns.</p>\n'}, {'answer_id': 730158, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>// Try this -></p>\n\n<pre><code>    Type t = System.Web.Compilation.BuildManager.GetType("MyServiceClass", true);\n    object act = Activator.CreateInstance(t);                \n    object o = t.GetMethod("hello").Invoke(act, null);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/62365', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/60/']