NetworkInterface EnableDhcp not working on Cobra 1 with v4.2

I’ve copied this post from another thread: http://www.tinyclr.com/forum/topic?id=10140&page=6#msg107726

if I use this code to initialize the network, I get an exception when networkInterface.EnableDhcp() is executed.


NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
if (interfaces != null && interfaces.Length > 0)
{
    NetworkInterface networkInterface = interfaces[0];

    if (networkInterface.IsDhcpEnabled == true)
    {
        networkInterface.RenewDhcpLease()
    }
    else
    {
        networkInterface.EnableDhcp();
    }

    int waitSecs = 10;
    IPAddress ip = IPAddress.GetDefaultLocalAddress();
    while (ip == IPAddress.Any && waitSecs > 0)
    {
        Thread.Sleep(1000);
        waitSecs--;
        ip = IPAddress.GetDefaultLocalAddress();
    }
}

But when I use this code, it works.


private static EthernetBuiltIn Eth1 = new EthernetBuiltIn();
if (Eth1.IsActivated)
{
    Eth1.Close();
    Eth1.Dispose();
    Eth1 = new EthernetBuiltIn();
}
Eth1.Open();
NetworkInterfaceExtension.AssignNetworkingStackTo(Eth1);

if (Eth1.IsActivated && Eth1.IsOpen && Eth1.IsCableConnected)
{
    NetworkInterface networkInterface = Eth1.NetworkInterface;

    if (networkInterface.IsDhcpEnabled == true)
    {
        networkInterface.RenewDhcpLease()
    }
    else
    {
        networkInterface.EnableDhcp();
    }

    int waitSecs = 10;
    IPAddress ip = IPAddress.GetDefaultLocalAddress();
    while (ip == IPAddress.Any && waitSecs > 0)
    {
        Thread.Sleep(1000);
        waitSecs--;
        ip = IPAddress.GetDefaultLocalAddress();
    }
}

@ jasdev - Network initialization changed with 4.2 and the premium library. The code that does not work is for the OSHW library.

@ Mike - Sorry, but I don’t understand your answer?

The first code is the “standard” NETMF way to initialize the network. Shouldn’t it work on all boards, including the Cobra 1?

The second code is the GHI premium way to initialize the network. I understand why it does work on the Cobra 1.

Standard MF only allows for one IP interface. With 4.2, GHI has added support to allow multiple interfaces to exist on
a premium device. This is why the initialization sequence has changed.

At the present time, only one interface can be active. There is talk about allowing more than one to be
active in the future.

Thanks @ Mike. I now understand your answer, but I still think the “standard” way of accessing the network interface should work. The standard method also allows for more than one interface to exist.


NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
if (interfaces != null && interfaces.Length > 0)
{
    NetworkInterface networkInterface = interfaces[0];
    - - -

With an OSHW device, the firmware is compiled to a specific hardware configuration. With the Premium devices, there is a bit more flexibility. For example, with the Spider you could use the built in PHY, or a SPI based Ethernet module. To use the standard interfaces they must have been initialized to specific hardware. I have not tried, but you might be able to use the standard interfaces once they have been assigned to an IP stack.