/// <summary>
/// Saves the endpoint information in the registry.
/// </summary>
public static void Save(ConfiguredEndpoint endpoint)
{
if (endpoint == null)
{
throw new ArgumentNullException("endpoint");
}
if (endpoint.ComIdentity == null)
{
throw new ApplicationException("Endpoint does not have a COM identity specified.");
}
// choose the clsid for the host process.
Guid hostClsid = GetServerHostClsid(endpoint.ComIdentity.Specification);
// check of COM host process registered.
string wrapperPath = ConfigUtils.GetExecutablePath(hostClsid);
if (String.IsNullOrEmpty(wrapperPath))
{
throw new ApplicationException("The UA COM Host process is not registered on the machine.");
}
// verify prog id.
string progId = endpoint.ComIdentity.ProgId;
if (String.IsNullOrEmpty(progId))
{
throw new ApplicationException("Endpoint does not have a valid ProgId.");
}
// remove existing CLSID.
Guid existingClsid = ConfigUtils.CLSIDFromProgID(progId);
if (existingClsid != Guid.Empty)
{
ConfigUtils.UnregisterComServer(existingClsid);
}
// determine CLSID to use.
Guid clsid = endpoint.ComIdentity.Clsid;
if (clsid == Guid.Empty)
{
clsid = existingClsid;
if (clsid == Guid.Empty)
{
clsid = Guid.NewGuid();
}
endpoint.ComIdentity.Clsid = clsid;
}
// remove existing clsid.
ConfigUtils.UnregisterComServer(clsid);
string clsidKey = String.Format(@"CLSID\{{{0}}}", clsid.ToString().ToUpper());
// create new entries.
RegistryKey key = Registry.ClassesRoot.CreateSubKey(clsidKey);
if (key == null)
{
throw new ApplicationException("Could not create key: " + clsidKey);
}
// save description.
if (endpoint.Description.Server.ApplicationName != null)
{
key.SetValue(null, endpoint.Description.Server.ApplicationName);
}
try
{
// create local server key.
RegistryKey subkey = key.CreateSubKey("LocalServer32");
if (subkey == null)
{
throw new ApplicationException("Could not create key: LocalServer32");
}
subkey.SetValue(null, wrapperPath);
subkey.Close();
// create prog id key.
subkey = key.CreateSubKey("ProgId");
if (subkey == null)
{
throw new ApplicationException("Could not create key: ProgId");
}
subkey.SetValue(null, progId);
subkey.Close();
}
finally
{
key.Close();
}
// save the configuration.
SaveConfiguredEndpoint(clsid, endpoint);
// create prog id key.
key = Registry.ClassesRoot.CreateSubKey(progId);
if (key == null)
{
throw new ApplicationException("Could not create key: " + progId);
}
try
{
// create clsid key.
RegistryKey subkey = key.CreateSubKey("CLSID");
if (subkey == null)
{
throw new ApplicationException("Could not create key: CLSID");
}
subkey.SetValue(null, String.Format("{{{0}}}", clsid.ToString().ToUpper()));
subkey.Close();
// create the OPC key use with DA 2.0 servers.
if (endpoint.ComIdentity.Specification == ComSpecification.DA)
{
subkey = key.CreateSubKey("OPC");
if (subkey == null)
{
throw new ApplicationException("Could not create key: OPC");
}
subkey.Close();
}
}
finally
{
key.Close();
}
// register as wrapper server.
ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_PseudoComServers, "OPC UA COM Pseudo-Servers");
// register in OPC component categories.
if (endpoint.ComIdentity.Specification == ComSpecification.DA)
{
ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_OPCDAServer20);
#if !OPCUA_NODA3_SUPPORT
ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_OPCDAServer30);
#endif
}
else if (endpoint.ComIdentity.Specification == ComSpecification.AE)
{
ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_OPCAEServer10);
}
else if (endpoint.ComIdentity.Specification == ComSpecification.HDA)
{
ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_OPCHDAServer10);
}
}