I need to build a small application to read/set the figure in app.config file, which in remoting section channel port value,
how to do it programmatically?

as following example, how can I read/set the 209 value in coding?

pls help!Thanks!
-----------------------------------------------------------------

<system.runtime.remoting>
<customErrors mode="off"/>
<application>
<channels>
<channel ref="tcp" port="209">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="soap" typeFilterLevel="Full" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
<service>

<wellknown mode="Singleton" .....
</service>
</application>
</system.runtime.remoting>

Dani AI

Generated

— the remoting section is not exposed by the simple AppSettings/ConnectionStrings helpers. 's pointer to a general read/write guide is useful for working with the config API, but for the specific remoting channel port it is safest to treat that section as XML (or to reconfigure channels at runtime with the remoting API). The snippet below shows two practical options: (A) read/update the system.runtime.remoting section in the actual config file using LINQ to XML, and (B) change the listening port immediately by unregistering and re-registering a TcpChannel.

Read the configured TCP port from the running app's config file:

using System;
using System.Linq;
using System.Xml.Linq;

public static int? ReadRemotingTcpPort()
{
    var cfg = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
    var doc = XDocument.Load(cfg);

    var channel = doc
        .Descendants("system.runtime.remoting")
        .Descendants("application")
        .Descendants("channels")
        .Descendants("channel")
        .FirstOrDefault(ch => (string)ch.Attribute("ref") == "tcp" || ch.Attribute("port") != null);

    var pa = channel?.Attribute("port");
    return pa != null && int.TryParse(pa.Value, out var p) ? (int?)p : null;
}

Set a new port in the file (then refresh the section) and, if you need the change live, unregister/register the channel programmatically:

using System.Configuration;
using System.Xml.Linq;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

public static void SetRemotingTcpPort(int newPort)
{
    var cfg = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
    var doc = XDocument.Load(cfg);

    var channel = doc
        .Descendants("system.runtime.remoting")
        .Descendants("application")
        .Descendants("channels")
        .Descendants("channel")
        .FirstOrDefault(ch => (string)ch.Attribute("ref") == "tcp" || ch.Attribute("port") != null);

    if (channel == null) throw new InvalidOperationException("No remoting channel node found.");
    var attr = channel.Attribute("port");
    if (attr == null) channel.Add(new XAttribute("port", newPort));
    else attr.Value = newPort.ToString();

    doc.Save(cfg);
    ConfigurationManager.RefreshSection("system.runtime.remoting");
}

// To apply without restarting:
public static TcpChannel ReplaceTcpChannel(TcpChannel old, int port)
{
    if (old != null) ChannelServices.UnregisterChannel(old);
    var ch = new TcpChannel(port);
    ChannelServices.RegisterChannel(ch, false);
    return ch;
}

Notes and cautions: back up the config before saving, ensure the process has write permission, and remember that editing the file alone won't change ports for already-running channels (unregister/register or restart required). .NET Remoting is a legacy technology and is not supported in .NET Core; for new work prefer modern alternatives (WCF / gRPC / HTTP) as described in Microsoft's remoting guidance.

Recommended Answers

All 2 Replies

Thanks for reply.
In application configuration file, I can use method Configuration.AppSettings to read value which setting in section of AppSetting ; I can use method Configuration.ConnectionStrings to read value which setting in section of ConnectionString . However , there is no any method provided to read value in section of system.runtime.remoting......
if I intent read value from the section, how can I do?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.