【问题标题】:Silverlight: How to setup ServiceReferences.ClientConfig when placed inside xapSilverlight:放置在 xap 中时如何设置 ServiceReferences.ClientConfig
【发布时间】:2009-08-25 08:39:02
【问题描述】:

我在 silverlight 应用程序中使用 wcf 服务。 wcf 服务的位置在 ServiceReferences.ClientConfig 文件中说明,并且必须更改为安装应用程序的位置。

但是,该文件包含在 xap 文件中,并且在部署应用程序时不容易更改。是否有另一种方法可以从 silverlight 应用程序中引用 wcf 服务?或者您如何更改 xap 文件中的 ServiceReferences.ClientConfig ?

【问题讨论】:

    标签: wcf silverlight


    【解决方案1】:

    可能有更好的方法,我愿意使用,但这对我有用,而且很灵活。

    在您的 Web 应用程序的 Web.config 中,在 AppSettings 中添加一个变量并存储基本 URL,注意我没有存储 SVC 文件的位置,稍后我会附加它。这是因为我有多个我通常指向的 SVC。你可以选择不同的方式。

     <appSettings>
        <add key="ServiceURI" value="http://localhost:64457/"/>
     </appSettings>
    

    在我的 Web 应用程序的网页中,添加一个名为 InitParms 的参数,这允许您添加键、对值列表(由 XAP 文件读取的逗号分隔)

    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight," type="application/x-silverlight-2"
            width="100%" height="100%" ID="Xaml1" >
            <param name="InitParams" value="ServiceURI=<%= ConfigurationManager.AppSettings("ServiceURI") %>" />
    

    在 Silverlight App.xaml.vb 中,将所有 InitParms 加载到资源或您想要的任何位置

        Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
        If e.InitParams IsNot Nothing Then
            For Each k As Generic.KeyValuePair(Of String, String) In e.InitParams
                Me.Resources.Add(k.Key, k.Value)
            Next
        End If
    

    然后在我的任何 XAML 文件中,我可以使用配置的 URI 初始化服务,我有这样的方法

    Private Sub InitializeService()
        Dim uri As String = App.Current.Resources("ServiceURI")
        If uri Is Nothing OrElse uri = String.Empty Then
            'if there is no value added in the web.config, I can fallback to default values
            _client = New ServiceClient
        Else
            'Notice I hardcoded the location of the SVC files in the client and append there here, you may choose not to do this
            Dim uri_withservice As String = uri & "svc/secure/Service.svc"
            _client = New ServiceClient("CustomBinding_Service", New EndpointAddress(uri_withservice))
        End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      太好了,有了这些建议,我设法让我的 WCF ServiceReferences.ClientConfig 数据在应用程序启动时动态更改,并从 web.config 读取服务 URI。这可以通过使用 VS2010 中的“web.config 转换”来实现。

      这是一个示例 web.config.debug,显示了当我为我的网站选择“发布”时如何替换 ServiceURI。

      <?xml version="1.0"?>
      <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
      <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <connectionStrings>
           <...>
        </connectionStrings>
      
        <appSettings>
          <add key="ServiceURI" value="http://my.location.com/myService.svc"
               xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
        </appSettings>
      </configuration>
      

      在我的基础 web.config 中,我拥有相同的键/值,指向本地服务。每次部署到测试/生产时都无需记住更改 ServiceURI。太好了,我已经找了一段时间了。

      【讨论】:

        【解决方案3】:

        在此博客中找到了解决方案。

        http://www.andybeaulieu.com/Default.aspx?tabid=67&EntryID=132

        这里的 wcf 服务端点是根据 silverlight 应用程序的位置计算得出的

        【讨论】:

        • 就我而言,这个解决方案是最好的。任何其他硬编码的解决方案都不适合我。
        【解决方案4】:

        这里介绍的解决方案在您修改应用程序以适应您的配置设置的意义上都是不切实际的。这个blog entry 搞定了。

        【讨论】:

        • 我发现这是最好的解决方案,因为它反映了可在部署时应用于 web.config 的 xml 转换
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多