我一直在尝试从我的 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>