Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > Marshal.GetActiveObject throws an exception in a C# webservice but

Reply
Thread Tools

Marshal.GetActiveObject throws an exception in a C# webservice but

 
 
OfurGørn
Guest
Posts: n/a
 
      01-28-2005
....it does not throw the exception in a C# form. The same code works
perfectly in C# forms application. It seems like the computer noes not let
ASPNET get into the ROT, where the object I want is. I have experienced with
changing users, impersonating and so on, but with no luck.
I test which user I am before the faulty code, with

WindowsIdentity.GetCurrent().Name

where I was the user ASPNET when I started, but now I am "myself" who,
incidentally am running the process I am trying to reach.

the exceptions are:
"System.NullReferenceException: Object reference not set to an instance of
an object", when I run a method from the object I am trying to load.
"System.Runtime.InteropServices.COMException (0x800401E3): Operation
unavailable at System.Runtime.InteropServices.Marshal.GetActiveOb ject(Guid&
rclsid, UInt32 reserved, Object& ppunk) at
System.Runtime.InteropServices.Marshal.GetActiveOb ject(String progID)", when
I try to access the external object itself with Marshal.GetActiveObject()

Like I typed before, exactly the same code works in the windows forms
version of C#, but with the web service this seems impossible.

This error is driving me mad !!! I have been trying to fix this but am
afraid that I've become so frustrated that I cannot see the solution now -
typically a simple solution....

________________________
Gunnar Gudmundsson
 
Reply With Quote
 
 
 
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      01-29-2005
Hi Gunnar,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you're trying to get the currenty
identity name using WindowsIdentity.GetCurrent().Name, and
NullReferenceException was thrown. If there is any misunderstanding, please
feel free to let me know.

I have tried it on my computer, however, it works fine and returns NT
AUTHORITY\SYSTEM as the name.

I suggest you add the following line before getting the identity name.

AppDomain.CurrentDomain.SetPrincipalPolicy(System. Security.Principal.Princip
alPolicy.WindowsPrincipal);

Also, you can try to use the following to get the identity for alternative.
(But make sure to add the above line.)

System.Threading.Thread.CurrentPrincipal.Identity. Name

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

 
Reply With Quote
 
 
 
 
OfurGørn
Guest
Posts: n/a
 
      01-31-2005
hello Kevin, and many thanks for the answer.
We have a bit of a misunderstanding here.
I have no issues with the identity. I have althogh problems in running
Marshal.GetActiveObject() in a web service, as the object supposed to get a
value from that is null. When I try to use that handle I get a
COMexceptionError:
"System.Runtime.InteropServices.COMException (0x800401E3): Operation
unavailable at
System.Runtime.InteropServices.Marshal.GetActiveOb ject(Guid&
rclsid, UInt32 reserved, Object& ppunk) at
System.Runtime.InteropServices.Marshal.GetActiveOb ject(String progID)"

The reason this is bugging me a lot is that I have written a C# form
application with the same code, which works perfectly. There is something
different with the C# web service that results with that I cannot get access
to the obejct using Marshal.GetactiveObject()

I am hoping that this is something very simple for you guys, as I have no
more ideas to resolve this.

regards,
Gunnar

"Kevin Yu [MSFT]" wrote:

> Hi Gunnar,
>
> First of all, I would like to confirm my understanding of your issue. From
> your description, I understand that when you're trying to get the currenty
> identity name using WindowsIdentity.GetCurrent().Name, and
> NullReferenceException was thrown. If there is any misunderstanding, please
> feel free to let me know.
>
> I have tried it on my computer, however, it works fine and returns NT
> AUTHORITY\SYSTEM as the name.
>
> I suggest you add the following line before getting the identity name.
>
> AppDomain.CurrentDomain.SetPrincipalPolicy(System. Security.Principal.Princip
> alPolicy.WindowsPrincipal);
>
> Also, you can try to use the following to get the identity for alternative.
> (But make sure to add the above line.)
>
> System.Threading.Thread.CurrentPrincipal.Identity. Name
>
> HTH.
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>
>

 
Reply With Quote
 
[MSFT]
Guest
Posts: n/a
 
      02-01-2005
Hello,

You need to give enough permissions to the user that the Web Service is
running under.

Take a look at:
Q317012 - INFO: Process and Request Identity in ASP.NET
Q306158 - INFO: Implementing Impersonation in an ASP.NET Application

Also, Marshal.GetActiveObject() method uses SecurityAction.LinkDemand to
prevent it from being called from untrusted code; only the immediate caller
is required to have SecurityPermissionAttribute.UnmanagedCode permission.
You may try following code in your APP to see it can work:

PermissionSet ps = new PermissionSet(PermissionState.None);
ps.AddPermission( new
SecurityPermission(SecurityPermissionFlag.Unmanage dCode ) );
ps.Assert();

Hope this help,

Luke



 
Reply With Quote
 
OfurGørn
Guest
Posts: n/a
 
      02-01-2005
Hi Luke

Thanks for the reply. Unfortunately, this solution does not work either.
The faulty code is as follows now:

// check which user we are
string user= WindowsIdentity.GetCurrent().Name;

// impersonate - unneccessary for form application
if (impersonateValidUser("Administrator", "MyComputer", "passWord"))
{
// just for kicks, check who we are now...
// should be the impersonated user
string impostor = WindowsIdentity.GetCurrent().Name;
// check if tstApp is running
System.Diagnostics.Process[] processes;
processes = Process.GetProcessesByName("tstApp32");
// check if the executable is running
if (processes.Length == 0)
{
zVal.zenError += "ZRT: tstApp32 is NOT running !! ";
}
else
{
zVal.zenError += "ZRT: tstApp32 is running !! ";
// the test application is found, get the handle !!
// the permissions have to be in order...
System.Security.PermissionSet ps = new
System.Security.PermissionSet
(System.Security.Permissions.
PermissionState.None);
ps.AddPermission(new
System.Security.Permissions.SecurityPermission
(System.Security.Permissions.

SecurityPermissionFlag.UnmanagedCode));
ps.Assert();

// the permissions should now be in order.... get the handle !!!
m_tstApplication = Marshal.GetActiveObject("tst.Application")
as tst.Application;
}

I am clueless now.... do you have any ideas on what I am doing wrong ????
The variable "impostor" now contains the same as the variable in my Form
program (that works), I added the ps stuff now, but cant see any difference.

What do you mean by giving enough permissions to the user that the Web
Service is running under ?? Isn't it enough to impersonate ? if not, how do I
give ASPNET the required permissions, do I change the permissions for the
executable that I run to start the "tst.Application" in the ROT.

I think we are getting somewhere now
best regards,
Gunnar

 
Reply With Quote
 
[MSFT]
Guest
Posts: n/a
 
      02-02-2005
Hi Gunnar,

Is "tst.Application" an application developped by your team? Not all
application are able to automate in asp.net. Have you try your code with
other applications. For example, Word or Excel?

Luke

 
Reply With Quote
 
OfurGørn
Guest
Posts: n/a
 
      02-02-2005
Hi Luke.

Tst.Application is an external application, that is developed elsewhere. I
do not have access to the source code for it. The problem is that I thought
that if I can develop a C# .NET form application, the code should work in C#
web service.....

I could send the author of the application a note regarding this, but it
would be better if I could be more precise on how the automation should be in
order for the web servive to be able to access it.

I will test my application with Word now, I expect it to work without
problems.

Any extra thoughts on this would be well appreciated.

Gunnar

"[MSFT]" wrote:

> Hi Gunnar,
>
> Is "tst.Application" an application developped by your team? Not all
> application are able to automate in asp.net. Have you try your code with
> other applications. For example, Word or Excel?
>
> Luke

 
Reply With Quote
 
[MSFT]
Guest
Posts: n/a
 
      02-03-2005
Hi Gunnar,

Based on my experience, some component may work in a winform but not in a
web service (ASP.NET) application. This is caused by security and many
other issues, for example, we cannot perform any UI related operation in a
web service. What is the result you test with Word?

Luke

 
Reply With Quote
 
OfurGørn
Guest
Posts: n/a
 
      02-03-2005
Hi Luke.

I wrote a VERY simple application to test Word. First, I wrote it as a C#
form application, and when that worked, I wrote it as a web service. The
"good" news is that I get exactly the same error as in the other one. The
code follows:

/******* CODE *******/
public Word.Application wordApp;
[WebMethod]
public string isWordThere()
{
// impersonate - unneccessary for form
if (impersonateValidUser("Administrator", "localhost", "password"))
{
try
{
// get the active object
wordApp = Marshal.GetActiveObject("Word.Application.11") as
Word.Application;
// stop impersonation
undoImpersonation();
// AOK - return a string to say so
return "AOK: word is running !!!! ";
}
catch (System.Exception ex)
{
undoImpersonation();
// exception - return it to see it
return "exception: "+ex.ToString();
}
}
else
{
// I dont think I need to undo a failed impersonation....
undoImpersonation();
// tell the user that the impersonation failed
return "impersonation failed";
}
}
/******* END CODE *******/

The thing is, I can use word if I dont use Marshal.GetActiveObject(), but
use appWord = new Word.Application() in stead. I haven't tried that in my
application, and the reason for that is that the supplier of the software
expects me to use the Marshal method.

The impersonation code is from one of microsofts articles mentioned before
in this thread - therefore not copied into this reply.

As before, I get the following error:
/***** ERROR *****/
System.Runtime.InteropServices.COMException (0x800401E3): Operation
unavailable
at System.Runtime.InteropServices.Marshal.GetActiveOb ject(Guid& rclsid,
UInt32 reserved, Object& ppunk)
at System.Runtime.InteropServices.Marshal.GetActiveOb ject(String progID)
at OfiiceWebService.OfficeService.isWordThere()
in c:\inetpub\wwwroot\ofiicewebservice\officeservice1 .asmx.cs:line 101
/***** END ERROR *****/

I am going to experiment with a Marshal-less version of my software, but to
indulge the external software provider (and my boss ), I want to have a
Marshal version working at some point.

I have been experimenting with the dcomcnfg utility to grant permissions for
my application and word, but with no luck.

I am getting more sure that the error has a trivial solution - which I
cannot see.
regards
Gunnar
"[MSFT]" wrote:

> Hi Gunnar,
>
> Based on my experience, some component may work in a winform but not in a
> web service (ASP.NET) application. This is caused by security and many
> other issues, for example, we cannot perform any UI related operation in a
> web service. What is the result you test with Word?
>
> Luke
>
>

 
Reply With Quote
 
OfurGørn
Guest
Posts: n/a
 
      02-04-2005
It seems like Marshaling is the only way to go on my application, do you have
any ideas ???

regards,
Gunnar

"OfurGørn" wrote:

> Hi Luke.
>
> I wrote a VERY simple application to test Word. First, I wrote it as a C#
> form application, and when that worked, I wrote it as a web service. The
> "good" news is that I get exactly the same error as in the other one. The
> code follows:
>
> /******* CODE *******/
> public Word.Application wordApp;
> [WebMethod]
> public string isWordThere()
> {
> // impersonate - unneccessary for form
> if (impersonateValidUser("Administrator", "localhost", "password"))
> {
> try
> {
> // get the active object
> wordApp = Marshal.GetActiveObject("Word.Application.11") as
> Word.Application;
> // stop impersonation
> undoImpersonation();
> // AOK - return a string to say so
> return "AOK: word is running !!!! ";
> }
> catch (System.Exception ex)
> {
> undoImpersonation();
> // exception - return it to see it
> return "exception: "+ex.ToString();
> }
> }
> else
> {
> // I dont think I need to undo a failed impersonation....
> undoImpersonation();
> // tell the user that the impersonation failed
> return "impersonation failed";
> }
> }
> /******* END CODE *******/
>
> The thing is, I can use word if I dont use Marshal.GetActiveObject(), but
> use appWord = new Word.Application() in stead. I haven't tried that in my
> application, and the reason for that is that the supplier of the software
> expects me to use the Marshal method.
>
> The impersonation code is from one of microsofts articles mentioned before
> in this thread - therefore not copied into this reply.
>
> As before, I get the following error:
> /***** ERROR *****/
> System.Runtime.InteropServices.COMException (0x800401E3): Operation
> unavailable
> at System.Runtime.InteropServices.Marshal.GetActiveOb ject(Guid& rclsid,
> UInt32 reserved, Object& ppunk)
> at System.Runtime.InteropServices.Marshal.GetActiveOb ject(String progID)
> at OfiiceWebService.OfficeService.isWordThere()
> in c:\inetpub\wwwroot\ofiicewebservice\officeservice1 .asmx.cs:line 101
> /***** END ERROR *****/
>
> I am going to experiment with a Marshal-less version of my software, but to
> indulge the external software provider (and my boss ), I want to have a
> Marshal version working at some point.
>
> I have been experimenting with the dcomcnfg utility to grant permissions for
> my application and word, but with no luck.
>
> I am getting more sure that the error has a trivial solution - which I
> cannot see.
> regards
> Gunnar
> "[MSFT]" wrote:
>
> > Hi Gunnar,
> >
> > Based on my experience, some component may work in a winform but not in a
> > web service (ASP.NET) application. This is caused by security and many
> > other issues, for example, we cannot perform any UI related operation in a
> > web service. What is the result you test with Word?
> >
> > Luke
> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is an exception specification needed if the method throws and catchesits own exception? elcapitan666@gmail.com C++ 2 11-27-2007 04:01 AM
Marshal.GetActiveObject throws an exception in a C# webservice but OfurGørn ASP .Net Web Services 1 02-01-2005 02:28 AM
Re: session.abandon throws 'invalid view state' exception in netscape Cowboy \(Gregory A. Beamer\) ASP .Net 1 05-16-2004 02:31 AM
Exception Exception is not compatible with throws clause in Runnable.run() Chris Miller Java 4 11-22-2003 03:11 PM
XmlValidatingReader throws exception for SAOP-ENV:encodingStyle attribute Himmat Dhange ASP .Net 0 08-26-2003 08:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57