分享
 
 
 

在 .NET 中使用 WEB SERVICE 的CallBacks机制 (4)

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

测试

WEB SERVICE Callbacks的概念和设计能使用下列位于http://localhost虚拟目录的三个子项目一起测试。测试的概要已经在上述段落中(Web Service Callback部分)说明。所有项目已经加入了跟踪断点,这样我们可以清楚的看到程序运行的过程(http://www.sysinternals.com/)。下面是它们的实现代码:

1. Web Service A

namespace WebServiceA

{

public class ServiceA : System.Web.Services.WebService

{

public ServiceA()

{

//CODEGEN: This call is required by the ASP.NET Web Services Designer

InitializeComponent();

Trace.WriteLine(string.Format("[{0}]ServiceA.ctor", GetHashCode()));

}

#region Component Designer generated code

private void InitializeComponent()

{

}

#endregion

protected override void Dispose( bool disposing )

{

Trace.WriteLine(string.Format("[{0}]ServiceA.Dispose", GetHashCode()));

}

[WebMethod]

public string DoSomeWorkA(int count, string ticket)

{

Trace.WriteLine(string.Format("[{0}]ServiceA.DoSomeWorkA start...",

GetHashCode()));

int startTC = Environment.TickCount;

// current state

Global.state[ticket] = "The job has been started";

string state = ticket;

// location of the source/target web services -

// (hard coded for test purpose only!)

string myWsdlUrl = "http://localhost/WebServiceA/ServiceA.asmx?wsdl";

string targetWsdlUrl = "http://localhost/WebServiceB/ServiceB.asmx?wsdl";

// call the WebService B

WebServiceAccessor wsa = new WebServiceAccessor(targetWsdlUrl);

object esObj = wsa.CreateInstance("ServiceB");

object retval = wsa.Invoke(esObj, "BeginDoSomeWorkB", count,

myWsdlUrl, state, null, null);

// Wait for the call to complete

WebClientAsyncResult ar = retval as WebClientAsyncResult;

ar.AsyncWaitHandle.WaitOne();

// retrieve a result

object result = wsa.Invoke(esObj, "EndDoSomeWorkB", ar);

int durationTC = Environment.TickCount - startTC;

Trace.WriteLine(string.Format("[{0}]ServiceA.DoSomeWorkA done in {1}ms",

GetHashCode(), durationTC));

//

Global.state.Remove(ticket);

return result.ToString();

}

[WebMethod]

public bool CallbackServiceA(string sender, string xmlEventArg)

{

WebServiceEventArgs ea = (WebServiceEventArgs)xmlEventArg;

string msg = string.Format(

"[{0}]ServiceA.CallbackServiceA({1}, [{2},{3},{4}])",

GetHashCode(), sender, ea.name, ea.state, ea.param);

if(Global.state.ContainsKey(ea.state))

{

Global.state[ea.state] = string.Format("{0}, [{1},{2},{3}]",

sender, ea.name, ea.state, ea.param);

Trace.WriteLine(msg);

return true;

}

return false;

}

[WebMethod]

public string AbortWorkA(string ticket)

{

Trace.WriteLine(string.Format("[{0}]ServiceA.AbortWorkA",

GetHashCode()));

if(Global.state.ContainsKey(ticket))

{

Global.state.Remove(ticket);

return string.Format("#{0} aborted.", ticket);

}

return string.Format("#{0} doesn't exist.", ticket);

}

[WebMethod]

public string GetStatusWorkA(string ticket)

{

if(Global.state.ContainsKey(ticket))

{

return string.Format("#{0} status: {1}", ticket, Global.state[ticket]);

}

return string.Format("#{0} doesn't exist.", ticket);

}

}

}

全局类:

namespace WebServiceA

{

public class Global : System.Web.HttpApplication

{

static public Hashtable state = null;

protected void Application_Start(Object sender, EventArgs e)

{

state = Hashtable.Synchronized(new Hashtable());

Trace.WriteLine(string.Format("[{0}]ServiceA.Application_Start",

GetHashCode()));

}

// ...

protected void Application_End(Object sender, EventArgs e)

{

state.Clear();

Trace.WriteLine(string.Format("[{0}]ServiceA.Application_End",

GetHashCode()));

}

}

}

请求和Callback Web Methods运行在不同的Session 中,这是状态为什么保存在可共享的资源类(如Hashtable)中的原因。每个客户端的请求有一个单独的票据凭证,它是WEB SERVICE和全球站点之间的一把通信钥匙(cookie)。

2. Web Service B

这个网络服务非常简单。 只有一个 Web Methods模拟一些工作。 程序运行期间,服务开始启用Callback Web Methods。工作以同步的方式在运行一些循环操作。每个循环调用WebService”A”的Callback Web Methods,基于它的返回值,程序可以自动停止。Service. 可以在session中保持状态。

namespace WebServiceB

{

public class ServiceB : System.Web.Services.WebService

{

public ServiceB()

{

//CODEGEN: This call is required by the ASP.NET Web Services Designer

InitializeComponent();

Trace.WriteLine(string.Format("[{0}]ServiceB.ctor", GetHashCode()));

}

#region Component Designer generated code

private void InitializeComponent()

{

}

#endregion

protected override void Dispose( bool disposing )

{

Trace.WriteLine(string.Format("[{0}]ServiceB.Dispose", GetHashCode()));

}

[WebMethod(EnableSession=true)]

public string DoSomeWorkB(int count, string callbackWS, string stateWS)

{

Trace.WriteLine(string.Format("[{0}]ServiceB.DoSomeWorkB start...",

GetHashCode()));

int startTC = Environment.TickCount;

// async call to the ServiceA.CallbackServiceA method

WebServiceAccessor wsa = new WebServiceAccessor(callbackWS);

object esObj = wsa.CreateInstance("ServiceA");

// prepare the callback arguments: sender, EventArgs

string sender = GetType().FullName;

WebServiceEventArgs ea = new WebServiceEventArgs();

ea.name = "This is a callback";

ea.state = stateWS;

for(int ii = 0; ii < (count & 0xff); ii++) // max. count = 255

{

ea.param = ii;

string xmlEventArgs = ea;

object retval = wsa.Invoke(esObj, "BeginCallbackServiceA",

sender, xmlEventArgs, null, null);

// simulate some task

Thread.Sleep(250);

// Wait for the call to complete

WebClientAsyncResult ar = retval as WebClientAsyncResult;

ar.AsyncWaitHandle.WaitOne();

// result

object result = wsa.Invoke(esObj, "EndCallbackServiceA", ar);

if((bool)result == false)

{

Trace.WriteLine(string.Format(

"[{0}]ServiceB.DoSomeWorkB has been aborted", GetHashCode()));

return string.Format("#{0} aborted during the progress of {1}.",

stateWS, ii);

}

}

//

int durationTC = Environment.TickCount - startTC;

Trace.WriteLine(string.Format("[{0}]ServiceB.DoSomeWorkB done in {1}ms",

GetHashCode(), durationTC));

return string.Format("#{0} done in {1}ms", stateWS, durationTC);

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有