如何在WSDL中修改httpAddress? [英] How to modify the httpAddress in WSDL?

查看:97
本文介绍了如何在WSDL中修改httpAddress?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎是这样的问题: ASP.NETWeb服务更改了Invoke上的端口.我尝试使用

the problem almost like this: ASP.NET Web Service changes port on Invoke.I try to use the SoapExtensionReflector to modify the service address in WSDL,and it works on SaopAddress,but the http address is still nochanging. the result like this:

 - <wsdl:service name="WebService1">
  - <wsdl:port name="WebService1Soap" binding="tns:WebService1Soap">
     <soap:address location="http://I can remove this Port:3821/WebService1.asmx" /> 
    </wsdl:port>
  - <wsdl:port name="WebService1Soap12" binding="tns:WebService1Soap12">
     <soap12:address location="http://I can remove this Port:3821/WebService1.asmx" /> 
    </wsdl:port>
  - <wsdl:port name="WebService1HttpGet" binding="tns:WebService1HttpGet">
     <http:address location="http://localhost:3821/WebService1.asmx" /> 
    </wsdl:port>
  - <wsdl:port name="WebService1HttpPost" binding="tns:WebService1HttpPost">
     <http:address location="http://localhost:3821/WebService1.asmx" /> 
    </wsdl:port>
 </wsdl:service>

我们可以看到肥皂的位置已更改,但最后两个http:地址的位置仍未更改.

we can see that the soap location has been changed but the last two http:address location are still Unchanged.

这是我的代码:

  public class OuterPortReflector : SoapExtensionReflector
  {       
    public override void ReflectMethod()
    {            
    }

    public override void ReflectDescription()
    {
        ServiceDescription description = ReflectionContext.ServiceDescription;
        foreach (Service service in description.Services)
        {
            foreach (Port port in service.Ports)
            {
                if (!portsName.ContainsKey(port.Binding.Name))
                {
                    portsName.Add(port.Binding.Name, true);
                }

                foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
                {
                    SoapAddressBinding binding = extension as SoapAddressBinding;
                    if (null != binding)
                    {
                        binding.Location = binding.Location.Replace("http://localhost", "http://I can remove this Port");
                    }
                    else
                    {
                        HttpAddressBinding httpBinding = extension as HttpAddressBinding;
                        if (httpBinding != null)
                        {
                            httpBinding.Location = httpBinding.Location.Replace("http://localhost", "http://I can remove this Port");
                        }
                        else
                        {
                            Soap12AddressBinding soap12Binding = extension as Soap12AddressBinding;
                            if (soap12Binding != null)
                            {
                                soap12Binding.Location = soap12Binding.Location.Replace("http://localhost", "http://I can remove this Port");
                            }
                        }
                    }
                }
            }
        }
    }      
}

如何处理?我的代码有问题吗?

How to deal with this? Is there something wrong with my code?

希望somebady可以给我一些建议或关键字.3Q

hope somebady can give me some suggestion or keywords.3Q

推荐答案

我唯一可以完成的方法是使用

The only way I could accomplish is to use HttpModule. In intercepts every request and changes the response for WSDL.

public class WsdlFixHttpModule : IHttpModule
{
    private static string _webServicesBaseUrl;
    public static WsdlFixHttpModule()
    {
        _webServicesBaseUrl = ConfigurationManager.AppSettings("BaseWebServicesUrl");
    }
    public void Init(HttpApplication context)
    {
        context.EndRequest += (s, e) => OnEndRequest(s, e);
        context.BeginRequest += (s, e) => OnBeginRequest(s, e);
    }
    private void OnBeginRequest(object sender, EventArgs e)
    {
        HttpContext httpContext = HttpContext.Current;
        if ((!string.IsNullOrWhiteSpace(_webServicesBaseUrl) & httpContext.Request.RawUrl.EndsWith("YourService.asmx?wsdl", StringComparison.InvariantCultureIgnoreCase)))
            httpContext.Response.Filter = new StreamWatcher(httpContext.Response.Filter);
    }
    private void OnEndRequest(object sender, EventArgs e)
    {
        HttpContext httpContext = HttpContext.Current;
        if ((!string.IsNullOrWhiteSpace(_webServicesBaseUrl) & httpContext.Request.RawUrl.EndsWith("YourService.asmx?wsdl", StringComparison.InvariantCultureIgnoreCase)))
        {
            string wsdl = httpContext.Response.Filter.ToString();
            wsdl = Regex.Replace(wsdl, @"(?<=address location="")(.*)(?=\/YourService.asmx"")", _webServicesBaseUrl);
            httpContext.Response.Clear();
            httpContext.Response.Write(wsdl);
            httpContext.Response.End();
        }
    }
    public void Dispose()
    {
    }
}

这里是 StreamWatcher ,用于读取默认响应.

Here is StreamWatcher that is used to read default response.

public class StreamWatcher : Stream
{
    private Stream _base;
    private MemoryStream _memoryStream = new MemoryStream();
    public StreamWatcher(Stream stream)
    {
        _base = stream;
    }
    public override void Flush()
    {
        _base.Flush();
    }
    public override int Read(byte[] buffer, int offset, int count)
    {
        return _base.Read(buffer, offset, count);
    }
    public override void Write(byte[] buffer, int offset, int count)
    {
        _memoryStream.Write(buffer, offset, count);
        _base.Write(buffer, offset, count);
    }
    public override string ToString()
    {
        return Encoding.UTF8.GetString(_memoryStream.ToArray());
    }
    public override bool CanRead
    {
        get
        {
            throw new NotImplementedException();
        }
    }
    public override bool CanSeek
    {
        get
        {
            throw new NotImplementedException();
        }
    }
    public override bool CanWrite
    {
        get
        {
            throw new NotImplementedException();
        }
    }
    public override long Seek(long offset, SeekOrigin origin)
    {
        throw new NotImplementedException();
    }
    public override void SetLength(long value)
    {
        throw new NotImplementedException();
    }
    public override long Length
    {
        get
        {
            throw new NotImplementedException();
        }
    }
    public override long Position
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}

并且 HttpModule 需要在web.config中注册

And HttpModule needs to be registered in the web.config

这篇关于如何在WSDL中修改httpAddress?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆