WCF:异步调用长时间工作的服务
2009-12-11 21:15
537 查看
今天继续在讲解WCF方面的课程。关于长时间工作的服务,实现异步的方式有很多种,例如新开一个Thread去调用,或者采用添加引用后生成的Beginxxxx方法去做。本文介绍一种比较特殊的写法:直接通过回调委托实现。
1. 合约
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2. 服务
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
3. 宿主
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
4. 客户端
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1. 合约
using System.ServiceModel; namespace Contracts { [ServiceContract] public interface ILongworker { [OperationContract] //[OperationContract(IsOneWay=true)] //将该操作设置为IsOneWay,表示该操作不需要回复。客户端调用之后无需等待即可返回 //该模式下即便服务器端抛出异常也不会被客户端接收到。 void Dowork(); } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2. 服务
using System; namespace Services { public class LongworkService:Contracts.ILongworker { #region ILongworker 成员 public void Dowork() { //这是一个长时间工作的服务方法 System.Threading.Thread.Sleep(10000);//设置休眠10秒钟 Console.WriteLine("服务器执行完了操作"); } #endregion } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
3. 宿主
using System; using System.ServiceModel; using System.ServiceModel.Description; namespace LongworkServiceHost { class Program { static void Main(string[] args) { #region 长时间工作的服务 using (ServiceHost host = new ServiceHost( typeof(Services.LongworkService), new Uri("http://localhost:8000/LongworkService") )) { host.AddServiceEndpoint( "Contracts.ILongworker", new BasicHttpBinding(), ""); ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; host.Description.Behaviors.Add(behavior); host.AddServiceEndpoint( "IMetadataExchange", MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); host.Open(); Console.WriteLine("服务器已经准备好"); Console.Read(); } #endregion } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
4. 客户端
Contracts.ILongworker proxy = new ChannelFactory<Contracts.ILongworker>( new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/LongworkService")).CreateChannel(); Action action = new Action(() => { proxy.Dowork(); });//这一句必须这样写,而不能写成new Action(proxy.Dowork),那样实现不了异步效果 Console.WriteLine("异步调用之前的工作"); action.BeginInvoke((a) => { Console.WriteLine("调用结束"); }, null); Console.WriteLine("主程序继续其他的工作");
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- WCF:异步调用长时间工作的服务
- SilverLight下用WebRequest异步调用WCF服务
- .NET:异步调用长时间工作的方法(采用lambda表达式封装回调函数)
- WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
- WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
- 【转载】WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
- 异步调用Restful的WCF服务
- .NET:异步调用长时间工作的方法(采用lambda表达式封装回调函数)
- WCF热门问题编程示例(4):WCF客户端如何异步调用WCF服务?
- wcf异步服务调用参考地址
- WCF初探-11:WCF客户端异步调用服务
- 多线程编程学习笔记——异步调用WCF服务
- WCF系列教程之客户端异步调用服务
- Silverlight中异步调用WCF服务,传入回调函数
- iPhone调用WCF服务
- WCF初探-10:WCF客户端调用服务
- asp调用wcf服务、webservice
- WCF服务开发与调用的完整示例
- [转]实例:在Android调用WCF服务
- jquery 直接调用 wcf,面向服务的SOA架构 ( 第二天)