How to use .NET WebClient calls in Clojure-CLR 1.4.1

474 Views Asked by At

In the question: Idiomatic way to write .NET interop function I found the following code segment:

(import '(System.Net HttpWebRequest NetworkCredential)
        '(System.IO StreamReader))

When I start a REPL and enter:

(import '(System.IO StreamReader))

all is OK. But when I enter:

(import '(System.Net HttpWebRequest))

(I don't need the NetworkCredential functionality) I get:

My.ns=> (import '(System.Net HttpWebRequest))
Bad type
NullReferenceException Object reference not set to an instance of an object.  cl
ojure.lang.Util.NameForType (D:\work\clojure-clr-1.4.1-fix\Clojure\Clojure\Lib\U
til.cs:729)

Oh, and I have previously done:

My.ns=> (System.Reflection.Assembly/LoadWithPartialName "System.Net")
#<RuntimeAssembly System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b
03f5f7f11d50a3a>

So what else do I need to do? Thanks for any help.

1

There are 1 best solutions below

0
On

This is a bug in 1.4. It comes from not properly handling a type being accessible from more than one assembly. In this case, System.Net.HttpWebRequest is in the System assembly. Importing that class before you load the System.Net assembly works just fine. After you load the System.Net assembly, HttpWebRequest is accessible from two assemblies (thanks to a TypeForwardedTo attribute in System.Net). In ClojureCLR 1.4.1, this is not handled properly.

Solutions:

  1. Either don't load System.Net or do your import of HttpWebRequest prior to doing so.
  2. Download the latest version of ClojureCLR -- the bug is fixed.
  3. If you need to stay with 1.4.1 and can build it yourself, it's a one-line fix. Look at https://github.com/clojure/clojure-clr/commit/38c34c2a60a63d5933386e412a250c5e8b879d5b .