ServiceStack AppHostHttpListenerBase Unable to con

2019-02-24 02:08发布

I'm working through some Functional Tests on my app, and I think I'm getting pretty close. My problem is that when I run my first test, I get the error.

unable to connect to the remote server.

Expected: OK
But was: 0

I can confirm that if I put a breakpoint on the Assert, and then try to hit the BaseUrl in my browser, it is not found.

Here's my test.

[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 ) );
}

The AppServerTestSetup looks like this

[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();
    }
}

And the ServiceTestAppHostBase looks like this.

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();
    }
}

note: I'm also using the AppHostContainerRegistrations in the main app, and it is working. I've also verified that it's being run in the test setup.

The AppHostContainerRegistrations (for reference) looks like this.

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" ) } );

    }
}

The reason we're doing our Db setup like this is because we have a single connection string and db name in the App.Config, and we rely on Transforms during deployment to setup the database.

Can anyone help me troubleshoot this issue?

1条回答
Bombasti
2楼-- · 2019-02-24 03:02

After a lengthy conversation with @mythz, it turns out that VS has to be run in Admin mode for the "AppHostHttpListenerBase" to run.

I also have to run Powershell as Admin when running ./build from the terminal.

查看更多
登录 后发表回答