ServiceReferences.ClientConfig 中的动态端点 [英] Dynamic endpoints in ServiceReferences.ClientConfig

查看:10
本文介绍了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 的帖子并搜索了一些谷歌之后,我找到了让 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="PropertiesAppManifest.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.

第三:在关闭</Project>

(再次在文本编辑器中打开它)

(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)MicrosoftVisualStudiov10.0WebMicrosoft.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 库复制/替换代码.

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 中的动态端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆