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如下
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")));
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
上述就是通过动态的形式获取得到ProductService了
修改Page.xaml.cs文件如下
void Page_Loaded(object sender,
RoutedEventArgs e)
{
ProductServiceClient client =
ServiceUtil.GetDynamicClient();
//动态获取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;
}
};
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
这样大家就可以在不用修改的情况下非常便捷的将WCF服务配置信息部署到IIS或者Apache上了。