【发布时间】:2016-02-13 02:32:30
【问题描述】:
我最初写了一个单元测试失败,现在我已经解决了这个问题,但是当我尝试再次运行我的单元测试时,我得到了这个错误
System.InvalidOperationException:在 ServiceModel 客户端配置部分中找不到引用合同“InventoryManager.Services.IUserService”的默认端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此协定匹配的端点元素。
我不知道为什么会发生这种情况,因为我在单元测试 App.Config 中使用与我的服务库 App.Config 相同的配置,并且合同名称相同。
服务库项目 App.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="InventoryManager.Services.UserService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:3637/Service.svc" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="basicHttpBinding" contract="InventoryManager.Services.IUserService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<connectionStrings>
<add name="InventoryManagerEntities" connectionString="metadata=res://*/InventoryManagerModel.csdl|res://*/InventoryManagerModel.ssdl|res://*/InventoryManagerModel.msl;provider=System.Data.SqlClient;provider connection string="data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
单元测试项目 App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="InventoryManager.Services.UserService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:3637/Service.svc?wsdl" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="basicHttpBinding" contract="InventoryManager.Services.IUserService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<connectionStrings>
<add name="InventoryManagerEntities" connectionString="metadata=res://*/InventoryManagerModel.csdl|res://*/InventoryManagerModel.ssdl|res://*/InventoryManagerModel.msl;provider=System.Data.SqlClient;provider connection string="data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
【问题讨论】:
-
baseAddress略有不同,是故意的吗? -
单元测试应该独立于任何外部资源,如数据库、服务等。
-
请注意,您有一个 WCF 服务 库。您需要将库的 app.config 中的
<system.serviceModel>部分复制到托管服务的应用程序的 web/app.config。甚至库项目模板也包含一个 app.config 文件,库不使用它 - 它们使用正在使用该库的应用程序的 web/app.config。 -
@MartinMulder 你能解释一下是什么让我的单元测试依赖于服务或数据库吗?...据我了解,我正在测试 ma 控制器。它是调用我的服务的控制器
-
让我更清楚一点:单元测试不应该直接或间接调用数据库或服务。
标签: c# wcf unit-testing asp.net-mvc-4 invalidoperationexception