Deux questions à propos de AsyncCallback et modèle IAsyncResult

Deux questions sur le rappel du motif avec AsyncCallback et IAsyncResult.

J'ai changé la question avec un exemple de code:

using System;
using System.Collections.Generic;
using System.Text;
namespace TestAsync
{
class Program
{
private static Wrapper test = new Wrapper();
static void Main(string[] args)
{
test.BeginMethod("parameter 1", "parameter 2", Callback);
Console.ReadKey();
}
private static void Callback(IAsyncResult ar)
{
string result = test.EndMethod(ar);
}
}
public interface ITest
{
IAsyncResult BeginMethod(string s1, string s2, AsyncCallback cb, object state);
string EndMethod(IAsyncResult result);
}
public class Wrapper
{
private ITest proxy = new Test();
public void BeginMethod(string s1, string s2, AsyncCallback cb)
{
proxy.BeginMethod(s1, s2, cb, proxy);
}
public string EndMethod(IAsyncResult result)
{
return ((ITest)(result.AsyncState)).EndMethod(result);
}
}
public class Test : ITest
{
private string WorkerFunction(string a, string b)
{
//"long running work"
return a + "|" + b;
}
public IAsyncResult BeginMethod(string s1, string s2, AsyncCallback cb, object state)
{
Func<string, string, string> function = new Func<string, string, string>(WorkerFunction);
IAsyncResult result = function.BeginInvoke(s1, s2, cb, state);
return result;
}
public string EndMethod(IAsyncResult result)
{
return (string)(result.AsyncState);
}
}
public delegate TResult Func<T1, T2, TResult>(T1 t1, T2 t2);
}

COMMENCER À MODIFIER
Je commence à voir ce qui se passe.
J'ai mélangé un WCF async modèle et de normal async modèle.
Dans WCF on utilise un serveur proxy et le Commencer - et cette instruction se doit être passé le proxy et non pas la fonction de délégué. Dans la WCF cas, le casting fonctionne, dans le cas normal, pas.
WCF utilise le [OperationContract(AsyncPattern = true)] attribut probablement à appliquer un peu de modèle différent.
FIN MODIFIER

Pourquoi l'erreur sur la ligne return (string)(result.AsyncState); ?
Exactement le même modèle dans le code de production est ok.

Deuxièmement, pourquoi je ne peux pas le code de débogage dans BeginMethod de la classe de Test?
Je ne peux pause dans WorkerFunction.

OriginalL'auteur Gerard | 2011-02-23