从VB移植的代码中的意外行为.NET转换为c#(未找到类型)
本文关键字:类型 转换 NET 代码 VB 意外 | 更新日期: 2023-09-27 18:15:58
我一直在尝试从我的web服务的URL中删除端口。web服务在多个服务器上负载均衡。我有以下代码在c#生产中正确工作;然而,对于这个特定的项目,我不得不使用VB。我已经转换了它,我没有任何编译错误,但我得到以下运行时错误:
无法解析属性'
type
'的值。无法从程序集"WebService1
"中加载类型"WebServicesSoapPortRemovalReflector
"。
我读到的所有东西都指向这篇文章: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
网络。配置(c# &VB)
<webServices>
<soapExtensionReflectorTypes>
<add type="WebService1.WebServicesSoapPortRemovalReflector, WebService1" />
</soapExtensionReflectorTypes>
</webServices>
在VB中。NET有一点不同。命名空间的前缀是项目属性中定义的根命名空间。如果您还没有注意到,这就是为什么在VB中添加了新类。. NET没有默认的命名空间(试试吧)。
因此,假设根名称空间是WebService1
,您的配置应该是:
<webServices>
<soapExtensionReflectorTypes>
<add type="WebService1.WebService1.WebServicesSoapPortRemovalReflector, WebService1" />
</soapExtensionReflectorTypes>
</webServices>
我认为这更好地解释了行为(也是一个演示)。
在任何情况下,确保检查您自己的项目属性并相应地调整配置。