2010-03-16 13 views
0

Je développe une application qui utilise WindowsFormsApplicationBase pour appliquer une instance unique. Je reçois l'erreur suivante lors de l'appel d'une méthode sur un objet Remote. Cela fonctionne très bien si je n'utilise pas l'approche Single Instance."Échec d'authentification" lors de l'appel d'une méthode sur un objet distant dans wpf

System.Runtime.Remoting.RemotingException: Échec d'authentification ---> System.IO.IOException: Impossible de lire les données de la connexion de transport: La connexion a été fermée. à System.Net.Security.NegoState.ProcessAuthentication (LazyAsyncResult lazyResult) à System.Net.Security.NegotiateStream.AuthenticateAsClient (de lettres de créance NetworkCredential, String targetName, ProtectionLevel requiredProtectionLevel, TokenImpersonationLevel allowedImpersonationLevel) à System.Runtime.Remoting.Channels.Tcp .TcpClientTransportSink.CreateAuthenticatedStream (flux netStream, String machinePortAndSid)

Voici mon code:

public class EntryPoint 
{ 
    [STAThread] 
    public static void Main(string[] args) 
    { 
     SingleInstanceManager sim = new SingleInstanceManager(); 
     sim.Run(args); 
    } 
} 


public class SingleInstanceManager : WindowsFormsApplicationBase 
{ 
    private App app; 

    public SingleInstanceManager() 
    { 
     IsSingleInstance = true; 
    } 

    protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs) 
    { 
     app = new App(); 
     app.InitializeComponent(); 
     app.Run(); 
     return false; 
    } 

    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) 
    { 
     base.OnStartupNextInstance(eventArgs); 
     app.Activate(); 
    } 
} 

Voici comment j'invoque l'objet Remoting:

public Hashtable GetData(string[] arg1, string[] arg2) 
{ 
    IDataProvider dataProvider = (IDataProvider)Activator.GetObject(typeof(IDataProvider), "tcp://....."); 

    Hashtable data = dataProvider.GetData(arg1, arg2); 

    return data; 
} 

Merci d'avance.

Répondre

0

J'ai trouvé la solution moi-même.

J'ai utilisé ce qui suit pour implémenter une instance unique (http://www.ai.uga.edu/mc/SingleInstance.html).

[STAThread] 
static void Main()     // args are OK here, of course 
{ 
    bool ok; 
    m = new System.Threading.Mutex(true, "YourNameHere", out ok); 

    if (! ok) 
    { 
     MessageBox.Show("Another instance is already running."); 
     return; 
    } 

    Application.Run(new Form1()); // or whatever was there 

    GC.KeepAlive(m);    // important! 
}