微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wcf – Silverlight:当放置在xap中时如何设置ServiceReferences.ClientConfig

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

但是,这个文件包含在xap文件中,而不是在部署应用程序时可以轻松更改的内容.有没有另一种方法从Silverlight应用程序中引用wcf服务?或者如何更改xap文件中的ServiceReferences.ClientConfig

解决方法

可能有更好的方法,我可以使用,但这适用于我,它是灵活的.

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

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

在我的Web应用程序的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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐