WebService端口号修改

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>

</webServices>

注: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)
                            {
                                httpBinding.Location = httpBinding.Location.Replace(portNum, string.Empty);
                            }
                            else
                            {
                                Soap12AddressBinding soap12Binding = extension as Soap12AddressBinding;
                                if (soap12Binding != null)
                                {
                                    soap12Binding.Location = soap12Binding.Location.Replace(portNum, string.Empty);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在创建WebService端口转发,可以使用上一步生成的接口类(ITestBinding)中的参数对象作为WebService接口的入参和返回。首先,需要创建一个WebService实例,并设置其Url属性为目标接口的地址。例如,假设目标接口地址为"http://xxxx/testwebservice.asmx",可以使用以下代码创建WebService实例并设置Url属性: ``` Wis.WebService wisbll = new Wis.WebService(); string url = "http://xxxx/testwebservice.asmx"; wisbll.Url = url; ``` 接下来,可以使用该WebService实例来进行端口转发的操作,传递参数对象作为接口的入参,并获取返回结果。具体的端口转发操作可以根据具体需求进行编写。需要注意的是,接口地址应该是"http://xxxx/testwebservice.asmx"的形式,不要在末尾加上"?wsdl",否则会返回404错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [WebService转发的实现及使用Postman调试](https://blog.csdn.net/yinshengchen/article/details/116760097)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [.NET WebService 外网端口映射访问 端口跳转问题](https://blog.csdn.net/m0_65407682/article/details/130307103)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值