2

app.configに次のようにアドレス文字列を作成する必要があります。

<client>
       <endpoint address="http://ServerName/xxx/yyy.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IClientIInfoService"
                    contract="DocuHealthLinkSvcRef.IClientIInfoService" name="BasicHttpBinding_IClientIInfoService" />
</client>

インストール中にユーザーが入力するServerName必要があります。そのために、インストーラーで新しいUIダイアログを作成しました。また、Installer.csクラスを作成し、次のようにオーバーライドしinstall ()ました。

public override void Install(System.Collections.IDictionary stateSaver)
        {

            base.Install(stateSaver);

            string targetDirectory = Context.Parameters["targetdir"];

            string ServerName = Context.Parameters["ServerName"];

            System.Diagnostics.Debugger.Break();

            string exePath = string.Format("{0}myapp.exe", targetDirectory);

           Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

            config.AppSettings.Settings["ServerName"].Value = ServerName;

            config.Save();
        }
    }

ServerNameしかし、これを使用app.configして指定された文字列を作成するにはどうすればよいですか。私はVS2010に取り組んでいます。

4

5 に答える 5

5

WiX(WindowsインストーラーXMLツールセット)を使用してMSIを構築できます。その場合、XmlFileユーティリティタグを使用してサーバー名を更新できます。

  <util:XmlFile Id="UpdateServerName" File="[INSTALLLOCATION]AppName.exe.config" Action="setValue" ElementPath="/client/endpoint" Name="address" Value="http://[SERVERNAME]/xxx/yyy.svc" />

WixUI拡張フォームを使用して、インストール中にサーバー名を取得できます。

WiXの利点:WiXはmsbuildに準拠しており(.vdprojファイルとは異なり)、特にインストーラーをよりきめ細かく制御できます。

于 2013-01-11T04:33:37.320 に答える
3

app.configで完全なServiceModelセクショングループを使用していると仮定します

基本的に、次の手順に従います。

  1. ServiceModel構成セクションをロードします
  2. クライアントセクションを取得
  3. ChannelEndpoint要素を取得する
  4. 文字列「ServerName」を入力した値に置き換えて、アドレス値を変更します
  5. アドレス属性を新しい値に設定します
  6. 設定を保存

    public override void Install(System.Collections.IDictionary stateSaver)
    {
    
        base.Install(stateSaver);  
    
        string targetDirectory = Context.Parameters["targetdir"];  
    
        string ServerName = Context.Parameters["ServerName"];  
    
        System.Diagnostics.Debugger.Break();  
    
        string exePath = string.Format("{0}myapp.exe", targetDirectory);  
    
        Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);  
    
        config.AppSettings.Settings["ServerName"].Value = ServerName;  
    
        //Get ServiceModelSectionGroup from config  
        ServiceModelSectionGroup group = ServiceModelSectionGroup.GetSectionGroup  (config);
    
        //get the client section
        ClientSection clientSection = group.Client;
    
        //get the first endpoint
        ChannelEndpointElement channelEndpointElement = clientSection.Endpoints[0];
    
        //get the address attribute and replace servername in the string.
        string address = channelEndpointElement.Address.ToString().Replace("ServerName", ServerName);
    
        //set the Address attribute to the new value
        channelEndpointElement.Address = new Uri(address);
    
        config.Save();
    }
    
于 2013-01-07T05:36:36.650 に答える
2

一日の終わりには、app.configファイルxmlです。Linq To XMLまたはXPathNavigatoraddressを使用して、要素の属性を置き換えることができますendpoint

以下のコードはLinqtoXmlを使用しています

    using System.Xml.Linq;
    public override void Install(System.Collections.IDictionary stateSaver)
    {

        base.Install(stateSaver);

        string targetDirectory = Context.Parameters["targetdir"];

        string ServerName = Context.Parameters["ServerName"];

        System.Diagnostics.Debugger.Break();

        string configPath = string.Format("{0myapp.exe.config", targetDirectory);

        XElement root = XElement.Load(configPath);
        var endPointElements = root.Descendants("endpoint");
        foreach(var element in endPointElements)
        {
            element.Attribute("address").Value = ServerName;
        }
        root.Save(configPath);
    }
}
于 2013-01-07T06:08:32.490 に答える
2

あなたはwindows-installerタグを持っているので、MSIパッケージを持っているか、それを作成できると思います...

それで:

  • インストール中に必要なENDPOINTSERVERのようなパブリックMSIプロパティを作成できます。
  • ENDPOINTSERVERの値を使用して、「InstallFinalize」の後に実行するようにapp.configを変更するカスタムアクションを追加します

サイレントインストールは、以下を使用して可能になります。

msiexec /i app.msi ENDPOINTSERVER=www.MyServer.com /qb-
于 2013-01-08T13:12:34.507 に答える
2

設定ファイルの変更を保存する前に、以下の2行を使用してみてください。

config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("Section Name");
 root.Save(configPath);

PS:ソリューションアイテム「app.config」は更新されませんが、F5で実行した場合はbin/フォルダーにある「.exe.config」が更新されます。

于 2013-01-08T15:30:23.310 に答える