| Hans's profileHans Hinnekint's spacePhotosBlogLists | Help |
July 14 File Synchronization in C#Last week I started developing a small application that mimics the functionality of Microsoft's SyncToy. As this one stays in Beta, I decide to create one myself.
The reason for starting to develop this solution (besides the regular intellectual curiosity) is the different issues we face with using Windows XP's Offline Files when being connected to a slow network (latency > 50 millisecs)
It is based upon the Microsoft Synchronization Framework.
The Codeplex site can be found at http://www.codeplex.com/FileSynchronizer1 July 04 Service Programming in c#Last week, I have been developing a system that allows a user to start+run automatic or stop+disable a set of predefined services.
The user is logged in as a regular user.
The solution I came up with was to create a windows service that runs with local system rights. On top of it, I needed to create a GUI that allows a regular (read restricted) user to interact with this service. As I had never programmed windows services before, I was astonished to find that there does not exist a regular programming model to pass commands with parameters to the service.
References on the web claimed that this had to be performed by remoting.
OK, let's do remoting, I decided and as COM and DCOM seem to be currently out of favour, I opted to go for .Net Remoting.
I seemed to have some options as means for transport of the data:
I chose IPC as my GUI application resides on the same system as the service.
A great start for creating the service was as usual an article at the codeproject site: http://www.codeproject.com/csharp/WindowsService.asp
Now the skeleton was ready.
In the OnStart method of the service I added the code to create the IPCChannel.
To correct the error Failed to create an IPC Port: Access is denied, I added a hash table and grant everybody the right to consume the service.
protected override void OnStart(string[] args) {
} In the OnStop method of the service I added the reverse.
protected override void OnStop()
{ ChannelServices.UnregisterChannel(ChannelServices.RegisteredChannels[0]); base.OnStop(); } The actual Class that is created for Remoting is RemoteServiceManager, that implements the interface IRemoteServiceManager.
public interface IRemoteServiceManager
{ bool StopService(string TheService); bool StartService(string TheService); ServiceControllerStatus ServiceStatus(string TheService); string GetStartupType(string TheService); bool SetStartupType(string TheService, string TheStartupType); } public class RemoteServiceManager : MarshalByRefObject, IRemoteServiceManager Writing the code for starting and stopping the service and knowing the state of the service was easy as this functionality is implemented in the .NET framework class ServiceController.
In order to change the StartupType of a windows service, some code had to be created. I could use the ChangeServiceConfig function that exists in the advapi32.dll or use WMI. Calling the ChangeServiceConfig function in advapi32.dll from C# would have been an interesting exercise, but I wanted results and postponed delving in the InteropServikces.
Again I found an article at the codeproject site http://www.codeproject.com/csharp/extendservicecontroller.asp that listed an extension of the ServiceController class that provides this functionality by means of WMI.
In the GUI, I created 2 methods to interact with the service:
InitializeChannel sets up the communication with the IPCchannel.
private void InitializeChannel()
{ "ipc://" + WindowsService.WindowsService.TheChannelName + "/" + RemoteServiceManager.TheObjectName; IpcChannel ipcCh = new IpcChannel(WindowsService.WindowsService.TheChannelName + "Client"); ChannelServices.RegisterChannel(ipcCh, true); } GetRemoteObject returns the Interface to the obnject that resides in the context of the service.
private IRemoteServiceManager GetRemoteObject()
{ IRemoteServiceManager obj; obj = ( IRemoteServiceManager)Activator.GetObject( typeof(IRemoteServiceManager), TheChannelName); return obj;} In order to make the GUI application a little bit more flexible, I decided to implement a ConfigurationManager class, that loads the services to control from a .config file.
appSettings = ConfigurationManager.AppSettings;
The public method to stop and disable the services is this:
public void DisableTheServices()
{ IRemoteServiceManager obj = this.GetRemoteObject(); string TheService = ""; for (int i = 0; i < appSettings.Count; i++) { "Disabled"); } obj = null;} Some exception handling and eventlogging stuff, as listed in the Microsoft Enterprise Patterns, which can be found at http://msdn.microsoft.com/practices will have to be added to make it more robust.
I'm still looking for a way to share my code with you, but probably I will add it to the www.codeproject.com community. June 19 Offline FilesAfter extensive testing, I found the best way to work with offline files is NOT to use the slow link detection. As this is a feature that checks the throughput to the servers from which we made files available, this has a side effect that group-shares (which are not to be taken offline) can not be accessed wehenever the systems think our connection is to slow. The current way we handle this is to use a batch-script that allows a user to put himself offline. This is the script (it can be improved upon): @rem COMMENT: Work Offline with My Documents and other Offline Files @rem========================================================================= @echo off cls rem ------------------------------------------------------------------------- if not exist csccmd.exe goto :Noccssmd rem ------------------------------------------------------------------------- echo Enumerating cached shares on machine csccmd /enum | findstr /i \\ > %temp%\%computername%_enum.txt if not errorlevel 0 goto :NoEnum echo Done Enumerating shares echo. rem ------------------------------------------------------------------------- echo Disconnecting all cached shares for /f %%a in (%temp%\%computername%_enum.txt) do csccmd /disconnect:%%a if not errorlevel 0 goto :NoDisconnect echo Done Disconnecting Shares echo. goto :end rem ------------------------------------------------------------------------- :Noccssmd echo csccmd Executable not found echo. Pause goto :Done rem ------------------------------------------------------------------------- :NoEnum echo Error Disconnecting Shares echo. goto :Done rem ------------------------------------------------------------------------- :NoDisconnect echo Error Disconnecting Shares echo. goto :Done rem ------------------------------------------------------------------------- :end echo You are Offline rem ------------------------------------------------------------------------- :Done June 18 Microsoft Hosting DayThis week, I went to the Belgium Microsoft Hosting Day.
It was the first time I joined this event. As I'm not a hosting provider, it was interesting to see the efforts Microsoft is putting into converting Linux hosters to the Windows platform and at the same time rewriting a lot of server software to make it more appropriate for hosting.
It was a little unclear on how Microsoft could sell its direct competition to the hosters, especially on hosted exchange.
One interesting thing was that Sharepoint seems to be a likely candidate for hosting.
How this interacts with customers having their own Active Directory is something I will investigate soon. |
|
|||||
|
|