Async calls to webservice

Hi,
I'm working on a intranet solution.
I'm trying to call asynchronously a web service on the same server and virtual directory of the aspx page (the one that generates the html with the <object ...> tag).
The smart client actually gets a dataset from the webservice and displays it on a datagrid.
With the sync version everything works fine.
With the async when I use Invoke to set the dataset:
public void getSessionDSCallback(IAsyncResult ar)
{
Trace.WriteLine("inizio getSessionDSCallback");
ds = new DataSet();
it.creval.WPEX.Includes.ControlliBrowser.WPEX.Net.ListaAssegni pf = (it.creval.WPEX.Includes.ControlliBrowser.WPEX.Net.ListaAssegni) ar.AsyncState;
pf.Credentials = CredentialCache.DefaultCredentials;
// Get the completed results.
ds = pf.EndgetSessionDS(ar);
DataBindToDataGridMethodDelegate CallDataBindToDataGrid = new DataBindToDataGridMethodDelegate(this.DataBindToDataGrid);
Object[] newArgs = new Object[1];
newArgs[0] = ds;
Trace.WriteLine("invoke");
try
{
Invoke(CallDataBindToDataGrid, newArgs);
}
catch (Exception anException)
{ Trace.WriteLine("getSessionDSCallback " + anException.Message + anException.StackTrace);
}
Trace.WriteLine("fine getSessionDSCallback");
}
I get
[964] inizio getSessionDSCallback
[964] invoke
[964] getSessionDSCallback Request for the permission of type System.Security.Permissions.SecurityPermission, mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed. at System.Security.CodeAccessSecurityEngine.CheckHelper(PermissionSet grantedSet, PermissionSet deniedSet, CodeAccessPermission demand, PermissionToken permToken)
[964] at System.Security.CodeAccessSecurityEngine.Check(PermissionToken permToken, CodeAccessPermission demand, StackCrawlMark& stackMark, Int32 checkFrames, Int32 unrestrictedOverride)
[964] at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
[964] at System.Security.CodeAccessPermission.Demand()
[964] at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
[964] at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
[964] at it.creval.WPEX.Includes.ControlliBrowser.SessionDataGrid.getSessionDSCallback(IAsyncResult ar)
[964] fine getSessionDSCallback

This problem is caused by the different thread were the getSessionDSCallback method is called.
The call to invoke use some unmanaged code.
When I give "Allow calls to unmanaged assemblies" to all_code the smart client is working fine.
Does someone if there's a way to give this policy only to a subset of code?

Thanks in advance for any help.
Carlo

[2911 byte] By [codefund.com] at [2008-2-15]
# 1
I have used Control.Invoke in my embedded intranet Windows Forms app without giving permission to execute unmanaged code.

However I have recently run into a problem that also involves asychronous calls to a web service, I posted this in the security forum:-

http://www.windowsforms.com/Forums/ShowPost.aspx?tabIndex=1&tabId=41&PostID=273

I'm beginning to get that sinking feeling that the whole WindowsForms.NET runtime is a little flakey inside the IE process.

You're other option is to change the security level for the Local Intranet which can be done by going to Control Panel->Admin Tools->.NET FrameWork Wizards->Adjust .NET Security. Give your local intranet applications Full Trust.

In my opinion you shouldn't need to do this. Your code should work with the default security for the Local Intranet. This basically needlessly forces you to extra administrative work for all clients using your app.

Oh..... and do make sure IE says Local Intranet in the bottom right corner when you run your app.

Michael

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,ClickOnce and Setup & Deployment Projects...
# 2
Hi,
I forgot to say that I use .net framework 1.1, so I think that if you're deploying your solution you are using 1.0. I'm right?
> I'm beginning to get that sinking feeling that the whole WindowsForms.NET runtime is a little flakey inside the IE process.

Having to start working with smar client I'm much worried about your sentence.

If you read an article by Chris Sells "Increasing Permissions for Web-Deployed Windows Forms Applications" he explain very well how things works.
So it isn't necessary to give full trust of all intranet sites (maybe this is not a problem for you).
It seems strange to me that you have no problem with invoke. Your smart client are used in the intranet with full trust?
If not I'm confused.
Looking into code of Invoke (with Anakrino) there's a request for unmanaged code.
I use this version

public virtual object Invoke(Delegate method, object[] args)
Are you using the same one?
In this method is used
IntSecurity.UnmanagedCode.Demand();

So you need UnmanagedCode permission.

Cheers
Carlo

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,ClickOnce and Setup & Deployment Projects...
# 3
This request is conditional....

local0 = (ActiveXImpl) this.Properties.GetObject(Control.PropActiveXImpl);
if (local0 != null)
IntSecurity.UnmanagedCode.Demand();

When I call invoke I only ever user the main form to call Invoke....Perhaps local0 is null for a System.Windows.Forms.Form control.

What control are you calling Invoke on ?

Michael

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,ClickOnce and Setup & Deployment Projects...