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>

</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)
                            {

   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);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM