設(shè)置WCF服務(wù)配置信息相關(guān)經(jīng)驗分享
WCF服務(wù)配置信息中有許多東西需要我們?nèi)ミM行適當(dāng)?shù)男薷幕蛘咴O(shè)置,才能實現(xiàn)一些功能。在這里我們將會了解到有關(guān)WCF服務(wù)配置信息的一些動態(tài)設(shè)置方法。#t#
在Silverlight中是使用ServiceReferences.ClientConfig文件來保存和查看WCF服務(wù)配置信息的,而ServiceReferences.ClientConfig又是包含在.xap文件中的。
這樣就導(dǎo)致如果您的Silverlight工程有用到WCF服務(wù)就需要在每次部署到不同網(wǎng)站的時候重新更改下WCF的配置并重新編譯。而且這個重新配置的過程又往往可能需要Visual Studio 2008的幫助來重新鏈接WCF服務(wù)。
而且對于有些部署的服務(wù)器就可能非常不現(xiàn)實了(有的服務(wù)器要求系統(tǒng)干凈,不允許安裝其他軟件)。
那么怎么辦呢?
WCF服務(wù)配置信息解決方案:
部署時由于WCF Service的部署地址不同,將需要我們重新索引,并編譯這個程序,非常繁瑣
你可以采用如下的動態(tài)配置的方式一舉解決這個問題:
刪除ServiceReferences.ClientConfig文件,并在Silverlight 工程下添加一個類文件(Class File)ServiceUtil.cs如下
- public static ProductServiceClient GetDynamicClient()
- {
- BasicHttpBinding binding = new BasicHttpBinding(
- Application.Current.Host.Source.Scheme.
Equals("https", StringComparison.
InvariantCultureIgnoreCase)- ? BasicHttpSecurityMode.Transport :
BasicHttpSecurityMode.None);- binding.MaxReceivedMessageSize = int.MaxValue;
- binding.MaxBufferSize = int.MaxValue;
- return new ProductServiceClient(binding,
new EndpointAddress(- new Uri(Application.Current.Host.Source,
"../ProductService.svc")));- }
上述就是通過動態(tài)的形式獲取得到ProductService了
修改Page.xaml.cs文件如下
- void Page_Loaded(object sender,
RoutedEventArgs e)- {
- ProductServiceClient client =
ServiceUtil.GetDynamicClient();
//動態(tài)獲取ProductServiceClient- this.Cursor = Cursors.Hand;
- client.RetreiveDataAsync();
- client.RetreiveDataCompleted +=
(sender2, e2) =>- {
- if (e2.Cancelled == false &&
e2.Error == null)- {
- ObservableCollection<ProductInfo>
products = e2.Result;- this.ProductLBCtl.ItemsSource = products;
- this.Cursor = Cursors.Arrow;
- }
- };
- }
這樣大家就可以在不用修改的情況下非常便捷的將WCF服務(wù)配置信息部署到IIS或者Apache上了。