Begin 및 End 메서드가 실행되지 않는 이유는 동기 인터페이스가 서비스 인터페이스에 정의되어 있습니까? 테스트 결과는 원래의 코드를 들어비동기 메서드 실행
public double GetSquareRoot(double value)
{
System.Diagnostics.Debugger.Log(0, "MyService", "Get - Start\n");
Thread.Sleep(3000);
System.Diagnostics.Debugger.Log(0, "MyService", "Get - Finish\n");
return Math.Sqrt(value);
}
public IAsyncResult BeginGetSquareRoot(double value, AsyncCallback callback, object state)
{
System.Diagnostics.Debugger.Log(0, "MyService", "Begin - Start\n");
GetSquareRootAsyncResult asyncResult = new GetSquareRootAsyncResult(callback, state);
asyncResult.Value = value;
ThreadPool.QueueUserWorkItem(new WaitCallback((Callback)), asyncResult);
System.Diagnostics.Debugger.Log(0, "MyService", "Begin - Finish\n");
return asyncResult;
}
public double EndGetSquareRoot(IAsyncResult asyncResult)
{
System.Diagnostics.Debugger.Log(0, "MyService", "End - Start\n");
double result = 0;
using (GetSquareRootAsyncResult getSquareRootAsyncResult = asyncResult as GetSquareRootAsyncResult)
{
getSquareRootAsyncResult.AsyncWaitHandle.WaitOne();
result = getSquareRootAsyncResult.Result;
}
System.Diagnostics.Debugger.Log(0, "MyService", "End - Finish\n");
return result;
}
: 나는 일부 디버그 정보를 추가 한 http://www.danrigsby.com/blog/index.php/2008/03/26/async-operations-in-wcf-iasyncresult-model-server-side/
:
- 여기 이 문서의 끝 부분에 예입니다 Get - Start
- Get - Finish
그럼 난 IMyService에서 GetSquareRoot 방법을 삭제, 내가있어 :
- 시작 -
- 가져 오기를 시작합니다 - - 마침을
- 콜백 - -
- 가져 오기를 시작
- 시작하기 시작 마침을
- 콜백 - 완료
- 끝 - 시작
- 엔드 - 마침
왜 그럴까요?
클라이언트 번호 :
public partial class Form1 : Form
{
IMyService m_Client;
public Form1()
{
InitializeComponent();
ChannelFactory<IMyService> factory = new ChannelFactory<IMyService>("netTcp");
factory.Open();
m_Client = factory.CreateChannel();
}
private void button1_Click(object sender, EventArgs e)
{
double value = 0;
Double.TryParse(m_ValueTextBox.Text, out value);
m_Client.BeginGetSquareRoot(value, OnEndGetSquareRoot, null);
m_StartButton.Enabled = false;
m_ResultTextBox.Text = @"Loading...";
}
public void OnEndGetSquareRoot(IAsyncResult asyncResult)
{
this.Invoke(new MethodInvoker(delegate()
{
m_ResultTextBox.Text =
m_Client.EndGetSquareRoot(asyncResult).ToString();
m_StartButton.Enabled = true;
}));
}
}
설명한 사례의 차이 IMyService 인터페이스 선언 GetSquareRoot의 부재이다.
어떻게 웹 서비스 클라이언트 측을 호출합니까? –
'using '블록이'IAsyncResult' 인수를 처리하지 않게할까요? – Amy