本文介绍了在ServiceReferences.ClientConfig动态端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当建立一个应用程序,它经常被部署在不同的环境(测试,偏差,督促),所以端点地址被改变。作为ServiceReferences.ClientConfig被构建为Silverlight中的.xap文件的一部份,其难改端点构建溶液后,由于经常与web.config中进行。

When building an app, it is often deployed in different environments (test, dev, prod), and therefore the endpoint addresses are changing. As the ServiceReferences.ClientConfig is built as a part of Silverlight's .xap file, its hard to change the endpoints after building the solution, as often is done with web.config.

我已经搜查了不少吧,但我不能弄清楚什么是最好的做法在这里,所以我的问题是:

I've searched quite a bit for it, but I cant figure out what is best practice here, so my question is:

什么是最好的做法,当谈到?在Silverlight动态WCF端点地址配置

What is best practice when it comes to dynamic wcf endpoint address configuration in silverlight?

要澄清,这取决于服务器上的应用程序是(测试,开发,督促)的端点改变:

To clarify, depending on which server the app is on (test,dev, prod) the endpoints change:

  <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客户端知道使用哪一个,这取决于服务器的上/哪个构建编译。

In some way, i need the silverlight client to know which one to use, depending on which server its on / which build is compiled.

推荐答案

阅读sLedgem的帖子,还有一些google搜索后,我找到了完美的解决方案,使ServiceReferences行为。一样的web.config

After reading sLedgem's post, and some googling, I found the perfect solution to make ServiceReferences act like web.config.

第一关:
手动创建不同的文件;

First off:Create the different files manually;

ServiceReferences.Debug.ClientConfig
ServiceReferences.Release.ClientConfig

你可以添加自己的,以及如果你有比Visual Studio中的两个默认配置的详细

You can add your own as well if you have more than the two default configurations in Visual Studio.

二:
添加文件相关性在Project.csproj文件(在文本编辑器中的项目文件):

Second:Add the file dependency in the Project.csproj file (Open the project file in a text editor):

  <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是可扩展的。解决方案资源管理器中,当你展开它,你会看到发布和调试文件

Now, when you reload the project, you will see that ServiceReferences.Release.ClientConfig is expandable in the Solution Explorer, and when you expand it, you will see the Release and Debug file.

第三:刚刚结束<$ C前添加转换规则到项目文件$ C>< /项目>

(同样,在一个文本编辑器打开)

(again, open it in a text editor)

<!-- 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库代码

What it does is to look in the corresponding servicereferences file, depending on your configuration, and copy / replace code using the same TransformXML library that web.config uses.

例如:

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

in my ServiceReferences.ClientConfig i have the following code:

  <endpoint name="ICatalogueService" 
            address="address" 
            binding="basicHttpBinding"
            bindingConfiguration="My_basicHttpBinding" 
            contract="Services.ServiceInterfaces.ICatalogueService"/>



ServiceReferences.Release.ClientConfig:

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属性完成。

As you can see, the endpoint will be replaced, and the match is done on the name attribute.

如果您有任何疑问,请让我知道:)

If you have any questions, let me know :)

这篇关于在ServiceReferences.ClientConfig动态端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:55