ServiceStack AppHostHttpListenerBase无法连接到远程服务器

时间:2013-10-07 19:27:14

标签: c# servicestack functional-testing

我正在为我的应用程序完成一些功能测试,我认为我已经非常接近了。我的问题是,当我运行第一次测试时,我得到了错误。

  

无法连接到远程服务器。

     

预期:好的    但是:0

我可以确认,如果我在Assert上设置断点,然后尝试点击浏览器中的BaseUrl,则找不到它。

这是我的测试。

[Test]
public void MyTestTest ()
{
    var client = new RestClient( ServiceTestAppHostBase.BaseUrl );
    // client.Authenticator = new HttpBasicAuthenticator( NUnitTestLoginName, NUnitTestLoginPassword );
    var request = new RestRequest( "/users/", Method.GET );
    request.RequestFormat = DataFormat.Json;
    var response = client.Execute( request );

    // do assertions on the response object now
    Assert.That( response.StatusCode, Is.EqualTo( HttpStatusCode.OK ) );
}

AppServerTestSetup看起来像这样

[SetUpFixture]
public class AppServerTestSetup
{
    ServiceTestAppHostBase _appHost;

    [SetUp]
    public void SetUp()
    {
        _appHost = new ServiceTestAppHostBase();
        _appHost.Init();
        _appHost.Start(ServiceTestAppHostBase.BaseUrl);
    }

    [TearDown]
    public void TearDown()
    {
        _appHost.Dispose();
    }
}

ServiceTestAppHostBase看起来像这样。

public class ServiceTestAppHostBase : AppHostHttpListenerBase
{
    public const string BaseUrl = "http://localhost:8082/";
    public ServiceTestAppHostBase () : base( "OurApp.AppServer", typeof( UserServiceInterface ).Assembly ) { }

    public override void Configure ( Container container )
    {
        JsConfig.EmitCamelCaseNames = true;

        SetConfig( new EndpointHostConfig
        {
            MapExceptionToStatusCode = {
                { typeof( NotFoundException ), 404 }, // Map exception to 404 not found http response.
                { typeof( SystemAccountChangeException ), 405 } // Map exception to 405 method not allowed.
            }
        } );

        // Shared Container Registration
        AppHostContainerRegistrations.Register( container );

        // Setup the database
        var migrationRunner = container.Resolve<IMigrationRunner>();

        migrationRunner.CreateDatabase();
        migrationRunner.RunAll();
    }
}

注意:我也在主应用中使用AppHostContainerRegistrations,它正在运行。我还验证了它是在测试设置中运行的。

AppHostContainerRegistrations(供参考)看起来像这样。

public class AppHostContainerRegistrations
{
    public static void Register(Container container)
    {
        // IOC Registration
        // Register base connection config
        var dbConnection = ConfigurationManager.ConnectionStrings["databaseConnection"];
        var databaseName = ConfigurationManager.AppSettings["databaseName"];

        // Register Sqlserver DbProvider
        container.Register<IDbConnectionProvider>( containr => new DbConnectionProvider( dbConnection.ConnectionString, dbConnection.ProviderName ) );
        container.Register<IDbProvider>( containr => new DbProvider( containr.Resolve<IDbConnectionProvider>(), databaseName ) );

        // Register repositories
        container.RegisterAs<DatabaseVersionRepository, IDatabaseVersionRepository>();
        container.RegisterAs<UserRepository, IUserRepository>();
        container.RegisterAs<GroupRepository, IGroupRepository>();
        container.RegisterAs<DeviceRepository, IDeviceRepository>();
        container.RegisterAs<SecuritySettingsRepository, ISecuritySettingsRepository>();

        // Register services
        container.RegisterAs<UserService, IUserService>();
        container.RegisterAs<GroupService, IGroupService>();
        container.RegisterAs<SecuritySettingsService, ISecuritySettingsService>();

        // Register everything else
        container.RegisterAs<PasswordHasher, IPasswordHasher>();
        container.RegisterAs<MigrationRunner, IMigrationRunner>();

        container.Register( new UserModel { Id = new Guid( "6C83DDEC-5E58-4F28-BDE2-61EBF1B49691" ) } );

    }
}

我们这样做Db设置的原因是因为我们在App.Config中有一个连接字符串和数据库名称,我们在部署期间依赖Transforms来设置数据库。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:5)

经过与@mythz的长时间对话后,发现必须在管理模式下运行VS才能运行“AppHostHttpListenerBase”。

从终端运行./build时,我还必须以管理员身份运行Powershell。