【问题标题】:Is it possible Create and Close wcf Service Host in MS Test是否可以在 MS 测试中创建和关闭 wcf 服务主机
【发布时间】:2011-12-06 20:52:16
【问题描述】:

我通常使用 NUnit 作为 UnitTest 框架,但是我现在工作的地方他们只使用 MSTest。 在 Nunit 中,我可以使用以下内容:

    [FixtureSetup]
    public override void MainSetup()
    {
        _serviceHost = new ServiceHost(typeof(PersonService));
        _serviceHost.Open();
     }
     [FixtureTearDown]
     public override void MainTeardown()
     {
        _serviceHost.Close();
     }

我注意到,在 MSTest 中,如果您想在所有测试期间初始化并在所有测试运行后关闭,您必须使用以下 STATIC 方法,并且您知道我不能再使用我的类了。 下面的方法会崩溃!!!

    [ClassInitialize()]
    public static void MyClassInitialize(TestContext testContext)
    {
        _serviceHost = new ServiceHost(typeof(PersonService));
        _serviceHost.Open();
    }

如何在 MSTest 中运行所有测试后初始化我的服务主机一次并关闭?

感谢您的任何建议

【问题讨论】:

  • “崩溃”是什么意思?你能更具体一点你遇到什么问题吗?是编译器错误还是堆栈跟踪?

标签: c# wcf unit-testing mstest


【解决方案1】:

如果您也将_serviceHost 变量设为静态,您应该是安全的。

【讨论】:

  • 为迟到的回复道歉。刚刚成功上线。这工作。为什么我没有想到这一点。谢谢!!
猜你喜欢
  • 2011-12-12
  • 1970-01-01
  • 2011-06-13
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多