1

サービス:

public class Service:IContracts
{
    private const string baseLocation = "E:\\";
    private FileStream _stream;
    private byte[] _buffer;
    public double Add(double x, double y) 【2】
    {
        return x + y;
    }

    public IAsyncResult BeginAsy(string fileName, AsyncCallback userCallback, object stateObject)【1】
    {
         this._stream = new FileStream(baseLocation + fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
         this._buffer = new byte[this._stream.Length];
         return this._stream.BeginRead(this._buffer, 0, this._buffer.Length, userCallback, stateObject);
    }

    public string EndAsy(IAsyncResult ar)
    {
        this._stream.EndRead(ar);
        this._stream.Close();
        Thread.Sleep(3000);
        return Encoding.ASCII.GetString(this._buffer);
     }
}

クライアント: 非同期呼び出し。

   proxy.BeginAsy("test.txt", asy =>   //'test.txt' is a large file 
        {
            Console.WriteLine(proxy.EndAsy(asy));
        }, null);
 Console.WriteLine(proxy.Add(1.0, 1.0));

コードのインターフェイス 'IService' と 'host' は絶対に正しいです! 投稿しないのですが、コード【1】が完了した後にコード【2】が実行されるのはなぜですか?サーバーは非同期です?そうでない場合は、非同期のWCFサーバーを使用する方法は? コード【1】がコード【2】の実行に影響を与えないことを願っています。コード【1】の実行中にコード【2】を実行できます。</p>

4

0 に答える 0