Replicating a Server
Server configurations can be exported to an XML file to allow easy setup of multiple servers with similar settings. The file contains all of the settings for the server, including all of the publishing point and plug-in configurations. After the configuration file has been exported, it can simply be put in place of another server configuration file or it can be copied to the proper location through scripting or code. To use a configuration file to replicate a server, you must perform the following steps. The accompanying examples illustrate how to programmatically export and import a configuration.
Export the server configuration to an XML file. See the examples later in this section.
Stop Windows Media Services on the computer whose configuration will be updated. This can be accomplished through the MMC console, programmatically through code, or through a command console by typing "net stop wmserver".
Copy the XML file created in step 1 into the Windows Media Services directory, overwriting the old configuration file located at %systemroot%\system32\windows media\server\ServerNamespace.xml.
Delete the NameSpaceDelta.xml file if it exists. This file is located in the same directory as the ServerNameSpace.xml file.
Restart Windows Media Services by typing "net start wmserver" in a command console, by using an administrative tool, or programmatically.
Change the user name and password of any anonymous authentication plug-in that impersonates a local user account. For example, when you installed Windows Media Services on the computer that you are exporting the configuration from, the WMS Anonymous User Authentication plug-in was configured to use the WMUS_ComputerName account and a randomly generated password. When you import the configuration, the user name and password will not be valid. If you do not change them, the plug-in will enter an error state, and the server will not be able to stream content.
If any third-party plug-ins were installed on the computer that you are exporting the configuration from, you must also install those plug-ins on the computer you are exporting the configuration to.
If the WMS HTTP Server Control Protocol plug-in is bound to a specific IP address on the computer that you are exporting the configuration from, you must bind it to the IP address of the computer you are exporting the configuration to.
If a publishing point in the original configuration specified a user name and password for distribution servers that request authentication, you must reset the credentials on the computer that you are exporting the configuration to. See the Credentials property of the publishing point in the Windows Media Services MMC.
If the WMS Network Data Source plug-in specifies proxy credentials for the HTTP or RTSP protocols on the computer you are exporting the configuration from, you must reset them on the computer you are exporting the configuration to.
If the WMS Publishing Points ACL Authorization plug-in on the computer you are exporting the configuration from specifies ACLs that contain local user accounts, you must reset these on the computer you are exporting the configuration to.
If the WMS Multicast Data Writer plug-in is bound to a specific IP address on the computer you are exporting the configuration from, reset it to the IP address of a network interface card on the computer you are exporting the configuration to.
The following examples illustrate how to export a server configuration.
Visual Basic .NET Example
Imports Microsoft.WindowsMediaServices.Interop
Imports System.Runtime.InteropServices
' Declare variables.
Dim Server As WMSServer
Try
' Create the WMSServer object.
Server = New WMSServer()
' Export the server configuration to an XML file.
Server.ExportXML("c:\temp\ServerNamespace.xml")
Catch errCom As COMException
' TODO: Handle COM exceptions.
Catch err As Exception
' TODO: Exception handler goes here.
Finally
' TODO: Clean-up code goes here.
End Try
C# Example
using Microsoft.WindowsMediaServices.Interop;
using System.Runtime.InteropServices;
WMSServer Server;
try
{
// Create the WMSServer object.
Server = new WMSServerClass();
// Export the server configuration to an XML file.
Server.ExportXML("c:\\temp\\ServerNamespace.xml");
}
catch (COMException comExc)
{
// TODO: Handle COM exceptions.
}
catch (Exception exc)
{
// TODO: Exception handler goes here.
}
finally
{
// TODO: Clean-up code goes here.
}
C++ Example
#include <windows.h>
#include <atlbase.h> // Includes CComBSTR.
#include "wmsserver.h"
// Declare variables and interfaces.
IWMSServer *pServer;
HRESULT hr;
CComBSTR bstrFile;
// Initialize the COM library and retrieve a pointer
// to an IWMSServer interface.
hr = CoInitialize(NULL);
hr = CoCreateInstance(CLSID_WMSServer,
NULL,
CLSCTX_ALL,
IID_IWMSServer,
(void **)&pServer);
if (FAILED(hr)) goto EXIT;
// Export the server configuration to an XML file.
bstrFile = "c:\\dir\\ServerNamespace.xml";
hr = pServer->ExportXML(bstrFile);
if (FAILED(hr)) goto EXIT;
EXIT:
// TODO: Release temporary COM objects and uninitialize COM.
The following examples illustrate how to import a server configuration.
Visual Basic .NET Example
Imports System.IO
Imports System.Management
' Declare variables.
Dim FSO
Dim WMI
Dim WMIServices
Dim WMIService
Dim strServerPath
Dim Folder
Try
' Retrieve the Windows Management Instrumentation object.
WMI = GetObject("WinMgmts:")
' Query the media server from the list of system services.
WMIServices = WMI.ExecQuery("select * from win32_service" & _
" where name='WMServer'")
' Stop the media server.
For Each WMIService In WMIServices
WMIService.stopService()
Next
' Create the FileSystemObject.
FSO = CreateObject("Scripting.FileSystemObject")
' Retrieve the location of the Windows directory and
' add the path to the server configuration file.
strServerPath = FSO.GetSpecialFolder(0).Path & _
"\system32\Windows Media\Server\ServerNamespace.xml"
' Copy the new configuration file over the old one.
FSO.CopyFile("c:\dir\NewServerConfig.xml", strServerPath)
' Restart the media server.
For Each WMIService In WMIServices
WMIService.startService()
Next
Catch Err As Exception
' TODO: Exception handler goes here.
Finally
' TODO: Clean-up code goes here.
End Try
C# Example
using System.IO;
using System.Management;
using Microsoft.WindowsMediaServices.Interop;
// Declare variables.
ConnectionOptions oConn;
ManagementScope oMS;
ObjectQuery oQuery1, oQuery2;
ManagementObjectSearcher oSearcher1, oSearcher2;
ManagementObjectCollection oReturnCollection1, oReturnCollection2;
WMSServer Server;
string strServerName="";
string strServerPath="";
try
{
// Initialize the local server.
Server = new WMSServerClass();
strServerName = Server.Name;
// Set the WMI connection options.
oConn = new ConnectionOptions();
oMS = new System.Management.ManagementScope("\\\\" +
strServerName + "\"", oConn);
// Use WMI to retrieve the WMServer service
// and then stop the service.
oQuery1 = new
System.Management.ObjectQuery("select * from Win32_Service where Name=\"WMServer\"");
oSearcher1 = new ManagementObjectSearcher(oMS, oQuery1);
oReturnCollection1 = oSearcher1.Get();
foreach (ManagementObject mo in oReturnCollection1)
{
mo.InvokeMethod("stopService", null);
}
// Use WMI to retrieve the location of the Windows directory
// on the server and then add the path to the server configuration file.
oQuery2 = new
System.Management.ObjectQuery("select windowsDirectory from win32_OperatingSystem");
oSearcher2 = new ManagementObjectSearcher(oMS, oQuery2);
oReturnCollection2 = oSearcher2.Get();
foreach (ManagementObject mo in oReturnCollection2)
{
strServerPath = mo.GetPropertyValue("WindowsDirectory") +
"\\system32\\Windows Media\\Server\\ServerNamespace.xml";
}
// Copy the new configuration file over the old one.
// (The last argument indicates that the target file should be
// overwritten if it exists.)
System.IO.File.Copy("c:\\temp\\NewServerConfig.xml", strServerPath, true);
// Start the media server.
oQuery1 = new
System.Management.ObjectQuery("select * from Win32_Service where Name=\"WMServer\"");
oSearcher1 = new ManagementObjectSearcher(oMS, oQuery1);
oReturnCollection1 = oSearcher1.Get();
foreach (ManagementObject mo in oReturnCollection1)
{
mo.InvokeMethod("startService", null);
}
}
catch (Exception exc)
{
// TODO: Exception handler goes here.
}
finally
{
// TODO: Clean-up code goes here.
}
}
}
C++ Example
#include <windows.h>
#include "wmsserver.h"
// Declare variables.
HRESULT hr;
SC_HANDLE hSCM;
SC_HANDLE hService;
SERVICE_STATUS ssStatus;
DWORD dwStartTime;
char strFile[MAX_PATH];
char strServerPath[MAX_PATH];
// Open a connection to the service control manager.
hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
// Open the Windows Media service.
hService = OpenService(hSCM, "WMServer", SERVICE_STOP | SERVICE_START |
SERVICE_QUERY_STATUS);
// The media server must be shut down if it is currently running
// before the new configuration can be imported.
dwStartTime = GetTickCount();
// Be sure the service is not already stopped.
hr = QueryServiceStatus(hService, &ssStatus);
if (FAILED(hr)) goto EXIT;
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
goto SUCCESS;
// If a stop is pending, wait for it.
while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
Sleep(ssStatus.dwWaitHint);
hr = QueryServiceStatus(hService, &ssStatus);
if (FAILED(hr)) goto EXIT;
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
goto SUCCESS;
// If for some reason the service hangs, the timeout
// value will prevent an infinite loop.
if (GetTickCount() - dwStartTime > MAX_TIMEOUT)
goto EXIT;
}
// Send a stop code to the service.
hr = ControlService(hService, SERVICE_CONTROL_STOP, &ssStatus);
if (FAILED(hr)) goto EXIT;
// Wait for the service to stop.
while (ssStatus.dwCurrentState != SERVICE_STOPPED)
{
Sleep(ssStatus.dwWaitHint);
hr = QueryServiceStatus(hService, &ssStatus);
if (FAILED(hr)) goto EXIT;
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
break;
if (GetTickCount() - dwStartTime > MAX_TIMEOUT)
goto EXIT;
}
// The media server has been stopped; now copy
// the new configuration file over the existing one.
SUCCESS:
// Retrieve the system Windows directory.
hr = GetWindowsDirectory(strServerPath, MAX_PATH);
if (FAILED(hr)) goto EXIT;
// Concatenate the Windows directory with the server
// configuration file location.
strcat_s(strServerPath, MAX_PATH,
"\\system32\\Windows Media\\Server\\ServerNamespace.xml");
// Prepare the new configuartion file to be copied.
strcpy_s(strFile, MAX_PATH, "c:\\dir\\NewServerConfig.xml");
// Copy the new file, overwriting the old one.
hr = CopyFile(strFile, strServerPath, FALSE);
if (FAILED(hr)) goto EXIT;
// The new server configuration file has been put
// in place; now start the media server running again.
dwStartTime = GetTickCount();
// Send a start code to the service.
hr = StartService(hService, 0, NULL);
if (FAILED(hr)) goto EXIT;
// Wait for the service to start.
while (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
Sleep(ssStatus.dwWaitHint);
hr = QueryServiceStatus(hService, &ssStatus);
if (FAILED(hr)) goto EXIT;
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
break;
if (GetTickCount() - dwStartTime > MAX_TIMEOUT)
return;
}
EXIT:
// TODO: Release temporary COM objects and uninitialize COM.
See Also
Reference
IWMSServer Object (Visual Basic .NET)