I have a site that requires to check the existence of a node from with in a zone. If it does not exist then I create that node.
This is for a site that allows logged in users create a sub name like :: MyCompany.EriksNewSite.Com. This way it looks a little more personalized.
----------
The service i wrote is fairly straight forward but getting the correct data back out of the stream from the response of a dns command is a B**CH to say the least.
Service:
----
public class AFCCDnsManager : MarshalByRefObject, AFCCDns.IAFCCDnsManager
{
public AFCCDnsManager()
{
}
/// <summary>
/// Checks to see if a zone contains a particular node
/// </summary>
/// <param name="server">dns server ( use ip when possible )</param>
/// <param name="zone">domain name that contains the node</param>
/// <param name="node">the prefix node in question</param>
/// <returns>true if has records</returns>
public bool CheckIfDomainZoneNodeHasRecords(string server, string zone, string node)
{
//
Process myProcess = null;
ProcessStartInfo myProcessStartInfo = null;
StreamReader myStreamReader = null;
string cmdFailed = string.Empty;
string dnsCmd = string.Empty;
StringBuilder output = null;
try
{
cmdFailed = "DNS Server failed";
//string cmdCompleted = "command completed successfully";
//Command failed: DNS_ERROR_NAME_DOES_NOT_EXIST 9714
//cmd server cmd zone node
//dnscmd afcc-inc-ns1 /enumrecords AFCCINC.COM handlers
dnsCmd = string.Format("dnscmd {0} /enumrecords {1} {2}", server, zone, node);
output = new StringBuilder();
myProcess = new Process();
myProcessStartInfo =
new ProcessStartInfo("cmd.exe");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.CreateNoWindow = true;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.RedirectStandardInput = true;
myProcessStartInfo.Arguments = dnsCmd;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
myStreamReader = myProcess.StandardOutput;
do
{
output.Append(myStreamReader.ReadLine() + "\n");
} while (myStreamReader.Peek() >= 0);
myProcess.StandardInput.WriteLine(dnsCmd);
do
{
output.Append(myStreamReader.ReadLine());
} while (myStreamReader.Peek() >= 0);
myProcess.StandardInput.WriteLine("\n");
do
{
output.Append(myStreamReader.ReadLine());
} while (myStreamReader.Peek() >= 0);
myProcess.StandardInput.WriteLine("\n");
do
{
output.Append(myStreamReader.ReadLine());
} while (myStreamReader.Peek() >= 0);
myProcess.StandardInput.WriteLine("\n");
do
{
output.Append(myStreamReader.ReadLine());
} while (myStreamReader.Peek() >= 0);
do
{
output.Append(myStreamReader.ReadLine());
} while (myStreamReader.Peek() >= 0);
myStreamReader.Close();
myProcess.Close();
Console.WriteLine(output.ToString());
if (output.ToString().ToLower().Contains(cmdFailed.ToLower()))
return false;//0
return true;
}
catch (Exception ex)
{
System.Net.Mail.MailMessage mm = null;
SmtpClient smtp = null;
mm =
new System.Net.Mail.MailMessage
(new System.Net.Mail.MailAddress("problems@afccDnsManager.com"),
new System.Net.Mail.MailAddress("support@xxxx.com"));
//
mm.Subject = "Problems with the AFCC Dns Manager Service";
mm.Body = "Message: " + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine;
mm.Body += "Source : " + Environment.NewLine + ex.Source;
smtp = new SmtpClient("smtp.afccinc.com");
smtp.Send(mm);
mm = null;
smtp = null;
//
return true;
}
finally
{
myProcess = null;
myProcessStartInfo = null;
myStreamReader = null;
cmdFailed = string.Empty;
dnsCmd = string.Empty;
output = null;
}
}
-----------
From web app
using System;
namespace AFCCDns
{
public interface IAFCCDnsManager
{
bool CheckIfDomainZoneNodeHasRecords(string server, string zone, string node);
}
}
----------------\
bool hasRecords = true;
//select channel to communicate with server
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
AFCCDns.IAFCCDnsManager remObject =
(AFCCDns.IAFCCDnsManager)RemotingServices.Connect
(typeof(AFCCDns.IAFCCDnsManager),
"tcp://111.11.29.111:8875/AFCCDnsQuerry");//111.11.29.111:8875
if (remObject == null)
Console.WriteLine("cannot locate server");
else
{
hasRecords = remObject.CheckIfDomainZoneNodeHasRecords("111.11.29.111", "HomeBuildersBlog.Com", "MyCompanyName");
Console.WriteLine(hasRecords);
}
return hasRecords;
}
I am needing to querry the DNS server for records.
Basically if there is a way MS can give us a object that will have the same commands as the DNS command .exe then we are good. This is what we are in need of. BIG TIME!