我是程序猿

注册

 

发新话题 回复该主题

Silverlight 动态配置WebService [复制链接]

1#
Silverlight 动态配置WebService

我们知道,在silverlight中对service(web service,wcf)添加引用后,会产生一个config文件: ServiceReferences.ClientConfig.

如下所示:
  1. <configuration>
  2. <system.serviceModel>
  3. <bindings>
  4. <basicHttpBinding>
  5. <binding name="BasicHttpBinding_Wcf_GetData"
  6. maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
  7. <security mode="None" />
  8. </binding>
  9. </basicHttpBinding>
  10. </bindings>
  11. <client>
  12. <endpoint
  13. address="http://localhost:1522/CH10/Wcf_GetData.svc"
  14. binding="basicHttpBinding"
  15. bindingConfiguration="BasicHttpBinding_Wcf_GetData"
  16. contract="WcfGetDataProxy.Wcf_GetData"
  17. name="BasicHttpBinding_Wcf_GetData" />
  18. </client>
  19. </system.serviceModel>
  20. </configuration>
复制代码
可以看到,这个config中配置了service的一些信息:地址,绑定类型等。这样在实例化服务时候,默认会到此config中读取相关节点信息...
但是这样有几个缺点:
1.本地开发与发布版本时候,需要重新配置文档,并编译才可。【地址位置发生了改变】
2.不安全。client端可以在IE临时文件夹中找到silverlight本地备份,xx.xap文件,可以轻松解包,看到此config文件,并获悉service地址信息....
为了避免这些问题,我们可以利用实例化服务时,服务的几个构造函数的重载函数来实现。 也就是说我们在实例化时,明确指出service的地址等信息,而不是到默认的config中查找。 可以自定义一个class,对config中的节点进行实例化,并返回特定信息,从而可以删除config文件,起到client端安全的作用。
自定义服务类别
  1. public class ServiceUtil {
  2. /// <summary>
  3. /// 调用远端Web service
  4. /// </summary>
  5. /// <returns></returns>
  6. public static ISDApp01Xml.SDApp01XmlSoapClient GetDynamicClient() {
  7. //BasicHttpBinding 定义
  8. BasicHttpBinding binding = new BasicHttpBinding(
  9. Application.Current.Host.Source.Scheme.Equals("https",
  10. StringComparison.InvariantCultureIgnoreCase) ?
  11. BasicHttpSecurityMode.Transport :
  12. BasicHttpSecurityMode.None);
  13. binding.MaxReceivedMessageSize = int.MaxValue;
  14. binding.MaxBufferSize = int.MaxValue;
  15. //Endpoind 定义
  16. EndpointAddress client = new EndpointAddress(GetHostUrl());
  17. //返回web service实例:注意这里利用了服务的构造函数的重载
  18. return new ISDApp01Xml.SDApp01XmlSoapClient(binding,client);
  19. }
  20. /// <summary>
  21. /// 调用本地Web service
  22. /// </summary>
  23. /// <returns></returns>
  24. public static myService.myServiceSoapClient GetmyService() {
  25. BasicHttpBinding binding = new BasicHttpBinding(
  26. Application.Current.Host.Source.Scheme.Equals("https",
  27. StringComparison.InvariantCultureIgnoreCase) ?
  28. BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
  29. binding.MaxReceivedMessageSize = int.MaxValue; binding.MaxBufferSize = int.MaxValue;
  30. //取得web service路径
  31. EndpointAddress client = new EndpointAddress((
  32. new Uri(Application.Current.Host.Source, "../myService.asmx")));
  33. return new myService.myServiceSoapClient(binding, client);
  34. }
  35. public static string GetHostUrl() {
  36. //指定web service路径
  37. return "http://webtest/WebServices/myService.asmx";
  38. }
  39. }
  40. silverlight调用:
  41. public partial class Demo012 : UserControl {
  42. public Demo012() {
  43. InitializeComponent();
  44. setBind();
  45. }
  46. private void setBind()
  47. {
  48. myService.myServiceSoapClient client= ServiceUtil.GetDynamicClient();
  49. string sql = "SELECT ID,NAME FROM PDM_BASIC_INFO";
  50. client.ExecuteQueryAsync(sql);
  51. client.ExecuteQueryCompleted += (sender2, e2) => {
  52. if (e2.Error == null) {
  53. dgshow.ItemsSource = from item in e2.Result.Descendants("row") select new BasicInfo {
  54. ID = (int)item.Attribute("ID"), Name = (string)item.Attribute("NAME") };
  55. }
  56. };
  57. myService.myServiceSoapClient wc = ServiceUtil.GetmyService();
  58. wc.sayHelloAsync();
  59. wc.sayHelloCompleted += (sender1, e1) => { MessageBox.Show(e1.Result); }; } }
  60. public class BasicInfo {
  61. public int ID { set; get; }
  62. public string Name { set; get; }
  63. }
复制代码
分享 转发
TOP
发新话题 回复该主题