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'是绝对正确的!不会post,为什么代码【1】完成后代码【2】会被执行?服务器是异步的?如果不是,WCF服务器异步是怎么用的?我希望代码【1】不影响代码【2】的执行,代码【2】可以在代码【1】执行的时候被执行。</p>

4

0 回答 0