我有一个服务,该服务使用执行域对外部应用程序进行API调用。因此,如果我处于测试模式,则称呼“ http://localhost”,如果我处于生产状态,则希望它称呼另一个地址。到目前为止,我有
public interface IMyService{
void DoStuff();
}
private string _url;
public class MyService : IMyService
{
public MyService(string ExecutingDomainAddress)
{
_url = ExecutingDomainAddress;
}
public void DoStuff()
{
var destination = _url + "/GetCustomers";
}
}
在我的Unity配置中,我将服务和合同定义为
container.RegisterType<IMyService, MyService>();
我想将当前的url /地址注入Service构造函数。可以从这里完成吗? 例如:
var theCurrenDomain = "http://localhost/MySite"; //I want this to be dynamically generated e.g. Request.Url.Authority
container.RegisterInstance<string>("ExecutingDomainAddress", theCurrenDomain, new PerThreadLifetimeManager());
container.RegisterType<IMyService, MyService>(new InjectionConstructor(new ResolvedParameter<string>("ExecutingDomainAddress")));
如何从此处获取网址并将其应用于“ theCurrentDomain”变量?
答案 0 :(得分:1)
我倾向于创建一个代表这些设置的类,然后将该类注册为类型。然后,任何依赖于这些设置的类都可以通过构造函数注入来请求该类型的对象。
container.RegisterType<FileSystemPricerStagingDirectorySettings>(new InjectionConstructor(ConfigurationManager.AppSettings["PricerStagingDirectory"]));
container.RegisterType<IPricerStagingRepository, FileSystemPricerStagingRepository>();
public FileSystemPricerStagingRepository(FileSystemPricerStagingDirectorySettings pricerStagingDirectorySettings)
{
// now I can get what I need from pricerStagingDirectorySettings
}