zoukankan      html  css  js  c++  java
  • websevice动态控制访问ip

          一般而言webservice是部署在哪台服务器,然后它的address location就是指向哪个,但是由于有些情况处于各种原因,如网络策略,需要先访问某个ip之后再进行跳转到一个ip,这个时候就需要代码控制websevice指向的IP地址了,就需要用到SoapExtensionReflector类了,重写里面的ReflectDescription方法:如下

     public override void ReflectDescription()
        {
            ServiceDescription description = ReflectionContext.ServiceDescription;
            foreach (Service service in description.Services)
            {
                foreach (Port port in service.Ports)
                {
                    foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
                    {
                        try
                        {
                            SoapAddressBinding binding = extension as SoapAddressBinding;
                            string path = "http://1.1.1.1"; // 需要访问的地址
                        {
                                string url = binding.Location;// 例如:http://localhost:8090/WebService/codes/new
                                //假如你websevice部署再2.2.2.2这台服务器,那么就将这个ip替换为你需要访问的ip,同时你也可以根据binding.Location来判断哪个服务需要替换。
                                 binding.Location = binding.Location.Replace("http://2.2.2.2", path);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
        }

      然后在web.config配置里configuration节点里加上如下节点

      <system.web>
        <webServices>
          <protocols>
            <add name="HttpSoap"/>
          </protocols>
          <soapExtensionReflectorTypes>
            <add type ="类名,该类所在文件夹"/>
          </soapExtensionReflectorTypes>
        </webServices>
      </system.web>

     注意:一但使用,则所有webservice将使用会执行这个程序

  • 相关阅读:
    Linux08:帮助与常用快捷键
    Android : 跟我学Binder --- (5) C++实现
    Linux应用调试 :使用gdb和gdbserver进行远程调试
    Mosquitto-1.5在Linux上的安装以及Android客户端的实现
    MySQL-8.0.15在Win10和Ubuntu上安装&使用
    Android : 跟我学Binder --- (4) 驱动情景分析
    Android : Android Studio 更新至gradle 4.10.1后Variants API变化
    Android : 跟我学Binder --- (3) C程序示例
    Android : 关于HTTPS、TLS/SSL认证以及客户端证书导入方法
    Android : 跟我学Binder --- (2) AIDL分析及手动实现
  • 原文地址:https://www.cnblogs.com/MrHanBlog/p/10530300.html
Copyright © 2011-2022 走看看