Reflection in a Web Service?

G

Guest

This code works great in a windows application:

// Dynamically load the DLL
sDLLName = "..\\bin\\Test2.dll";
Assembly objAssembly = System.Reflection.Assembly.LoadFrom(sDLLName);

// Create an instance of the class
object testObject = objAssembly.CreateInstance("Test2.Test");

// Call the method "TestMessage" which has one argument
object[] object2 = new object[1];
object2[0] = 200;

MethodInfo Method = testObject.GetType().GetMethod("TestMessage");
object result = Method.Invoke(testObject, object2);

// This should return 300 and does
Console.WriteLine(result.ToString());

However, from a web service project, I can't seem to get this to work. It
can't find the DLL.

Questions:

* Does IIS sandbox the project and not allow external references?
* Is there a special way that IIS needs set up to allow this?
* I assume that IIS works with server paths, so maybe the path I have
specified above does not work. Should I be using a different path variable?
* Does IIS shadow the web service files to another location making this
approach impossible?

Any help is most appreciated!

Thanks!
Michael
 
G

Guest

- asp.net doesn't sandbox by default (can change this in web.config)
- what happens if you change the code to?
sDLLName = "Test2.DLL";
Assembly.Load(sDLLName)

and place Test2.DLL in the bin directory of the web service

- asp.net does shadow copy DLLs but that shouldn't affect the path operations.

Niroo [MSFT]
 
G

Guest

Hi! The problem is that we use the same DLLs for other apps on the server.
For example, a windows service, desktop app, etc. It would be nice to have
the same DLL used in all cases and not 3 copies for each project.

Thanks!
Michael

Niroo TP said:
- asp.net doesn't sandbox by default (can change this in web.config)
- what happens if you change the code to?
sDLLName = "Test2.DLL";
Assembly.Load(sDLLName)

and place Test2.DLL in the bin directory of the web service

- asp.net does shadow copy DLLs but that shouldn't affect the path operations.

Niroo [MSFT]

moflaherty said:
This code works great in a windows application:

// Dynamically load the DLL
sDLLName = "..\\bin\\Test2.dll";
Assembly objAssembly = System.Reflection.Assembly.LoadFrom(sDLLName);

// Create an instance of the class
object testObject = objAssembly.CreateInstance("Test2.Test");

// Call the method "TestMessage" which has one argument
object[] object2 = new object[1];
object2[0] = 200;

MethodInfo Method = testObject.GetType().GetMethod("TestMessage");
object result = Method.Invoke(testObject, object2);

// This should return 300 and does
Console.WriteLine(result.ToString());

However, from a web service project, I can't seem to get this to work. It
can't find the DLL.

Questions:

* Does IIS sandbox the project and not allow external references?
* Is there a special way that IIS needs set up to allow this?
* I assume that IIS works with server paths, so maybe the path I have
specified above does not work. Should I be using a different path variable?
* Does IIS shadow the web service files to another location making this
approach impossible?

Any help is most appreciated!

Thanks!
Michael
 
J

Joshua Flanagan

As I mentioned before, you should always use Assembly.Load() instead of
Assembly.LoadFrom() when you are loading the assembly to use its
functionality.

If you want to have 1 copy of the files in a single location, you have a
few options:

1) Put the files in the Global Assembly Cache
http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconglobalassemblycache.asp

2) Use the codebase element of assemblyBinding in your config file:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfcodebase.asp

3) Use the probing element of assemblyBinding in your config file:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfprobing.asp


For a great resource on understanding Fusion (the .NET assembly loader),
do this workshop:
http://www.grimes.demon.co.uk/workshops/fusionWS.htm


Joshua Flanagan
http://flimflan.com/blog


Hi! The problem is that we use the same DLLs for other apps on the server.
For example, a windows service, desktop app, etc. It would be nice to have
the same DLL used in all cases and not 3 copies for each project.

Thanks!
Michael

:

- asp.net doesn't sandbox by default (can change this in web.config)
- what happens if you change the code to?
sDLLName = "Test2.DLL";
Assembly.Load(sDLLName)

and place Test2.DLL in the bin directory of the web service

- asp.net does shadow copy DLLs but that shouldn't affect the path operations.

Niroo [MSFT]

:

This code works great in a windows application:

// Dynamically load the DLL
sDLLName = "..\\bin\\Test2.dll";
Assembly objAssembly = System.Reflection.Assembly.LoadFrom(sDLLName);

// Create an instance of the class
object testObject = objAssembly.CreateInstance("Test2.Test");

// Call the method "TestMessage" which has one argument
object[] object2 = new object[1];
object2[0] = 200;

MethodInfo Method = testObject.GetType().GetMethod("TestMessage");
object result = Method.Invoke(testObject, object2);

// This should return 300 and does
Console.WriteLine(result.ToString());

However, from a web service project, I can't seem to get this to work. It
can't find the DLL.

Questions:

* Does IIS sandbox the project and not allow external references?
* Is there a special way that IIS needs set up to allow this?
* I assume that IIS works with server paths, so maybe the path I have
specified above does not work. Should I be using a different path variable?
* Does IIS shadow the web service files to another location making this
approach impossible?

Any help is most appreciated!

Thanks!
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top