RabbitMQを参照しているライブラリを参照するのではなく、参照せずにライブラリ(正確にはRabbitMQ.Client)を使用しようとしています。混乱しないことを願っています。ところで、もし変だと思うなら、SimpleInjector を使って動的に切り替えられるメッセージ キュー ライブラリが欲しかったからです。私の問題は、関数を使用するたびに、参照時に FileNotFound Exception がスローされることです。サンプルコードは次のとおりです。
依存関係リゾルバー
参考: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;
}
}
MessageQ.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))