ServiceReferences.ClientConfig中的动态端点

ran*_*oms 16 c# silverlight wcf-binding

构建应用程序时,它通常部署在不同的环境(test,dev,prod)中,因此端点地址正在发生变化.由于ServiceReferences.ClientConfig是作为Silverlight的.xap文件的一部分构建的,因此在构建解决方案后很难更改端点,这通常是通过web.config完成的.

我已经搜索了很多,但是我无法弄清楚这里的最佳做法是什么,所以我的问题是:

在Silverlight中进行动态wcf端点地址配置时,最佳做法是什么?

为了澄清,根据应用程序所在的服务器(test,dev,prod),端点会发生变化:

  <endpoint
    name="MyService"
    address="http://testserv/MyService.svc"
    binding="basicHttpBinding"
    bindingConfiguration="MybasicHttpBinding"
    contract="MyApp.MyService"
             />

  <endpoint
    name="MyService"
    address="http://prodserv/MyService.svc"
    binding="basicHttpBinding"
    bindingConfiguration="MybasicHttpBinding"
    contract="MyApp.MyService"
             />
Run Code Online (Sandbox Code Playgroud)

在某种程度上,我需要Silverlight客户端知道使用哪一个,具体取决于它在哪个服务器上/哪个构建编译.

ran*_*oms 26

在阅读了sLedgem的帖子和一些谷歌搜索之后,我找到了使ServiceReferences像web.config一样的完美解决方案.

首先:手动创建不同的文件;

ServiceReferences.Debug.ClientConfig
ServiceReferences.Release.ClientConfig
Run Code Online (Sandbox Code Playgroud)

如果在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>
Run Code Online (Sandbox Code Playgroud)

现在,当您重新加载项目时,您将看到ServiceReferences.Release.ClientConfig在解决方案资源管理器中是可扩展的,当您展开它时,您将看到Release和Debug文件.

第三步:在结束之前将转换规则添加到项目文件中 </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>
Run Code Online (Sandbox Code Playgroud)

它的作用是查看相应的servicereferences文件,具体取决于您的配置,并使用web.config使用的相同TransformXML库复制/替换代码.

例:

在我的ServiceReferences.ClientConfig中,我有以下代码:

  <endpoint name="ICatalogueService" 
            address="address" 
            binding="basicHttpBinding"
            bindingConfiguration="My_basicHttpBinding" 
            contract="Services.ServiceInterfaces.ICatalogueService"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

如您所见,将替换端点,并在name属性上完成匹配.

如果您有任何疑问,请告诉我:)

  • 这是我见过的"最棒的",最伟大,最精彩的东西.正是我需要的......我将它用于我自己的XML文件,其中包含其他类型的配置.非常感谢您发布此内容.完美的工作!+1 (3认同)

小智 6

这个问题很好的解决方案
我无法让该<ItemGroup></ItemGroup>部分在我的解决方案中有效运作.
我删除了它并将以下脚本添加到项目中的Prebuild事件中:

del $(ProjectDir)ServiceReferences.ClientConfig;
copy $(ProjectDir)ServiceReferences.$(ConfigurationName).ClientConfig $(ProjectDir)ServiceReferences.ClientConfig;
Run Code Online (Sandbox Code Playgroud)