websevice动态控制访问ip
一般而言webservice是部署在哪台服务器,然后它的address location就是指向哪个,但是由于有些情况处于各种原因,如网络策略,需要先访问某个ip之后再进行跳转到一个ip,这个时候就需要代码控制websevice指向的IP地址了,就需要用到SoapExtensionReflector类了,重写里面的ReflectDescription方法:如下
public override void ReflectDescription()
{
ServiceDescription description = ReflectionContext.ServiceDescription;
{
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;
{
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)
{
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将使用会执行这个程序
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库