Category Archives: .NET

NAnt FilePoke Implementation

NAnt (for those of you who don’t know) is the .NET version of Ant. It’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’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’m sure raises a red flag for alot of you. But Microsoft left me know choice. See, in order to create a WSE compliant webservice client, you’re supposed to simply:

add a reference to Microsoft.Web.Services2 to your assembly…[and then]… change the base class from SoapHttpClientProtocol to the WebServicesClientProtocol class from the Microsoft.Web.Services2 namespace.

[source]

Now that would be all fine and dandy if I used $isualStudio to create my webservices, but I’d rather use Eclipse and NAnt. So NAnt has this great task called a <script> which allows you to plug in any C# code you want and make a task out of it. So here’s a task I made called filepoke:

<script language="C#">
<code>
  <![CDATA[
    [TaskName("filepoke")]
    public class FilePoke : Task {
      private string _file;
      private string _target;
      private string _value;
      [TaskAttribute("file", Required=true)]
      public string TargetFile {
          get { return _file; }
          set { _file = value; }
      }
      [TaskAttribute("target", Required=true)]
      public string Target {
          get { return _target; }
          set { _target = value; }
      }
      [TaskAttribute("value", Required=true)]
      public string Value {
          get { return _value; }
          set { _value = value; }
      }
      protected override void ExecuteTask() {
        string path = _file;
        StringBuilder filecontents = new StringBuilder();
        using (StreamReader sr = File.OpenText(path)) {
          string s = "";
           while ((s = sr.ReadLine()) != null) {
              filecontents.Append(s.Replace(_target, _value) + "\n");
          }
      }
      using (StreamWriter sw = new StreamWriter(path)) {
        sw.Write(filecontents.ToString());
      }
      }
    }
  ]]>
</code>
</script>

which you can use (like I did) while looping over all the generated proxy .cs files to replace all instances of x with y:

<filepoke file="${filename}" target="System.Web.Services.Protocols.SoapHttpClientProtocol" value="Microsoft.Web.Services2.WebServicesClientProtocol" />

Seems like it would make way more sense for the wsdl.exe tool to give you the option of overriding the base class of the generated proxy, but this’ll get’er done.

Fiddler, .NET and SOAP

This morning I spent some time playing with WSE, .NET and Java. I knew Fiddler could listen in on conversations between IE and the world, but I never was able to get it to listen to conversations between me and well, me (ie: localhost). Turns out, according to the well named help page, that

“….NET will always bypass the Fiddler proxy for URLs containing localhost. So, rather than using localhost, change your code to refer to the machine name.”

Easy.

eBay Java / C# SOAP Examples

At the start of this year I worked with some guys at eBay to further develop their code samples. Some, but not all of the twelve examples I wrote went live in the recently launched Community Codebase. You can download all the Java examples (of which I wrote three) or browse the Subversion repository. I wrote the SOAPAddItem, SOAPGetItem, and SOAPGetUser items in the Java source tree.

The examples I wrote were different from the majority of the (then) existing examples in that I didn’t make any IDE assumptions (most of the Java examples have JBuilder .jpx files and Eclipse .project files, the .NET projects contain the Visual Studio artifacts.. tsk tsk.) and as such, all the examples I wrote contain comprehensive Ant / NAnt build files which means you can get up and running without having to setup your fancy schmancy IDE. But the biggest difference was that all the examples I wrote used either Ant (with WSDL2Java) or NAnt (with NAntContrib) tasks to conditionally download the eBay WSDL, generate the client stub(s), and compile the resulting code, which makes for a prettier source code repository (generated stubs aren’t checked into source) and gives you compile time checking of your code against the API.

If you’re interested in how you can use Ant or NAnt in a build environment where you access SOAP services, you should check it out!

ASP.NET: The resource cannot be found.

Spent some time today buried in IIS trying to figure out why an ASP.NET application would throw the following error message for every single page that existed in an application within a website:

Server Error in ‘/myapp’ Application.
—————————————————-
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested Url: /myapp/default.aspx
—————————————————-
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032

Googled for “resource cannot be found” and found this link on the third result page. In it Jarrett mentions that he solved the problem by reading this experts-exchange.com question which suggests setting up the application as a Virtual Directory in IIS instead of simply marking the folder in the website as an application, which of course solved the problem for me as well. Since a picture is worth a thousand words, here’s the difference between a Virtual Directory (which is an application in and of itself) and directory in IIS that has been marked as an application:

virtual_directory.gif

directory.gif

My suspicion is that the problem has to do with ASP.NET configuration inheritance settings with Virtual Directories not inheriting from parent directories like a folder marked as an application would, but there isn’t much to go on. Any Microsoft IIS experts out there want to talk about the difference between a Virtual Directory and a folder that has been marked as an application from a configuration standpoint?

daily links

· Bloglines API

· Paul Graham on what the Bubble got right: “… what would be wrong would be that how one presented oneself counted more than the quality of one’s ideas. That’s the problem with formality. Dressing up is not so much bad in itself. The problem is the receptor it binds to: dressing up is inevitably a substitute for good ideas. It is no coincidence that technically inept business types are known as ‘suits.'”

· LionShare: “… an innovative effort to facilitate legitimate file-sharing among individuals and educational institutions around the world.”

· .NET Reflector: allows you to view the source code to most .NET framework classes and code written in .NET. So how does one write / compile code in such a way as to not allow someone to disassemble their code?

The ‘using’ keyword

Joe pointed out that the C# PGP decryption code that I wrote could be better; specifically I should be checking the xExitCode property of the Process instance and the code would also be better served if I made certain that I disposed of the Process instance by calling the Close() method when I’m done using it. The ExitCode improvement is relatively simple to add; start the process, read any lines of output and then check the ExitCode to see if everything went smoothly:

Process process = Process.Start(psi);
...
while(!process.HasExited) {
... // do stuff
}
if (process.ExitCode != 0) {
// something went wrong..
}

The second thing that Joe mentioned was the ‘using’ statement, which is a novel feature of C# that provides “… an easy way for C# code to acquire and release unmanaged or otherwise precious resources.” The code I originally wrote didn’t destroy the handle to the process; after all was said and done I should have called the Close() or Dispose() method of the process:

Process process;
try {
process = Process.Start(psi);
...
} catch {
process.Close();
}

The ‘using’ statement is syntactic sugar that’s a shortcut for the well worn try / catch idiom and shortens the above example to:

using (Process process = Process.Start(psi)) {
... // do stuff w/ the process
}

which then automatically calls the Dispose() method of the process.

Joe goes on to hijack the ‘using’ statement in some other novel ways which should you check out when you have a chance.

Conflicting mindsets of C# vs. Java: Part II

You all read the the ‘Conflicting mindsets of C# vs. Java‘ weblog post right? And you all noticed that the guys running the Lucene.NET project on sourceforge closed up shop, took all their toys and went on home right? I’m gonna go out on a limb and say that they’re related.

The way I see it, *in general* the .NET community conversation is dominated by talk about the latest and greatest that microsoft is putting out; there’s talk about MapPoint Location Server, SQL Server, Longhorn, ASP.NET 2.0 and Visual Studio; all products of Redmond. The same group of Java developers are talking about JBoss, Hibernate, Struts and Eclipse: none of which came out of the Silicon Valley.

Malcolm’s mindset #1 says that .NET developers accept the tools and services that are provided them by Microsoft and I think for the most part this is true. You don’t see .NET developers spending their cycles on persistence layers, web application frameworks or caching solutions probably because Microsoft has provided Microsoft solutions for all these problems. But if it’s just providing tools, then why aren’t JSF, JDO and NetBeans dominating the javablogs conversations? Seriously, take a look at ASP.NET and JSF. They aren’t that different and yet ASP.NET is widely used in conjunction with Visual Studio while JSF is rarely lauded and more often derided. I think he’s right, it’s really a mindset.

Which brings me back to the Lucene.NET guys. Why would they close up shop? Why not continue to donate their time and energy to an excellent cause? Maybe the Microsoft mindset has something to do with it. How about this: a search on google for ‘lucene’ within the weblogs.asp.net domain yields exactly 17 results. The same search on jroller.com yields 2570 results. Admittedly, Lucene has been around longer, but maybe one of the reasons that the Lucene.NET guys packed it up (and are now trying to sell their work) is that no one paid any attention to them because they were all too busy working with SQL Server full-text indexing, a tool given them by Microsoft (but one that costs thousands of dollars per processor). Another reason that a project like Lucene or Struts or Tomcat flourishes is because there is a certain amount of prestige working on a big open source project. If you work on open source projects for the prestige and you’re not getting the attention you think you deserve, you find another motivation. In their case money was a motivation, so they closed up project on sourceforge and they’re selling a personal edition and a business edition. They might make a couple bucks, but I bet in 1 year there won’t be many people writing about searchblackbox.com.

So what’s my point? That all .NET developers are greedy and don’t care about the community? Not really. I think it’s that the two communities have different bus drivers: .NET developers look to Microsoft to provide the tools they need to do their jobs… and if they look elsewhere or copy something else, Microsoft will eventually come in and make a product of their own that does the job, thereby negating any work the developers do in the meantime. Microsoft drives the bus. Java developers look at the products and specs that Sun puts out and then go and build their own tools or frameworks or applications to do the job. Sun will eventually put out something through the JCP that does the job…. but the developers in the Java community will only use it if they want too, witness the continued popularity of Struts and the lack of interest in JSF. In the Java camp, the developers drive the bus.