使用 Gallio/MbUnit 对 WCF Web 服务进行单元测试

发布于 2024-09-17 09:30:31 字数 687 浏览 2 评论 0原文

我有一个 WCF 服务,带有一个调用该服务的简单 Gallio 单元测试。但是,我对框架的行为感到困惑。当我第一次启动 Visual Studio 并尝试运行单元测试时,出现以下错误:

System.ServiceModel.EndpointNotFoundException:没有端点侦听 http://localhost:43671/MyService .svc/MyService 可以接受消息。这通常是由不正确的地址或 SOAP 操作引起的。有关更多详细信息,请参阅 InnerException(如果存在)。 ---> System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets.SocketException:无法建立连接,因为目标机器主动拒绝它127.0.0.1:43671 错误代码:10061

但是,如果我右键单击单元测试项目中的服务引用并执行“更新服务引用”,则会弹出一个窗口,显示端口 43671 上的 ASP.NET 开发服务器已启动,并且单元测试工作正常。

如何将服务器启动代码添加到我的单元测试中,这样我就不必每次想要运行单元测试时都“更新服务引用”?

谢谢。

I have a WCF service with a simple Gallio unit test that calls the service. However, I am confused by the framework's behavior. When I start visual studio for the first time, and try to run the unit test, I get the following error:

System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http://localhost:43671/MyService.svc/MyService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:43671
ErrorCode: 10061

However, if I right click on the Service Reference in the unit test project and do "Update service reference", a popup saying that ASP.NET Development server on port 43671 has been started shows up, and the unit test works fine.

How do I add the server start up code to my unit test so I don't have to "Update service reference" every time I want to run a unit test?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生不灭 2024-09-24 09:31:09

您可以使用 ServiceHost 自行托管服务。如果您在单元测试的启动代码中使用它,WCF 服务将托管在单元测试中。

我不确定您正在托管的单元测试的 app.config 在哪里。但是,您始终可以在单独的 AppDomain 中运行该服务并设置 AppDOmainSetup.ConfigurationFile

You can self host a service using ServiceHost. If you use that in the startup code of your unit tests, the WCF service will be hosted in the unit test.

I'm not sure where the app.config is for the unit tests you are hosting. However, you can always run the service in a separate AppDomain and set the AppDOmainSetup.ConfigurationFile

情愿 2024-09-24 09:31:02

发生这种情况是因为运行 Visual Studio 时 WCF 服务主机未启动。一旦您尝试更新服务引用,Visual Studio 就会自动启动 WCF 服务主机,之后您的服务将可以访问。

解决此问题的一种方法是将您的服务托管在 IIS 中。这将确保即使 VS 未运行也可以访问服务。

其他解决方案是将引用(程序集引用)添加到您的服务并直接实例化服务类。您不需要运行该服务即可对其运行单元测试。因此,如果您有一个名为 SomeServiceClass 的服务类,您可以在单元测试中创建此类的实例 (SomeServiceClass instance = new SomeServiceClass()) 并针对它编写断言(甚至没有托管服务)。

This happens because your WCF service host is not started when you run visual studio. Once you try to update the service reference Visual Studio automatically starts the WCF service host and after that your services will be accessible.

One way to solve this problem is to host your service(s) in IIS. This will ensure that the services will be accessible even if VS is not running.

Other solution would be to add reference (assembly reference) to your service and instantiate the service class directly. You don't need to have the service running to run unit tests against it. So if you have a service class called SomeServiceClass you could create an instance of this class in your unit tests (SomeServiceClass instance = new SomeServiceClass()) and write your asserts against it (without even hosting the service).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文