设置WCF服务配置信息相关经验分享

开发 开发工具
当我们在用到WCF服务的时候,需要配置WCF服务配置信息,如果每次都这样做一遍,非常浪费时间和繁琐。那么有没有一种动态配置WCF服务配置信息的呢?

WCF服务配置信息中有许多东西需要我们去进行适当的修改或者设置,才能实现一些功能。在这里我们将会了解到有关WCF服务配置信息的一些动态设置方法。#t#

在Silverlight中是使用ServiceReferences.ClientConfig文件来保存和查看WCF服务配置信息的,而ServiceReferences.ClientConfig又是包含在.xap文件中的。

这样就导致如果您的Silverlight工程有用到WCF服务就需要在每次部署到不同网站的时候重新更改下WCF的配置并重新编译。而且这个重新配置的过程又往往可能需要Visual Studio 2008的帮助来重新链接WCF服务。

而且对于有些部署的服务器就可能非常不现实了(有的服务器要求系统干净,不允许安装其他软件)。

那么怎么办呢?

WCF服务配置信息解决方案:

部署时由于WCF Service的部署地址不同,将需要我们重新索引,并编译这个程序,非常繁琐

你可以采用如下的动态配置的方式一举解决这个问题:

 

 

 

删除ServiceReferences.ClientConfig文件,并在Silverlight 工程下添加一个类文件(Class File)ServiceUtil.cs如下

 

  1. public static ProductServiceClient GetDynamicClient()  
  2. {  
  3. BasicHttpBinding binding = new BasicHttpBinding(  
  4. Application.Current.Host.Source.Scheme.
    Equals("https", StringComparison.
    InvariantCultureIgnoreCase)  
  5. ? BasicHttpSecurityMode.Transport : 
    BasicHttpSecurityMode.None);  
  6. binding.MaxReceivedMessageSize = int.MaxValue;  
  7. binding.MaxBufferSize = int.MaxValue;  
  8. return new ProductServiceClient(binding,
     new EndpointAddress(  
  9. new Uri(Application.Current.Host.Source,
     "../ProductService.svc")));  

 

上述就是通过动态的形式获取得到ProductService了

修改Page.xaml.cs文件如下

 

  1. void Page_Loaded(object sender,
     RoutedEventArgs e)  
  2. {  
  3. ProductServiceClient client = 
    ServiceUtil.GetDynamicClient();
    //动态获取ProductServiceClient  
  4. this.Cursor = Cursors.Hand;  
  5. client.RetreiveDataAsync();  
  6. client.RetreiveDataCompleted +=
     (sender2, e2) =
    > 
  7. {  
  8. if (e2.Cancelled == false && 
    e2.Error == null)  
  9. {  
  10. ObservableCollection<ProductInfo> 
    products = e2.Result;  
  11. this.ProductLBCtl.ItemsSource = products;  
  12. this.Cursor = Cursors.Arrow;  
  13. }  
  14. };  

 

这样大家就可以在不用修改的情况下非常便捷的将WCF服务配置信息部署到IIS或者Apache上了。

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-22 16:03:03

WCF异常

2009-12-22 13:48:09

引用WCF服务

2009-12-07 15:02:46

WCF学习

2009-12-22 19:26:51

WCF绑定

2010-02-22 17:58:06

WCF异步上传

2011-05-16 09:30:30

jQueryWCF

2009-12-22 18:18:11

WCF客户端编程

2010-02-24 11:22:04

WCF方法重载

2010-02-22 17:43:19

WCF服务启动

2010-02-25 13:54:48

WCF安全参数

2009-11-05 15:50:25

WCF behavio

2010-02-22 11:10:17

WCF获取客户端IP

2009-12-25 09:44:52

WPF窗口设置

2017-07-27 17:37:44

MySQL死锁日志

2010-02-25 13:40:17

WCF禁用安全配置

2010-02-26 14:39:27

WCF服务寄宿

2010-02-26 16:05:14

寄宿WCF服务

2009-12-21 16:37:41

WCF获取服务元数据

2010-02-24 09:28:37

WCF安全配置

2009-12-21 11:19:50

WCF配置文件
点赞
收藏

51CTO技术栈公众号