在阅读了 sLedgem 的帖子和一些谷歌搜索后,我找到了使 ServiceReferences 像 web.config 一样的完美解决方案。
首先:
手动创建不同的文件;
ServiceReferences.Debug.ClientConfig
ServiceReferences.Release.ClientConfig
如果您在 Visual Studio 中有两个以上的默认配置,您也可以添加自己的配置。
第二:
在 Project.csproj 文件中添加文件依赖(在文本编辑器中打开项目文件):
<ItemGroup>
<None Include="Properties\AppManifest.xml" />
<Content Include="ServiceReferences.ClientConfig">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="ServiceReferences.Debug.ClientConfig">
<DependentUpon>ServiceReferences.ClientConfig</DependentUpon>
</Content >
<Content Include="ServiceReferences.Release.ClientConfig">
<DependentUpon>ServiceReferences.ClientConfig</DependentUpon>
</Content >
</ItemGroup>
现在,当您重新加载项目时,您将看到 ServiceReferences.Release.ClientConfig 在解决方案资源管理器中是可展开的,当您展开它时,您将看到 Release 和 Debug 文件。
第三:在关闭</Project>之前将Transformation规则添加到Project文件中
(再次,在文本编辑器中打开它)
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. -->
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="exists('ServiceReferences.$(Configuration).ClientConfig')">
<!-- Generate transformed ServiceReferences config in the intermediate directory -->
<TransformXml Source="ServiceReferences.ClientConfig" Destination="$(IntermediateOutputPath)$(TargetFileName).ClientConfig" Transform="ServiceReferences.$(Configuration).ClientConfig" />
<!-- Force build process to use the transformed configuration file from now on. -->
<ItemGroup>
<ServiceReferencesConfigWithTargetPath Remove="ServiceReferences.ClientConfig" />
<ServiceReferencesConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).ClientConfig">
<TargetPath>$(TargetFileName).ClientConfig</TargetPath>
</ServiceReferencesConfigWithTargetPath>
</ItemGroup>
</Target>
它的作用是根据您的配置查看相应的服务引用文件,并使用 web.config 使用的相同 TransformXML 库复制/替换代码。
例子:
在我的 ServiceReferences.ClientConfig 我有以下代码:
<endpoint name="ICatalogueService"
address="address"
binding="basicHttpBinding"
bindingConfiguration="My_basicHttpBinding"
contract="Services.ServiceInterfaces.ICatalogueService"/>
ServiceReferences.Release.ClientConfig:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.serviceModel>
<client>
<endpoint
name="ICatalogueService"
address="http://server/Services/CatalogueService.svc"
binding="basicHttpBinding"
bindingConfiguration="My_basicHttpBinding"
contract="Services.ServiceInterfaces.ICatalogueService"
xdt:Transform="Replace" xdt:Locator="Match(name)" />
</client>
<extensions />
</system.serviceModel>
</configuration>
如您所见,端点将被替换,并且匹配在 name 属性上完成。
如果您有任何问题,请告诉我:)