首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ServiceReferences.ClientConfig中的动态端点

ServiceReferences.ClientConfig中的动态端点
EN

Stack Overflow用户
提问于 2011-09-09 18:50:03
回答 4查看 14.8K关注 0票数 16

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

我已经搜索了很多,但我不知道这里的最佳实践是什么,所以我的问题是:

在silverlight中进行动态wcf端点地址配置时,最佳实践是什么?

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

代码语言:javascript
复制
  <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"
             />

在某种程度上,我需要silverlight客户端知道使用哪个版本,这取决于它所在的服务器/编译的版本。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-11-04 19:38:26

在阅读了sLedgem的帖子并用谷歌搜索后,我找到了一个完美的解决方案,可以让ServiceReferences表现得像web.config一样。

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

代码语言:javascript
复制
ServiceReferences.Debug.ClientConfig
ServiceReferences.Release.ClientConfig

如果在Visual Studio中有两个以上的默认配置,您也可以添加自己的配置。

第二:在Project.csproj文件中添加文件依赖项(在文本编辑器中打开项目文件):

代码语言:javascript
复制
  <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 and Debug文件。

第三:在关闭</Project>之前将转换规则添加到项目文件中

(同样,在文本编辑器中打开它)

代码语言:javascript
复制
<!-- 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>

它所做的是查找相应的servicereferences文件,并使用与web.config相同的TransformXML库复制/替换代码。

示例:

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

代码语言:javascript
复制
  <endpoint name="ICatalogueService" 
            address="address" 
            binding="basicHttpBinding"
            bindingConfiguration="My_basicHttpBinding" 
            contract="Services.ServiceInterfaces.ICatalogueService"/>

ServiceReferences.Release.ClientConfig:

代码语言:javascript
复制
<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属性上完成匹配。

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

票数 27
EN

Stack Overflow用户

发布于 2012-11-19 22:30:56

很好的问题解决方案

我无法让<ItemGroup></ItemGroup>部分在我的解决方案中有效地工作。

我删除了它,并将以下脚本添加到项目中的预构建事件中:

代码语言:javascript
复制
del $(ProjectDir)ServiceReferences.ClientConfig;
copy $(ProjectDir)ServiceReferences.$(ConfigurationName).ClientConfig $(ProjectDir)ServiceReferences.ClientConfig;
票数 6
EN

Stack Overflow用户

发布于 2011-09-13 03:02:31

看看这里:

http://weblogs.asp.net/srkirkland/archive/2009/10/13/common-web-config-transformations-with-visual-studio-2010.aspx

然后在这里

http://www.funkymule.com/post/2010/03/08/XML-Transform-on-Silverlight-ClientConfig-Files.aspx

它使用web.config转换背后的相同原理(即,web.config根据您正在编译的配置(即发布、调试)进行更改,因此serviceref.config在编译时根据您的突发奇想进行更改。施展魅力

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7360533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档