{"id":790,"date":"2006-04-24T22:22:23","date_gmt":"2006-04-25T02:22:23","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=790"},"modified":"2006-04-24T22:22:23","modified_gmt":"2006-04-25T02:22:23","slug":"nant-filepoke-implementation","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2006\/04\/24\/nant-filepoke-implementation\/","title":{"rendered":"NAnt FilePoke Implementation"},"content":{"rendered":"<p>NAnt (for those of you who don&#8217;t know) is the .NET version of Ant. It&#8217;s very similar to Ant, but has some nice extensions, like <a href=\"http:\/\/nant.sourceforge.net\/release\/latest\/help\/tasks\/xmlpoke.html\">xmlpoke<\/a>, which replaces text in an XML file at the location specified by an XPath expression.  I&#8217;ve used xmlpoke a couple different times, usually to meddle with web.config but today I ran into an issue where I need to change a .cs file, which I&#8217;m sure raises a red flag for alot of you. But Microsoft left me know choice. See, in order to create a <a href=\"http:\/\/msdn.microsoft.com\/webservices\/webservices\/building\/wse\/default.aspx\">WSE<\/a> compliant webservice client, you&#8217;re supposed to simply: <\/p>\n<blockquote><p>add a reference to Microsoft.Web.Services2 to your assembly&#8230;[and then]&#8230; change the base class from <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.web.services.protocols.soaphttpclientprotocol.aspx\">SoapHttpClientProtocol<\/a> to the <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/wse3.0_mref\/html\/T_Microsoft_Web_Services3_WebServicesClientProtocol.asp?frame=true\">WebServicesClientProtocol<\/a> class from the Microsoft.Web.Services2 namespace.<\/p><\/blockquote>\n<p> [<a href=\"http:\/\/weblogs.asp.net\/kennykerr\/archive\/2004\/09\/25\/234250.aspx\">source<\/a>]<\/p>\n<p>Now that would be all fine and dandy if I used $isualStudio to create my webservices, but I&#8217;d rather use <a href=\"http:\/\/eclipse.org\/\">Eclipse<\/a> and NAnt. So NAnt has this great task called a <a href=\"http:\/\/nant.sourceforge.net\/release\/latest\/help\/tasks\/script.html\">&lt;script&gt;<\/a> which allows you to plug in any C# code you want and make a task out of it. So here&#8217;s a task I made called filepoke:<br \/>\n<code><br \/>\n&lt;script language=\"C#\"&gt;<br \/>\n&lt;code&gt;<br \/>\n&nbsp;&nbsp;&lt;![CDATA[<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;[TaskName(\"filepoke\")]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public class FilePoke : Task {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private string _file;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private string _target;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private string _value;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[TaskAttribute(\"file\", Required=true)]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public string TargetFile {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get { return _file; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set { _file = value; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[TaskAttribute(\"target\", Required=true)]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public string Target {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get { return _target; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set { _target = value; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[TaskAttribute(\"value\", Required=true)]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public string Value {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get { return _value; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set { _value = value; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected override void ExecuteTask() {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string path = _file;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuilder filecontents = new StringBuilder();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using (StreamReader sr = File.OpenText(path)) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string s = \"\";<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;while ((s = sr.ReadLine()) != null) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filecontents.Append(s.Replace(_target, _value) + \"\\n\");<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using (StreamWriter sw = new StreamWriter(path)) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Write(filecontents.ToString());<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;]]&gt;<br \/>\n&lt;\/code&gt;<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><br \/>\nwhich you can use (like I did) while looping over all the generated proxy .cs files to replace all instances of x with y:<br \/>\n<code><br \/>\n&lt;filepoke file=\"${filename}\" target=\"System.Web.Services.Protocols.SoapHttpClientProtocol\" value=\"Microsoft.Web.Services2.WebServicesClientProtocol\" \/&gt;<br \/>\n<\/code><br \/>\nSeems like it would make way more sense for the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cptools\/html\/cpgrfWebServicesDescriptionLanguageToolWsdlexe.asp\">wsdl.exe<\/a> tool to give you the option of overriding the base class of the generated proxy, but this&#8217;ll get&#8217;er done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NAnt (for those of you who don&#8217;t know) is the .NET version of Ant. It&#8217;s very similar to Ant, but has some nice extensions, like xmlpoke, which replaces text in an XML file at the location specified by an XPath expression. I&#8217;ve used xmlpoke a couple different times, usually to meddle with web.config but today &hellip; <a href=\"https:\/\/cephas.net\/blog\/2006\/04\/24\/nant-filepoke-implementation\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">NAnt FilePoke Implementation<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/790"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=790"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/790\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}