<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C# &#8211; Dynamically Invoke Web Service At Runtime</title>
	<atom:link href="http://www.crowsprogramming.com/archives/66/feed" rel="self" type="application/rss+xml" />
	<link>http://www.crowsprogramming.com/archives/66</link>
	<description>Computer Programming and Random Blurbs</description>
	<lastBuildDate>Sat, 04 Feb 2012 17:06:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: neil</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-39951</link>
		<dc:creator>neil</dc:creator>
		<pubDate>Sat, 04 Feb 2012 17:06:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-39951</guid>
		<description>Hi, 
  I plan to have a web service running on IIS ( I am new to web services), does a web service stay running after a browser has requested information?

I have written ISAPI , and as soon as it is called it stays in memory running. Saving a lot of time as it opens databases the first time it ran, and any future calls to it there is no need in opening theses databases.

Neil</description>
		<content:encoded><![CDATA[<p>Hi,<br />
  I plan to have a web service running on IIS ( I am new to web services), does a web service stay running after a browser has requested information?</p>
<p>I have written ISAPI , and as soon as it is called it stays in memory running. Saving a lot of time as it opens databases the first time it ran, and any future calls to it there is no need in opening theses databases.</p>
<p>Neil</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alan gilchrist</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-39913</link>
		<dc:creator>alan gilchrist</dc:creator>
		<pubDate>Wed, 01 Feb 2012 16:56:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-39913</guid>
		<description>&lt;strong&gt;Sources...&lt;/strong&gt;

[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]…...</description>
		<content:encoded><![CDATA[<p><strong>Sources&#8230;</strong></p>
<p>[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]…&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lokesh</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-38999</link>
		<dc:creator>lokesh</dc:creator>
		<pubDate>Tue, 29 Nov 2011 13:47:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-38999</guid>
		<description>when I am trying to create an instance of the web service with extension .svc, I am alwasy getting the exception similar below:

“unable to load Service :unable to import binding BasicHttpBinding_IcontractDocumentService from NameSpace: “http://tempuri.org/”.”

Any ideas why?</description>
		<content:encoded><![CDATA[<p>when I am trying to create an instance of the web service with extension .svc, I am alwasy getting the exception similar below:</p>
<p>“unable to load Service :unable to import binding BasicHttpBinding_IcontractDocumentService from NameSpace: “http://tempuri.org/”.”</p>
<p>Any ideas why?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dennise</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-38342</link>
		<dc:creator>Dennise</dc:creator>
		<pubDate>Wed, 02 Nov 2011 06:40:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-38342</guid>
		<description>Hi,

Thanks for posting this code. I have one question, when I am create an instance of the webservice with extension .asmx, the code above is working fine.

However, when I am trying to create an instance of the web service with extension .svc, I am alwasy getting the exception similar below:

&quot;unable to load Service :unable to import binding BasicHttpBinding_IcontractDocumentService from NameSpace: “http://tempuri.org/”.&quot;

Any ideas why?

Thank you.

Dennise</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Thanks for posting this code. I have one question, when I am create an instance of the webservice with extension .asmx, the code above is working fine.</p>
<p>However, when I am trying to create an instance of the web service with extension .svc, I am alwasy getting the exception similar below:</p>
<p>&#8220;unable to load Service :unable to import binding BasicHttpBinding_IcontractDocumentService from NameSpace: “http://tempuri.org/”.&#8221;</p>
<p>Any ideas why?</p>
<p>Thank you.</p>
<p>Dennise</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jean Moulis</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-38270</link>
		<dc:creator>Jean Moulis</dc:creator>
		<pubDate>Mon, 31 Oct 2011 11:29:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-38270</guid>
		<description>Good works and relevant contributions too!

Nevertheless, in a production environment there are still several issues when you compile your assembly dynamically.

See http://msdn.microsoft.com/en-us/library/ms366723.aspx for more information about disadvantages of dynamic compilation.

Regards.</description>
		<content:encoded><![CDATA[<p>Good works and relevant contributions too!</p>
<p>Nevertheless, in a production environment there are still several issues when you compile your assembly dynamically.</p>
<p>See <a href="http://msdn.microsoft.com/en-us/library/ms366723.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms366723.aspx</a> for more information about disadvantages of dynamic compilation.</p>
<p>Regards.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joseph</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-38114</link>
		<dc:creator>Joseph</dc:creator>
		<pubDate>Thu, 27 Oct 2011 15:01:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-38114</guid>
		<description>Please can you help me?

I need to pass an int, string, ref struct to my webservice.

How can I obtain the right value for ref struct?

Cheers!</description>
		<content:encoded><![CDATA[<p>Please can you help me?</p>
<p>I need to pass an int, string, ref struct to my webservice.</p>
<p>How can I obtain the right value for ref struct?</p>
<p>Cheers!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jose Villagrana</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-37849</link>
		<dc:creator>Jose Villagrana</dc:creator>
		<pubDate>Thu, 20 Oct 2011 16:06:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-37849</guid>
		<description>if your service has windows authentication add these lines in the method &quot;private Assembly BuildAssemblyFromWSDL(Uri webServiceUri)&quot;

  XmlTextReader xmlreader = new XmlTextReader(webServiceUri.ToString() + &quot;?wsdl&quot;);

            // Supply the credentials necessary to access the Web server.
            XmlUrlResolver resolver = new XmlUrlResolver();
            resolver.Credentials = new NetworkCredential(UserName, Password);
            xmlreader.XmlResolver = resolver;</description>
		<content:encoded><![CDATA[<p>if your service has windows authentication add these lines in the method &#8220;private Assembly BuildAssemblyFromWSDL(Uri webServiceUri)&#8221;</p>
<p>  XmlTextReader xmlreader = new XmlTextReader(webServiceUri.ToString() + &#8220;?wsdl&#8221;);</p>
<p>            // Supply the credentials necessary to access the Web server.<br />
            XmlUrlResolver resolver = new XmlUrlResolver();<br />
            resolver.Credentials = new NetworkCredential(UserName, Password);<br />
            xmlreader.XmlResolver = resolver;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jose Villagrana</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-37848</link>
		<dc:creator>Jose Villagrana</dc:creator>
		<pubDate>Thu, 20 Oct 2011 16:04:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-37848</guid>
		<description>Hi, Very nice code!
When the method that your invoke have any other parameter that is not string  show an error. make this changes in the &quot;public T InvokeMethod( string serviceName, string methodName, params object[] args )&quot; and will works fine:

MethodInfo method = type.GetMethod(methodName); 
            foreach (MethodInfo minfo in type.GetMethods(BindingFlags.Instance &#124; BindingFlags.Public &#124; BindingFlags.DeclaredOnly))
            {
                if (minfo.Name.ToLower() == methodName.ToLower())
                {        
                    ParameterInfo [] parametesrs = method.GetParameters();
                    object[] availableArgs = new object[minfo.GetParameters().Length];
                    for (int i = 0; i &lt; availableArgs.Length; i++)
                    {
                         object value = Convert.ChangeType(args[i], Type.GetType(parametesrs[i].ParameterType.FullName));
                         availableArgs[i] = value;
                    }
                    result = (T)minfo.Invoke(obj, availableArgs);
                    break;
                }
            }
            return result;</description>
		<content:encoded><![CDATA[<p>Hi, Very nice code!<br />
When the method that your invoke have any other parameter that is not string  show an error. make this changes in the &#8220;public T InvokeMethod( string serviceName, string methodName, params object[] args )&#8221; and will works fine:</p>
<p>MethodInfo method = type.GetMethod(methodName);<br />
            foreach (MethodInfo minfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))<br />
            {<br />
                if (minfo.Name.ToLower() == methodName.ToLower())<br />
                {<br />
                    ParameterInfo [] parametesrs = method.GetParameters();<br />
                    object[] availableArgs = new object[minfo.GetParameters().Length];<br />
                    for (int i = 0; i &lt; availableArgs.Length; i++)<br />
                    {<br />
                         object value = Convert.ChangeType(args[i], Type.GetType(parametesrs[i].ParameterType.FullName));<br />
                         availableArgs[i] = value;<br />
                    }<br />
                    result = (T)minfo.Invoke(obj, availableArgs);<br />
                    break;<br />
                }<br />
            }<br />
            return result;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Zahrobsky</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-37557</link>
		<dc:creator>Christopher Zahrobsky</dc:creator>
		<pubDate>Tue, 11 Oct 2011 16:40:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-37557</guid>
		<description>I rewrote the generic InvokeMethod logic for my current implementation.  The &quot;InvokeMember(methodName&quot; was failing. 
&lt;code&gt; 
        /// 
        /// Invokes the specified method of the named service.
        /// 
        /// The expected return type.
        /// The name of the service to use.
        /// The name of the method to call.
        /// The arguments to the method.
        /// The return value from the web service method.
        public T InvokeMethod(string serviceName, string methodName, params object[] args) 
        {
            // create an instance of the specified service
            // and invoke the method
            T result = default(T);
            object obj = this.webServiceAssembly.CreateInstance(serviceName);
            Type type = obj.GetType();

            foreach (MethodInfo minfo in type.GetMethods(BindingFlags.Instance &#124; BindingFlags.Public &#124; BindingFlags.DeclaredOnly))
            {
                if (minfo.Name.ToLower() == methodName.ToLower())
                {
                    object[] availableArgs = new object[minfo.GetParameters().Length];
                    for (int i = 0; i &lt; availableArgs.Length; i++ )
                    {
                        availableArgs[i] = args[i];
                    }
                    result = (T)minfo.Invoke(obj, availableArgs);
                    break;
                }
            }
            return result;
        }
 &lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I rewrote the generic InvokeMethod logic for my current implementation.  The &#8220;InvokeMember(methodName&#8221; was failing.<br />
<code><br />
        ///<br />
        /// Invokes the specified method of the named service.<br />
        ///<br />
        /// The expected return type.<br />
        /// The name of the service to use.<br />
        /// The name of the method to call.<br />
        /// The arguments to the method.<br />
        /// The return value from the web service method.<br />
        public T InvokeMethod(string serviceName, string methodName, params object[] args)<br />
        {<br />
            // create an instance of the specified service<br />
            // and invoke the method<br />
            T result = default(T);<br />
            object obj = this.webServiceAssembly.CreateInstance(serviceName);<br />
            Type type = obj.GetType();</p>
<p>            foreach (MethodInfo minfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))<br />
            {<br />
                if (minfo.Name.ToLower() == methodName.ToLower())<br />
                {<br />
                    object[] availableArgs = new object[minfo.GetParameters().Length];<br />
                    for (int i = 0; i &lt; availableArgs.Length; i++ )<br />
                    {<br />
                        availableArgs[i] = args[i];<br />
                    }<br />
                    result = (T)minfo.Invoke(obj, availableArgs);<br />
                    break;<br />
                }<br />
            }<br />
            return result;<br />
        }<br />
 </code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jay</title>
		<link>http://www.crowsprogramming.com/archives/66/comment-page-2#comment-30355</link>
		<dc:creator>jay</dc:creator>
		<pubDate>Mon, 18 Jul 2011 01:48:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=66#comment-30355</guid>
		<description>For some reason I keep hitting the warning NoCodeGenerated in ServiceDescriptionImportWarnings. I can&#039;t find anything on what that might be and why. The servicedescription has consumed it the WSDL url fine and if i debug I can see the description information. Any ideas? My WCF service is basicHttp and it&#039;s pretty simple. Thanks everyone.</description>
		<content:encoded><![CDATA[<p>For some reason I keep hitting the warning NoCodeGenerated in ServiceDescriptionImportWarnings. I can&#8217;t find anything on what that might be and why. The servicedescription has consumed it the WSDL url fine and if i debug I can see the description information. Any ideas? My WCF service is basicHttp and it&#8217;s pretty simple. Thanks everyone.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

