1

我一直在尝试从我的 Web 服务的 URL 中删除一个端口。Web 服务在多个服务器之间实现负载平衡。我有以下代码在 C# 生产中正常工作;但是,对于这个特定的项目,我必须使用 VB。我已对其进行了转换,并且没有任何编译错误,但出现以下运行时错误:

type无法解析属性“ ”的值。错误是:无法WebServicesSoapPortRemovalReflector从程序集“ ”加载类型“ WebService1”。

我读到的所有内容都参考了这篇文章:http: //blogs.msdn.com/b/kaevans/archive/2005/11/16/493496.aspx

C#(这有效,没有错误。)

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services.Description;
using System.Web;

namespace Webservice1
{
    /// <summary>
    /// Taken from http://blogs.msdn.com/b/kaevans/archive/2005/11/16/493496.aspx
    /// </summary>
    /// <remarks></remarks>
    public class WebServicesSoapPortRemovalReflector : SoapExtensionReflector
    {

        public override void ReflectMethod() { }

        public override void ReflectDescription()
        {
            if (bool.Parse(ConfigurationManager.AppSettings["EnableWebServicePortRemoval"]))
            {
                string portToRemove = string.Format(":{0}", ConfigurationManager.AppSettings["WebServicePortToRemove"]);
                ServiceDescription description = ReflectionContext.ServiceDescription;
                foreach (Service service in description.Services)
                {
                    foreach (Port port in service.Ports)
                    {
                        foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
                        {
                            if (extension is SoapAddressBinding)
                            {
                                SoapAddressBinding binding = (SoapAddressBinding)extension;
                                if (binding != null)
                                    binding.Location = binding.Location.Replace(portToRemove, "");
                            }
                            else if (extension is Soap12AddressBinding)
                            {
                                Soap12AddressBinding binding = (Soap12AddressBinding)extension;
                                if (binding != null)
                                    binding.Location = binding.Location.Replace(portToRemove, "");
                            }
                            else if (extension is HttpAddressBinding)
                            {
                                HttpAddressBinding binding = (HttpAddressBinding)extension;
                                if (binding != null)
                                    binding.Location = binding.Location.Replace(portToRemove, "");
                            }
                        }
                    }
                }
            }
        }
    }
}

VB(接收运行时错误)

Imports System.Configuration
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web.Services.Description
Imports System.Web

Namespace WebService1
    ''' <summary>
    ''' Taken from http://blogs.msdn.com/b/kaevans/archive/2005/11/16/493496.aspx
    ''' </summary>
    ''' <remarks></remarks>
    Public Class WebServicesSoapPortRemovalReflector
        Inherits SoapExtensionReflector

        Public Overrides Sub ReflectMethod()
        End Sub

        Public Overrides Sub ReflectDescription()
            If Boolean.Parse(ConfigurationManager.AppSettings("EnableWebServicePortRemoval")) Then
                Dim portToRemove As String = String.Format(":{0}", ConfigurationManager.AppSettings("WebServicePortToRemove"))
                Dim description As ServiceDescription = ReflectionContext.ServiceDescription
                For Each service As Service In description.Services
                    For Each port As Port In service.Ports
                        For Each extension As ServiceDescriptionFormatExtension In port.Extensions
                            If TypeOf extension Is SoapAddressBinding Then
                                Dim binding As SoapAddressBinding = DirectCast(extension, SoapAddressBinding)
                                If binding IsNot Nothing Then
                                    binding.Location = binding.Location.Replace(portToRemove, "")
                                End If
                            ElseIf TypeOf extension Is Soap12AddressBinding Then
                                Dim binding As Soap12AddressBinding = DirectCast(extension, Soap12AddressBinding)
                                If binding IsNot Nothing Then
                                    binding.Location = binding.Location.Replace(portToRemove, "")
                                End If
                            ElseIf TypeOf extension Is HttpAddressBinding Then
                                Dim binding As HttpAddressBinding = DirectCast(extension, HttpAddressBinding)
                                If binding IsNot Nothing Then
                                    binding.Location = binding.Location.Replace(portToRemove, "")
                                End If
                            End If
                        Next
                    Next
                Next
            End If
        End Sub

    End Class
End Namespace

Web.config(C# 和 VB 相同)

<webServices>
    <soapExtensionReflectorTypes>
      <add type="WebService1.WebServicesSoapPortRemovalReflector, WebService1" />
    </soapExtensionReflectorTypes>
</webServices>
4

1 回答 1

1

在 VB.NET 中它有点不同。命名空间以项目属性中定义的根命名空间为前缀。这就是为什么,如果您还没有注意到,VB.NET 中新添加的类没有默认命名空间(试一试)。

所以,假设根命名空间是WebService1,你的配置应该是:

<webServices>
    <soapExtensionReflectorTypes>
      <add type="WebService1.WebService1.WebServicesSoapPortRemovalReflector, WebService1" />
    </soapExtensionReflectorTypes>
</webServices>

我认为这更好地解释了这种行为(也是一个演示)。

在任何情况下,请确保检查您自己的项目属性并相应地调整配置。

于 2013-05-28T19:51:20.243 回答