检查是否有任何 class 具有在组件中定义的自定义属性,其中自定义属性和 class 都是动态加载的
checking if any class has a custom attribute defined in assemby where both custom attribute and class are loaded dynamically
我在 wcf 服务中工作,其中服务动态地托管在随机端口上。服务契约和服务行为组件是动态加载的,所有类型都被扫描以匹配服务名称及其版本。
相同的服务可以 运行ning 在不同的 versions.To 上区分服务的版本我们已经创建了一个自定义的 ServiceIdentifierAttribute 属性。
public class ServiceIdentifierAttribute : Attribute
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _version;
public string Version
{
get { return _version; }
set { _version = value; }
}
}
服务合同及其行为 class 用 SerivceIdentifierAttribute 修饰。
[ServiceContract(Name = "ABCServicesV0.0.0.0")]
[ServiceIdentifierAttribute(Name = "ABCServicesV0.0.0.0", Version = "V0.0.0.0")]
public interface IABCService
{
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, Name = "ABCServicesV0.0.0.0")]
public class ABCService : IABCService
{}
服务合同、属性在一个程序集中定义,服务实现在另一个程序集中定义。我们有一个 GenericSericeHost 控制台应用程序,它通过加载两个程序集来动态托管服务。我们需要搜索所有类型并从程序集中获取服务合同类型。
private static bool SeachForServiceContractAttribute(Type type, String serviceIdentifier)
{
if (type.IsDefined(typeof(ServiceContractAttribute), false))
{
var attributeTypes = type.GetCustomAttributes();
foreach (var attributeType in attributeTypes)
{
try
{
ServiceContractAttribute attribute = (ServiceContractAttribute)attributeType;
if (attribute != null && !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Equals(serviceIdentifier))
return true;
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
}
return false;
}
GenericServiceHost 引用了 ServiceContract 程序集。在运行时间
ServiceContractAttribute attribute = (ServiceContractAttribute)attributeType;
正在抛出错误 Invalid cast exception。由于在 运行 时间加载了两个版本的 ServiceContractAttribute。一个是动态加载的,另一个是通过 GenerciServiceHost 引用加载的。我们无法删除服务引用,因为它会导致 ServiceContractAttribute not defined complication 错误。
所有不同的服务实现都将具有不同的程序集,我们不想添加对来自 genereicservicehost 的所有程序集的引用,因为它会在任何服务行为发生变化时导致重建 genericservicehost。我们希望 GenericServiceHost 始终 运行。
我们如何通过从程序集加载类型转换为程序集加载类型来使其工作
ServiceContractAttribute attribute = (ServiceContractAttribute)attributeType;
有指针吗?
您的架构存在缺陷。看起来你已经多次声明 ServiceContractAttribute
,然后是的,演员表不会 ever 工作,因为每个声明都会产生一个 distinct类型。
您必须将 ServiceContractAttribute
分解为一个单独的程序集,定义主机应用程序和服务程序集之间的公共 API,并在所有服务之间共享它。
我在 wcf 服务中工作,其中服务动态地托管在随机端口上。服务契约和服务行为组件是动态加载的,所有类型都被扫描以匹配服务名称及其版本。 相同的服务可以 运行ning 在不同的 versions.To 上区分服务的版本我们已经创建了一个自定义的 ServiceIdentifierAttribute 属性。
public class ServiceIdentifierAttribute : Attribute
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _version;
public string Version
{
get { return _version; }
set { _version = value; }
}
}
服务合同及其行为 class 用 SerivceIdentifierAttribute 修饰。
[ServiceContract(Name = "ABCServicesV0.0.0.0")]
[ServiceIdentifierAttribute(Name = "ABCServicesV0.0.0.0", Version = "V0.0.0.0")]
public interface IABCService
{
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, Name = "ABCServicesV0.0.0.0")]
public class ABCService : IABCService
{}
服务合同、属性在一个程序集中定义,服务实现在另一个程序集中定义。我们有一个 GenericSericeHost 控制台应用程序,它通过加载两个程序集来动态托管服务。我们需要搜索所有类型并从程序集中获取服务合同类型。
private static bool SeachForServiceContractAttribute(Type type, String serviceIdentifier)
{
if (type.IsDefined(typeof(ServiceContractAttribute), false))
{
var attributeTypes = type.GetCustomAttributes();
foreach (var attributeType in attributeTypes)
{
try
{
ServiceContractAttribute attribute = (ServiceContractAttribute)attributeType;
if (attribute != null && !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Equals(serviceIdentifier))
return true;
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
}
return false;
}
GenericServiceHost 引用了 ServiceContract 程序集。在运行时间
ServiceContractAttribute attribute = (ServiceContractAttribute)attributeType;
正在抛出错误 Invalid cast exception。由于在 运行 时间加载了两个版本的 ServiceContractAttribute。一个是动态加载的,另一个是通过 GenerciServiceHost 引用加载的。我们无法删除服务引用,因为它会导致 ServiceContractAttribute not defined complication 错误。
所有不同的服务实现都将具有不同的程序集,我们不想添加对来自 genereicservicehost 的所有程序集的引用,因为它会在任何服务行为发生变化时导致重建 genericservicehost。我们希望 GenericServiceHost 始终 运行。
我们如何通过从程序集加载类型转换为程序集加载类型来使其工作
ServiceContractAttribute attribute = (ServiceContractAttribute)attributeType;
有指针吗?
您的架构存在缺陷。看起来你已经多次声明 ServiceContractAttribute
,然后是的,演员表不会 ever 工作,因为每个声明都会产生一个 distinct类型。
您必须将 ServiceContractAttribute
分解为一个单独的程序集,定义主机应用程序和服务程序集之间的公共 API,并在所有服务之间共享它。