我有一个ServiceStack AppHostHttpListenerBase在我的测试配置与运行测试的,像这样的宗旨:ServiceStack AppHostHttpListenerBase无法解析服务的依赖关系
public class UserProfileBehaviours : BaseTest<UserProfileService>
{
[Test]
public void Can_access_secure_service()
{
var client = GetClientWithUserPassword();
var result = ((IRestClient)client).Get(new Dto.View.Requests.UserProfileRequest
{
Slug = "user"
});
result.Result.UserAccount.Username.Should().Be("user");
}
}
我BaseTest样子:
[TestFixture]
public class BaseTest<T>
{
protected TestAppHostHttpListenerBase<T> AppHost;
private const string ListeningOn = "http://localhost:82/";
private const string UserName = "user";
private const string Password = "[email protected]";
protected readonly Container Container;
public BaseTest()
{
Container = new Funq.Container
{
Adapter = new WindsorContainerAdapter()
};
}
[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
AppHost = new TestAppHostHttpListenerBase<T>();
AppHost.Init();
AppHost.Start(ListeningOn);
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
AppHost.Dispose();
}
protected IServiceClient GetClient()
{
return new JsonServiceClient(ListeningOn);
}
protected IServiceClient GetClientWithUserPassword()
{
return new JsonServiceClient(ListeningOn)
{
UserName = UserName,
Password = Password
};
}
}
然后我WindsorContainerAdapter:
最后我的TestAppHostH ttpListener
public class TestAppHostHttpListenerBase<T> : AppHostHttpListenerBase
{
public const string WebHostUrl = "http://localhost:82/";
private InMemoryAuthRepository _userRep;
private const string UserName = "user";
private const string Password = "[email protected]";
public const string LoginUrl = "specialLoginPage.html";
public TestAppHostHttpListenerBase()
: base("Validation Tests", typeof(T).Assembly)
{
}
public override void Configure(Container container)
{
var appSettings = new AppSettings();
SetConfig(new EndpointHostConfig { WebHostUrl = WebHostUrl });
Plugins.Add(new AuthFeature(
() =>
new AuthUserSession(),
new IAuthProvider[]
{
new BasicAuthProvider(),
new CredentialsAuthProvider(),
new TwitterAuthProvider(appSettings),
new FacebookAuthProvider(appSettings)
}, "~/" + LoginUrl));
container.Register<ICacheClient>(new MemoryCacheClient());
_userRep = new InMemoryAuthRepository();
container.Register<IUserAuthRepository>(_userRep);
CreateUser(1, UserName, null, Password, new List<string> { "TheRole" }, new List<string> { "ThePermission" });
}
private void CreateUser(int id, string username, string email, string password, List<string> roles = null, List<string> permissions = null)
{
string hash;
string salt;
new SaltedHash().GetHashAndSaltString(password, out hash, out salt);
if (_userRep.GetUserAuthByUserName(username) == null)
{
_userRep.CreateUserAuth(new UserAuth
{
Id = id,
DisplayName = "DisplayName",
Email = email ?? "[email protected]{0}.com".Fmt(id),
UserName = username,
FirstName = "FirstName",
LastName = "LastName",
PasswordHash = hash,
Salt = salt,
Roles = roles,
Permissions = permissions
}, password);
}
}
}
当容器被配置,我可以看到,然而对于UserAccountRepository
一个组件 - 如果该组件是一个UserProfileService
的扶养客户端收到异常说的自动装配Autowired扶养无法解析。 我不明白的是AppHostHttpListenerBase
从哪里得到它的容器?
我的Windsor适配器永远不会被要求到Resolve
存储库的组件。
我怎样才能给AppHostHttpListenerBase
容器,以便它可以解决这些依赖关系?或者我需要另一种方式进行配置?
BOOM !!多谢,伙计 – iwayneo