zoukankan      html  css  js  c++  java
  • Silverlight 动态调用 WCF And WebService

    1. 配置 IIS 绑定 IP地址

    2. 在 SL 中引用 WebService

    3. 在需要调用 WebService 的地方写下列代码:

    image

    WCF :


    1BasicHttpBinding basicBinding =new BasicHttpBinding();
    2
    3CustomBinding binding =new CustomBinding(basicBinding);
    4
    5BindingElement binaryElement =new BinaryMessageEncodingBindingElement();
    6
    7// 删除原来 Elements 集合内的 TextMessageEncodingBindingElement
    8
    9binding.Elements.Remove(binding.Elements[0]);
    10
    11// 添加 BinaryMessageEncodingBindingElement
    12
    13binding.Elements.Insert(0, binaryElement);
    14
    15// wcf 地址
    16
    17EndpointAddress endPoint =new EndpointAddress("http://172.168.1.100/DynamicInvokeWCF.Web/DynWCF.svc");
    18
    19// 创建 wcf 客户端
    20
    21DynWCFClient client = (DynWCFClient)Activator.CreateInstance(typeof(DynWCFClient), binding, endPoint);
    22
    23client.DoWorkCompleted +=new EventHandler<DoWorkCompletedEventArgs>(client_DoWorkCompleted);
    24
    25client.DoWorkAsync();
    26
    27

    传统 WebService:


    1BasicHttpBinding basicBinding =new BasicHttpBinding();
    2
    3CustomBinding binding =new CustomBinding(basicBinding);
    4
    5BindingElement binaryElement =new BinaryMessageEncodingBindingElement();
    6
    7EndpointAddress endPoint =new EndpointAddress("http://172.168.1.100/DynamicInvokeWCF.Web/Asmx.asmx");
    8
    9AsmxSoapClient client = (AsmxSoapClient)Activator.CreateInstance(typeof(AsmxSoapClient), binding, endPoint);
    10
    11client.HelloWorldCompleted +=new EventHandler<HelloWorldCompletedEventArgs>(client_HelloWorldCompleted);
    12
    13client.HelloWorldAsync();
    14
    15

    这样就可以动态的调用 webservice 了. 完全不需要 ServiceReferences.ClientConfig 文件了.

  • 相关阅读:
    Java 面向对象(十二)类的成员 之 代码块
    Java 关键字:static
    Java 常用类(二):包装类(Wrapper)
    Java 之 clone 方法(对象拷贝)
    SQL分组聚合查询
    Rabbitmq消息持久化
    idea 插件
    TCP粘包,拆包及解决方法
    redis内存淘汰策略及如何配置
    MySQL存储过程/存储过程与自定义函数的区别
  • 原文地址:https://www.cnblogs.com/cuihongyu3503319/p/2241290.html
Copyright © 2011-2022 走看看