我试图在不引用它的情况下使用库(准确地说是 RabbitMQ.Client),而不是通过引用引用 RabbitMQ 的库。我希望这不会令人困惑。顺便说一句,如果你觉得这很奇怪,我背后的原因是我想拥有可以使用 SimpleInjector 动态切换的消息队列库。我的问题是,每次我使用一个函数时,它都会在我的引用上抛出一个 FileNotFound 异常。这是一个示例代码:
依赖解析器
参考:SimpleInjector
public class DependencyResolver
{
public static GetImplementation(string location, Type service)
{
Container container = new Container();
System.Reflection.Assembly thisAsm = System.Reflection.Assembly.LoadFile(location);
var qry = from type in thisAsm.GetTypes()
where service.IsAssignableFrom(type)
&& type.Name == dependency.Name
select new
{
Service = type.GetInterfaces().FirstOrDefault(),
Implementation = type
};
if (qry.Count() > 0)
{
container.Register(qry.FirstOrDefault().Service,qry.FirstOrDefault().Implementation, Lifestyle.Transient);
var obj = this.Container.GetInstance(serviceType);
return obj.GetType();
}
return null;
}
}
消息Q.dll
public interface IMessageQ
{
string ConnectionString {get;set;}
void Connect();
void Send(string msg);
}
Rabbit-Style-MQ.dll
参考:RabbitMQ.Client.dll、MessageQ.dll
public class RabbitMQStyle:IMessageQ
{
public string ConnectionString {get;set;}
private QueueingBasicConsumer Consumer { get; set; }
private IModel Channel { get; set; }
private IConnection Connection { get; set; }
private ConnectionFactory Factory { get; set; }
public void Connect()
{
this.Factory = new ConnectionFactory();
this.Factory.Uri = this.ConnectionString;
this.Connection = this.Factory.CreateConnection();
this.Channel = Connection.CreateModel();
this.Channel.QueueDeclare(this.QueueName, true, false, false, null);
}
public void Send(string msg)
{
}
}
申请代码
参考:MessageQ.dll、DependencyResolver.dll;
static void Main(string[] args)
{
var mq = (IMessageQ)Activator.CreateInstance(DependencyResolver.GetImplementation("E:\Rabbit-Style-MQ.dll",typeof(IMessageQ)));
//Type of 'mq' is RabbitMQStyle =)
mq.Connect();//Here is the part where my application threw an exception
}
如果我在我的应用程序上引用 RabbitMQ.Client 一切正常,但我不想这样做,我只想在我的 Rabbit-Style-Mq.dll 上引用它。提前致谢。
Exception: Could not load file or assembly 'RabbitMQ.Client, Version=3.1.5.0, Culture=neutral, PublicKeyToken=89e7d7c5feba84ce' or one of its dependencies. Invalid pointer (Exception from HRESULT: 0x80004003 (E_POINTER))