WebService asmx生成的wsdl 修改 location
C#中使用webservice接口的时候,返给服务器的IP地址是带上了端口号的。但是有时候不能要那个端口(比如用nginx做了转发),就需要在服务端处理一下(处理内容就是后面的代码)。此外,需要在配置文件中web.config中的system.web中添加一些东西:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
<add name="HttpSoap"/>
</protocols>
<soapExtensionReflectorTypes>
<add type="CommMethod.OuterPortReflector,CommMethod"/>
</soapExtensionReflectorTypes>
注:1.type内容是类名,包名。这一点和msdn上的那个人写的有些不同(有可能是版本的问题)(http://blogs.msdn.com/b/kaevans/archive/2005/11/16/493496.aspx)。
2.如果没有protocols中的内容的话,有可能post和get请求不能被正确识别(未做过验证,只是在博客园上看见过类似问题)。
3.stackoverflow源地址:http://stackoverflow.com/questions/1531448/asp-net-web-service-changes-port-on-invoke
代码:
//-----------------------------------------------------------------------
// <copyright file="OuterPortReflector.cs" company="">
// * Copyright (C)
// * version :
// * author : ying83811
// * FileName: OuterPortReflector.cs
// * history : created by ying83811
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Description;
namespace Better517Na.KoreaProductInterface.CommMethod
{
/// <summary>
/// OuterPortReflector类
/// </summary>
public class OuterPortReflector : SoapExtensionReflector
{
/// <summary>
/// 重写ReflectMethod
/// </summary>
public override void ReflectMethod()
{
}
/// <summary>
/// 重写ReflectDescription
/// </summary>
public override void ReflectDescription()
{
// 为了说明问题,这里直接把端口号写死了,建议写在配置文件中
string portNum = "8888";
portNum = ":" + portNum;
ServiceDescription description = ReflectionContext.ServiceDescription;
foreach (Service service in description.Services)
{
foreach (Port port in service.Ports)
{
foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
{
SoapAddressBinding binding = extension as SoapAddressBinding;
if (null != binding)
{
binding.Location = binding.Location.Replace(portNum, string.Empty);
}
else
{
HttpAddressBinding httpBinding = extension as HttpAddressBinding;
if (httpBinding != null)
{
binding.Location = "https://baidu.com";
httpBinding.Location = httpBinding.Location.Replace(portNum, string.Empty);
}
else
{
binding.Location = "https://baidu.com";
Soap12AddressBinding soap12Binding = extension as Soap12AddressBinding;
if (soap12Binding != null)
{
soap12Binding.Location = soap12Binding.Location.Replace(portNum, string.Empty);
}
}
}
}
}
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2014-08-19 group by 汇总