websevice动态控制访问ip

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

 

 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将使用会执行这个程序ide

相关文章
相关标签/搜索