3

I wrote an application using a webservice and I want to simulate a network failure for test purposes. I know I could turn off the network manually, but it would be awesome if it would be automatically.

I tried the solution from: How to simulate network failure for test purposes (in C#)? from Larsenal but it doesn't recognize the ManagementClass/ObjectCollection/... and I don't know why (i used System.Managment.Man... and it still didn't work. I imported the required references - didn't work. I have no idea what I am doing wrong)

It should work something like this:

[TestMethod]
public void Service_Login_NoInternetConnection()
{
  // Some code...
  TurnOffNetworkConnection();
  // More code...
  TurnOnNetworkConnection();
  // Blablabla code...
}
Community
  • 1
  • 1
  • How does your code connect across the network? You could just potentially route your code to `0.0.0.0` and see how it handles that.. the idea being that network failure simulation doesn't actually have to mean disabling the network interface .. just breaking the address its going to. – Simon Whitehead Aug 14 '14 at 23:29
  • The problem is: I am using an very lousy webservice made by the government of my country. It's not documented, written in German and English at the same time and I don't want to manipulate it, because it is an miracle that it works ^^ But thanks for the answer ^^ when there is no better solution I will/must try it :) – MrApfelstrudel Aug 14 '14 at 23:40
  • do you access the web service via an interface? – lockstock Aug 15 '14 at 00:10
  • Yes, its a WSDL document converted to C#. I wrote an 'Service' using the interface, because the original service is (sorry) shit. Instead of throwing an network exception it throws an System.Exception and you have to read the message to know it is a network exception... WSDL documents: https://finanzonline.bmf.gv.at/fon/services/SessionWSI/wsdl/SessionWSIService.wsdl and https://finanzonline.bmf.gv.at/fon/ws/uidAbfrageService.wsdl – MrApfelstrudel Aug 15 '14 at 00:25

1 Answers1

1

You can use WMI for it.

First make sure you add reference : System.Management

Then I get all devices with :
"ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration");"

Now i need to check if a device got DHCPLeaseObtained.
So I use foreach to check every network device in the searcher :
String Check = Convert.ToString(queryObj["DHCPLeaseObtained"]);

If the device has no DHCPLeaseObtained the string will be emty. So I check if the string is emty :
if (String.IsNullOrEmpty(Check))

Then you can use ReleaseDHCPLease and RenewDHCPLease in the else.
ManagementBaseObject outParams = queryObj.InvokeMethod("ReleaseDHCPLease", null, null);
or
ManagementBaseObject outParams = queryObj.InvokeMethod("RenewDHCPLease", null, null);

using System.Management;


    public void TurnOnNetworkConnection()
{

        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration");

               foreach (ManagementObject queryObj in searcher.Get())
               {
                  String Check = Convert.ToString(queryObj["DHCPLeaseObtained"]);
                  if (String.IsNullOrEmpty(Check))
                    {
                    }
                    else
                    {
                    ManagementBaseObject outParams = queryObj.InvokeMethod("RenewDHCPLease", null, null);
                    }
               }
         }
           catch (ManagementException e)
           {
           MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
           }
}

    public void TurnOffNetworkConnection()
{
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration"); 

            foreach (ManagementObject queryObj in searcher.Get())
            {
                String Check = Convert.ToString(queryObj["DHCPLeaseObtained"]);
                if (String.IsNullOrEmpty(Check)) 
                {
                }
                else
                {
                    ManagementBaseObject outParams = queryObj.InvokeMethod("ReleaseDHCPLease", null, null);
                }
            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
        }
}
Creator
  • 1,502
  • 4
  • 17
  • 30