ServiceStack AppHostHttpListenerBase无法连接到远程服务器 [英] ServiceStack AppHostHttpListenerBase Unable to connect to the remote server

查看:105
本文介绍了ServiceStack AppHostHttpListenerBase无法连接到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过我的应用程序进行一些功能测试,我认为我已经接近了.我的问题是,当我运行我的第一个测试时,我得到了错误.

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.

预期:确定
但是是:0

Expected: OK
But was: 0

我可以确认是否在断言上放置了一个断点,然后尝试在浏览器中点击BaseUrl,但找不到它.

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.

这是我的考试.

[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,它正在工作.我还验证了它正在测试设置中运行.

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.

AppHostContainerRegistrations(供参考)如下所示.

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

    }
}

之所以进行这样的Db设置,是因为App.Config中只有一个连接字符串和数据库名称,并且在部署过程中依赖于Transforms来设置数据库.

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?

推荐答案

与@mythz进行了漫长的交谈之后,事实证明,必须以Admin模式运行VS才能运行"AppHostHttpListenerBase".

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

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

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

这篇关于ServiceStack AppHostHttpListenerBase无法连接到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆