WCF行为控制代码示例应用解读

WCF开发工具中有很多比较重要的应用技术及供功能特点需要我们熟练应用。在这里我们将会为大家详细介绍一下有关WCF行为控制的相关内容。希望大家可以从这里介绍的内容中获得相关帮助。#t#

在嘉定等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、成都网站建设 网站设计制作按需求定制网站,公司网站建设,企业网站建设,品牌网站制作,全网整合营销推广,成都外贸网站制作,嘉定网站建设费用合理。

在完成服务契约设计和服务实现后,我们可以设置该服务的运行期行为(Behavior)。这些WCF行为控制包括 Service Behaviors、Endpoint Behaviors、Contract Behaviors、Operation Behaviors。

有关所有行为的说明,可以查看 ms-help://MS.MSSDK.1033/MS.NETFX30SDK.1033/WCF_con/html/5c5450ea-6af1-4b75-a267-613d0ac54707.htm。

以下就常用的行为使用,做些演示。

ServiceBehaviorAttribute & OperationBehaviorAttribute

这是两个最常用的WCF行为控制特性,可用于控制:

服务对象生命周期。

并发管理。

异步通讯。

配置文件参数。

事务。

元数据转换。

会话(Session)周期。

等等...

 
 
 
  1. [ServiceContract]
  2. public interface ICalculate
  3. {
  4. [OperationContract]
  5. int Add(int a, int b);
  6. }
  7. [ServiceBehavior(InstanceContextModeInstanceContextMode=
    InstanceContextMode.PerCall)]
  8. public class CalculateService : ICalculate
  9. {
  10. public int Add(int a, int b)
  11. {
  12. Console.WriteLine(this.GetHashCode());
  13. return a + b;
  14. }
  15. }
  16. public class WcfTest
  17. {
  18. public static void Test()
  19. {
  20. AppDomain.CreateDomain("Server").DoCallBack(delegate
  21. {
  22. ServiceHost host = new ServiceHost(typeof(CalculateService));
  23. host.AddServiceEndpoint(typeof(ICalculate), new WSHttpBinding(), 
    "http://localhost:8080/calc");
  24. host.Open();
  25. });
  26. ChannelFactory factory = new ChannelFactory
    (new WSHttpBinding(), 
  27. "http://localhost:8080/calc");
  28. ICalculate o = factory.CreateChannel();
  29. Console.WriteLine(o.Add(1, 2));
  30. Console.WriteLine(o.Add(1, 2));
  31. factory.Close();
  32. }
  33. }

输出:

 
 
 
  1. 30136159
  2. 3
  3. 41153804
  4. 3
  5. ServiceMetadataBehavior

用于开启元数据获取功能。只有使用该WCF行为控制,客户端才能通过 Svcutil.exe 或其他工具获取服务信息,进而生成客户端代理文件。

 
 
 
  1. ServiceHost host = new ServiceHost(typeof(CalculateService));
  2. host.AddServiceEndpoint(typeof(ICalculate), 
    new BasicHttpBinding(), "http://localhost:8080/calc");
  3. ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
  4. behavior.HttpGetEnabled = true;
  5. behavior.HttpGetUrl = new Uri("http://localhost:8080/calc");
  6. host.Description.Behaviors.Add(behavior);
  7. host.Open();
  8. ServiceDebugBehavior

开启调试功能,如将服务器端的异常信息直接传送给客户端。

 
 
 
  1. ServiceHost host = new ServiceHost(typeof(CalculateService));
  2. host.AddServiceEndpoint(typeof(ICalculate), 
    new WSHttpBinding(), "http://localhost:8080/calc");
  3. host.Description.Behaviors.Find()
    .IncludeExceptionDetailInFaults = true;
  4. host.Open();

以上就是对WCF行为控制的相关介绍。

网站标题:WCF行为控制代码示例应用解读
网页地址:http://www.shufengxianlan.com/qtweb/news17/406617.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联