110,536
社区成员
发帖
与我相关
我的任务
分享
[ServiceContract]
public interface ISampleService
{
[OperationContract]
string SampleMethod(string mesg);
[OperationContractAttribute(AsyncPattern = true)]
IAsyncResult BeginSampleMethod(string mesg, AsyncCallback callback, object stat);
string EndSampleMethod(IAsyncResult ar);
}
public class SampleService :ISampleService
{
private const string baseLocation = @"D:\";
private StreamWriter _stream = null;
private byte[] _buffer = null;
public string SampleMethod(string mesg)
{
Console.WriteLine("调用异步方法");
return mesg+"ample";
}
public IAsyncResult BeginSampleMethod(string mesg, AsyncCallback callback, object stat)
{
_stream = new StreamWriter(baseLocation + mesg, true);
_stream.WriteLine("beijing");
_stream.Close();
//Console.WriteLine("streamReader");
return new CompleteAsyncResult<string>("over");
}
public string EndSampleMethod(IAsyncResult ar)
{
var result = ar as CompleteAsyncResult<string>;
Console.WriteLine("EndSampleMethod:"+result.Data);
Console.WriteLine("streamReader");
return result.Data;
}
}
public IAsyncResult BeginSampleMethod
我就是纠结于这个函数体执行后,怎么没效果,文本并没有输出到文件。
var channel = new ChannelFactory<Service.ISampleService>("uy").CreateChannel();
using (channel as IDisposable)
{
var ar = channel.BeginSampleMethod("test.txt", delegate { Console.WriteLine("异步开始"); }, null);
ar.AsyncWaitHandle.WaitOne();
var res = channel.EndSampleMethod(ar);
if (ar.IsCompleted)
Console.WriteLine("ok");
else Console.WriteLine("no");
Console.WriteLine(res);
}
@ findcaiyzh ,我已经调用了啊。我都犯糊涂了,begin中的方法竟然没执行。