2009-08-25 24 views
14

Silverlightアプリケーションでwcfサービスを使用しています。 wcfサービスの場所はServiceReferences.ClientConfigファイルに記述されており、アプリケーションがインストールされている場所に変更する必要があります。Silverlight:ServiceReferences.ClientConfigをxap内に配置するときの設定方法

ただし、このファイルはxapファイルに含まれており、アプリケーションの展開時に簡単に変更できるものではありません。 Silverlightアプリケーションからwcfサービスへの参照を行う別の方法はありますか?または、xapファイルのServiceReferences.ClientConfigをどのように変更しますか?

答えて

2

このブログで解決策が見つかりました。ここで

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

WCFサービスのエンドポイントは、私が使用してのオープンだが、これは私のために動作し、それは柔軟だもっと良い方法があるかもしれませんSilverlightアプリケーション

+0

この解決策は私が心配する限り最高です。他のハードコーディングされた解決策は私のためには機能しません。 – helios456

8

の位置から計算されます。

WebアプリケーションのWeb.configで、AppSettingsに変数を追加し、ベースURLを保存します。これは、SVCファイルの場所が格納されていないことに注意してください。これは、私が通常指し示す複数のSVCがあるためです。あなたはそれを違うように選ぶかもしれません。私のWebアプリケーションのWebページで

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

、PARAMを追加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を読み込むか、どこが私のXAMLファイルのいずれかでその後

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 

をしたい、これまで私が設定された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 
7

エクセレントようethodは、これらの提案で、私は、URIがweb.configファイルから読み込むサービスで、私のWCF ServiceReferences.ClientConfigデータを動的にアプリケーションの起動時に変更する得ることができました。これはVS2010の "web.config transformations"を使用することで可能です。

サンプルweb.config.debugでは、自分のWebサイトで「公開」を選択したときの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を変更する必要はありません。偉大な、私はしばらくそれを探していた。

2

ここに示すソリューションはすべて、アプリケーションを変更して設定設定に合わせるという意味では実用的ではありません。これはblog entryに刻まれました。

+0

デプロイ時にweb.configに適用できるxml変換を反映するため、これが最適なソリューションであることがわかりました – Calanus

関連する問題