在 .NET 中使用 WEB SERVICE 的CallBacks机制 (4)
来源:互联网 发布:网络协议内容 编辑:程序博客网 时间:2024/05/21 15:04
测试
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);
}
}
}
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (4)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (4)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (1)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (2)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (3)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (5)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (1)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (5)
- .NET中Web Service的异常机制
- Using the Web Service Callbacks in the .NET Application
- 演练:在 Visual Web Developer 中创建和使用 ASP.NET Web 服务------Web Service
- 在.NET中建立Web service安全措施
- .NET Framework中使用XML Web Service
- ASP.NET AJAX中使用Web Service
- 使用web service在实施中遇到的 问题
- 在Web Service中使用SPS Object 的相关问题?
- .Net web service studio的使用
- 在 Microsoft.NET Framework 精简版中使用 XML Web Service
- 让JBuilder眩出个性
- .NET中加密和解密的实现方法
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (1)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (2)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (3)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (4)
- 在 .NET 中使用 WEB SERVICE 的CallBacks机制 (5)
- 如何使用vs.net生成方法的小技巧
- 如何在.NET中实现事务(1)
- 如何在C#中使用 Win32和其他库
- c#中分割字符串的几种方法
- 用C#创建COM对象
- 如何在.NET中实现事务(2)
- 用Visual C#编写屏幕保护程序