I created a Windows Forms application that uses a ChromiumBrowser. The application is composed by the follow components:
Main application
Web browser library
Launcher application
When I launch my application normally, the web browser works correctly. If I launch my application from a launcher, the web browser doesn't work. It tolds me the follow error:
Unhandled exception of 'System.IO.FileNotFoundException' in Unknown module.
Cannot load file or assembly 'CefSharp, Version=57.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138' or a relative dependency.
Unable to found the specified file.
I need to use the launcher not only for updates, but since the application is distributed on network, sometimes there are problems to access files on the server.
The problem is not relative only to my app. I created a test solution that I posted below and I have had the same issue.
Notes on the project
Cefsharp runtime is located in C:\Program Files (x86)\CEFRuntime\x64 and C:\Program Files (x86)\CEFRuntime\x86. (I created an installer that copy the runtime files in this position). Runtime is based on the NuGet package.
All executables are compiled in AnyCpu (AnyCpu support)
Cefsharp version 57 (Cef redist 3.2987.1601)
Runtime content
x64 folder
cef.pak
CefSharp.BrowserSubprocess.Core.dll
CefSharp.BrowserSubprocess.Core.pdb
CefSharp.BrowserSubprocess.exe
CefSharp.BrowserSubprocess.pdb
CefSharp.Core.dll
CefSharp.Core.pdb
CefSharp.Core.xml
CefSharp.dll
CefSharp.pdb
CefSharp.WinForms.dll
CefSharp.WinForms.pdb
CefSharp.WinForms.XML
CefSharp.XML
cef_100_percent.pak
cef_200_percent.pak
cef_extensions.pak
chrome_elf.dll
d3dcompiler_47.dll
devtools_resources.pak
icudtl.dat
libcef.dll
libEGL.dll
libGLESv2.dll
natives_blob.bin
snapshot_blob.bin
widevinecdmadapter.dll
locales folder (with all .paks)
x86 folder
cef.pak
CefSharp.BrowserSubprocess.Core.dll
CefSharp.BrowserSubprocess.Core.pdb
CefSharp.BrowserSubprocess.exe
CefSharp.BrowserSubprocess.pdb
CefSharp.Core.dll
CefSharp.Core.pdb
CefSharp.Core.xml
CefSharp.dll
CefSharp.pdb
CefSharp.WinForms.dll
CefSharp.WinForms.pdb
CefSharp.WinForms.XML
CefSharp.XML
cef_100_percent.pak
cef_200_percent.pak
cef_extensions.pak
chrome_elf.dll
d3dcompiler_47.dll
devtools_resources.pak
icudtl.dat
libcef.dll
libEGL.dll
libGLESv2.dll
natives_blob.bin
snapshot_blob.bin
widevinecdmadapter.dll
locales folder (with all .paks)
I post the test solution that gives me the same error.
Test Solution
The test solution is composed by three projects:
StackOverflowIssueLauncher
StackOverflowIssue (reference to WebBrowser)
WebBrowser (dll library that contains the webbrowser)
The code is shown below:
Project StackOverflowIssueLauncher
Program.cs
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace StackOverflowIssueLauncher {
/// <summary>
/// Launcher program
/// </summary>
internal static class Program {
/// <summary>
/// Launcher body
/// </summary>
[STAThread, LoaderOptimization(LoaderOptimization.MultiDomainHost)]
private static void Main() {
//Initialize path of application
string startupPath = Environment.CurrentDirectory;
string cachePath = Path.Combine(Path.GetTempPath(), "Program-" + Guid.NewGuid());
string assemblyPath = CanonicalizePathCombine(startupPath, #"..\..\..\StackOverflowIssue\bin\Debug\");
string executablePath = Path.Combine(assemblyPath, "StackOverflowIssue.exe");
string configFile = executablePath + ".config";
//Start App Domain
try {
var setup = new AppDomainSetup() {
ApplicationName = "StackOverflowIssue",
ShadowCopyFiles = "true",
ShadowCopyDirectories = assemblyPath,
CachePath = cachePath,
ConfigurationFile = configFile
};
var domain = AppDomain.CreateDomain("StackOverflowIssue", AppDomain.CurrentDomain.Evidence, setup);
domain.ExecuteAssembly(executablePath);
AppDomain.Unload(domain);
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//Empty cache path
try {
Directory.Delete(cachePath, true);
}
catch (Exception) {
//DO NOTHING
}
}
private static string CanonicalizePathCombine(string sourcePath, string destPath) {
string resultPath = Path.Combine(sourcePath, destPath);
var sb = new StringBuilder(Math.Max(260, 2 * resultPath.Length));
PathCanonicalize(sb, resultPath);
return sb.ToString();
}
[DllImport("shlwapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool PathCanonicalize([Out] StringBuilder sb, string src);
}
}
Project StackOverflowIssue
WebControlForm.cs
using System.Windows.Forms;
using WebBrowser;
namespace StackOverflowIssue {
/// <summary>
/// Form that contains the webbrowser control
/// </summary>
public class WebControlForm : Form {
/// <summary>
/// Create a new web control form
/// </summary>
public WebControlForm() {
InitializeComponent();
Controls.Add(new CefControl { Dock = DockStyle.Fill });
}
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.SuspendLayout();
//
// WebControlForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(529, 261);
this.Name = "WebControlForm";
this.Text = "WebControlForm";
this.ResumeLayout(false);
}
#endregion
}
}
MainForm.cs
using System;
using System.Windows.Forms;
namespace StackOverflowIssue {
/// <summary>
/// Main application form
/// </summary>
public partial class MainForm : Form {
/// <summary>
/// Creates the main form
/// </summary>
public MainForm() {
InitializeComponent();
}
/// <summary>
/// Show a new Web Control form
/// </summary>
/// <param name="sender">Object that raised the event</param>
/// <param name="e">Event arguments</param>
private void ShowBtn_Click(object sender, EventArgs e) {
var wcf = new WebControlForm();
wcf.Show(this);
}
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
/// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Codice generato da Progettazione Windows Form
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent() {
this.ShowBtn = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// ShowBtn
//
this.ShowBtn.Location = new System.Drawing.Point(12, 12);
this.ShowBtn.Name = "ShowBtn";
this.ShowBtn.Size = new System.Drawing.Size(134, 40);
this.ShowBtn.TabIndex = 0;
this.ShowBtn.Text = "Show web browser";
this.ShowBtn.UseVisualStyleBackColor = true;
this.ShowBtn.Click += new System.EventHandler(this.ShowBtn_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.ShowBtn);
this.Name = "MainForm";
this.Text = "Main form";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button ShowBtn;
}
}
Program.cs
using System;
using System.Diagnostics;
using System.Windows.Forms;
using WebBrowser;
namespace StackOverflowIssue {
/// <summary>
/// Main application program
/// </summary>
internal static class Program {
/// <summary>
/// Main application program.
/// </summary>
[STAThread] private static void Main() {
WebBrowserInitializer.Initialize();
Debug.Print("Application started");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="cef.redist.x64" version="3.2987.1601" targetFramework="net452" />
<package id="cef.redist.x86" version="3.2987.1601" targetFramework="net452" />
<package id="CefSharp.Common" version="57.0.0" targetFramework="net452" />
<package id="CefSharp.WinForms" version="57.0.0" targetFramework="net452" />
</packages>
Project WebBrowser
CefControl.cs
using System.Windows.Forms;
using CefSharp.WinForms;
namespace WebBrowser {
/// <summary>
/// WebBrowser control
/// </summary>
public class CefControl: UserControl {
public CefControl() {
CefInitializer.Initialize();
InitializeComponent();
var cr = new ChromiumWebBrowser("https://www.google.com");
cr.Dock = DockStyle.Fill;
Controls.Add(cr);
}
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
/// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Codice generato da Progettazione componenti
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
}
#endregion
}
}
CefInitializer.cs
using CefSharp;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace WebBrowser {
/// <summary>
/// Class that contains the base methods for CEF initializations
/// </summary>
public static class CefInitializer {
/// <summary>
/// Initialize properties
/// </summary>
static CefInitializer() {
CachePath = Path.Combine(Path.GetTempPath(), "SOIssue", "Cache");
LogFile = Path.Combine(Path.GetTempPath(), "SOIssue", "Logs");
UserDataPath = Path.Combine(Path.GetTempPath(), "SOIssue", "Data");
if (!Directory.Exists(CachePath))
Directory.CreateDirectory(CachePath);
if (!Directory.Exists(LogFile))
Directory.CreateDirectory(LogFile);
if (!Directory.Exists(UserDataPath))
Directory.CreateDirectory(UserDataPath);
//Complete the files combine
LogFile = Path.Combine(LogFile, "WebBrowser.log");
AppDomain.CurrentDomain.DomainUnload += (sender, args) => Shutdown();
}
/// <summary>
/// Shutdown all CEF instances
/// </summary>
internal static void Shutdown() {
using (var syncObj = new WindowsFormsSynchronizationContext()) {
syncObj.Send(o => {
if (Cef.IsInitialized)
Cef.Shutdown();
}, new object());
}
}
/// <summary>
/// Initialize CEF libraries
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)] internal static void Initialize() {
if (Cef.IsInitialized)
return;
//Get proxy properties
WebProxy proxy = WebRequest.DefaultWebProxy as WebProxy;
string cefPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Cef)).Location);
Debug.Print($"CEF Library Path: {cefPath}");
Debug.Assert(cefPath != null, nameof(cefPath) + " != null");
var settings = new CefSettings() {
BrowserSubprocessPath = Path.Combine(cefPath, "CefSharp.BrowserSubprocess.exe"),
LocalesDirPath = Path.Combine(cefPath, "locales"),
ResourcesDirPath = cefPath,
Locale = CultureInfo.CurrentCulture.Name,
CachePath = CachePath,
LogFile = LogFile,
UserDataPath = UserDataPath
};
if (proxy == null || proxy.Address.AbsoluteUri != string.Empty)
settings.CefCommandLineArgs.Add("no-proxy-server", string.Empty);
Cef.Initialize(settings);
}
internal static readonly string CachePath;
internal static readonly string LogFile;
internal static readonly string UserDataPath;
}
}
WebBrowserInitializer.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace WebBrowser {
/// <summary>
/// Class that contains the assembly resolve functions
/// </summary>
public static class WebBrowserInitializer {
private static readonly object _initializer = new object();
private static bool _initialized;
/// <summary>
/// Check if the WebBrowser is initialized
/// </summary>
public static bool IsInitialized {
get {
lock (_initializer)
return _initialized;
}
}
/// <summary>
/// Initialize the current assembly
/// </summary>
public static void Initialize() {
lock (_initializer) {
if (!_initialized) {
AppDomain.CurrentDomain.AssemblyResolve += CefSharp_AssemblyResolve;
_initialized = true;
}
}
}
/// <summary>
/// Try to resolve the assembly
/// </summary>
/// <param name="sender">Object that has raised the event</param>
/// <param name="args">Event raised</param>
/// <returns>Assembly loaded</returns>
private static Assembly CefSharp_AssemblyResolve(object sender, ResolveEventArgs args) {
Debug.Print($"Library: {args.Name}");
if (!args.Name.StartsWith("CefSharp", StringComparison.OrdinalIgnoreCase))
return null;
string assemblyName = args.Name.Split(new[] {','}, 2)[0] + ".dll";
foreach (var path in GetAssemblyPaths()) {
string checkPath = Path.Combine(path, assemblyName);
if (File.Exists(checkPath)) {
Debug.Print($"Relative path FOUND for {args.Name} in {checkPath}");
return Assembly.UnsafeLoadFrom(checkPath);
}
Debug.Write($"Relative path not found for {args.Name} in {checkPath}");
}
return null;
}
/// <summary>
/// Get all possible assembly paths
/// </summary>
/// <returns>List of possible assembly paths</returns>
private static IEnumerable<string> GetAssemblyPaths() {
string pathPrefix = Environment.Is64BitProcess ? "x64" : "x86";
if (Directory.Exists(#"C:\Program Files (x86)\CEFRuntime\" + pathPrefix))
yield return #"C:\Program Files (x86)\CEFRuntime\" + pathPrefix;
yield return Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, pathPrefix);
yield return Path.Combine(Environment.CurrentDirectory, pathPrefix);
Assembly currentAssembly = Assembly.GetAssembly(typeof(CefInitializer));
if (!string.IsNullOrEmpty(currentAssembly.Location))
yield return Path.Combine(currentAssembly.Location, pathPrefix);
}
}
}
packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="cef.redist.x64" version="3.2987.1601" targetFramework="net452" />
<package id="cef.redist.x86" version="3.2987.1601" targetFramework="net452" />
<package id="CefSharp.Common" version="57.0.0" targetFramework="net452" />
<package id="CefSharp.WinForms" version="57.0.0" targetFramework="net452" />
</packages>
Looking at CefSharp General Usage (https://github.com/cefsharp/CefSharp/wiki/General-Usage#need-to-knowlimitation), I noticed a line that explains that CefSharp works only on default AppDomain. I have looked at the project https://github.com/stever/AppHostCefSharp and I found a solution.
I need to run the WebBrowser on a Default AppDomain (I forked and edited RedGate.AppHost repository. See below for why i did it.). To allow communications between controls I implements two NamedPipes services, one on the main form, another on the created object.
I posted the complete solution (https://github.com/rupertsciamenna89/cefsharp-remoting) so the source code will be simplier to see. And it can be improved or fixed (like my english :))
I renamed the original projects to better names.
The solution is composed of 4 projects:
MainApplication [old StackOverflowIssue] (base application that I have to launch with ShadowCopy)
MainApplication.Launcher [old StackOverflowIssue.Launcher] (application launcher)
MainApplication.WebBrowser [old WebBrowser] (winforms controls library that contains the WebBrowser)
MainApplication.Interfaces (interfaces that must be implemented for the operations)
MainApplication.Interfaces
This project contains the interfaces that must be implemented by the client and the server. It contains five files:
IFormService is the interface that allows to create the control throught the RedGate.AppHost. It contains two Guids that identifies the unique names for Control/Server named pipes.
IAppClient is the client interface that will be implemented into the Control library to perform remote calls on the Application.
IAppServer is the server interface that will be implemented into the Application to accept remote calls from the Control library.
IWebBrowserClient is the client interface that will be implemented into Application to perform remote calls on the Control library.
IWebBrowserServer is the server interface that will be implemented into the Control libray to accept remote calls from the Application.
MainApplication.WebBrowser
This project implements the OutOfProcessEntryPoint interface that initializes the Control WCF Service. It contains the implementation of the Server interface and allows the remote client show the folder and retrieve the returned result.
MainApplication
I edited the Program.Main accepting the binaries path. I save this argument into a static variable that I'll use to crate the child process handle. The function that creates the process handle is this:
public static IChildProcessHandle CreateChildProcessHandle() {
string assemblyPath = _sourcePath ?? Path.GetDirectoryName(Assembly.GetAssembly(typeof(WebBrowserInitializer)).Location);
Debug.Assert(assemblyPath != null, "assemblyPath != null");
var al = new ChildProcessFactory() { ClientExecutablePath = _sourcePath };
return al.Create(Path.Combine(assemblyPath, "MainApplication.WebBrowser.dll"), false, Environment.Is64BitProcess);
}
If the source path isn't passed (like if I execute the application directly), RedGate will use the default location (the executing assembly path).
Once the windows is opened, the user could press the Show (or ShowDialog) button. The application "simply" run these lines of code:
//Generates client id and server id
string appId = Guid.NewGuid().ToString("N");
string controlId = Guid.NewGuid().ToString("N");
_service = AppServer.Start(appId, controlId);
_service.FormCompleted += Service_FormCompleted;
_locator = new FormServiceLocator(appId, controlId);
_element = _handle.CreateElement(_locator);
_service.StartRemoteClient();
_service.ShowDialog((long)Handle);
When the user will close the window, the callback function will be called:
private void Service_FormCompleted(object sender, AppServerEventArgs e) {
//Check if invoke is required
if (InvokeRequired) {
Invoke(new Action<object, AppServerEventArgs>(Service_FormCompleted), sender, e);
return;
}
_element = null;
MessageBox.Show(this, $"Result: {e.Result} - Data: {e.AdditionalData}");
}
MainApplication.Launcher
This is the project that launch our application with ShadowCopy enabled. I pass as argument the path of the binaries.
var domain = AppDomain.CreateDomain("CefSharp-Remoting",
AppDomain.CurrentDomain.Evidence, setup);
domain.ExecuteAssembly(executablePath, new[] { $"\"/path:{assemblyPath}\"" });
Why I forked the RedGate.AppHost repository
RedGate.AppHost try to found the Clients application looking into the Assembly location. With ShadowCopy enabled, this is not possible, because the application is copied into a "random" folder, and the Client application is in the source path.
I added the ClientExecutablePath property into the ChildProcessFactory.cs and ProcessStarter.cs, so the ProcessStarter use this folder instead of default folder if this property is setted.
You can see that edits in the follow files:
https://github.com/rupertsciamenna89/RedGate.AppHost/blob/master/RedGate.AppHost.Server/ChildProcessFactory.cs
https://github.com/rupertsciamenna89/RedGate.AppHost/blob/master/RedGate.AppHost.Server/ProcessStarter.cs
Related
When I run the following code :
static void Main(string[] args)
{
if (!System.Diagnostics.EventLog.SourceExists("eventSource"))
{
System.Diagnostics.EventLog.CreateEventSource("eventSource", "");
}
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
eventLog.Source = "eventSource";
eventLog.Log = "";
for (int i = 0; i < 10000; i++)
{
Thread.Sleep(100);
eventLog.WriteEntry("test", EventLogEntryType.Information);
//I also tried the static method, the program still leaks
//System.Diagnostics.EventLog.WriteEntry("eventSource", "test", EventLogEntryType.Information);
}
Console.ReadKey();
}
The memory usage starts around 1MB, but rise up very quickly and doesn't stop. Why ?
The code I'm using to install windows service with a designated event log:
[RunInstaller(true)]
public partial class SampleServiceInstaller : System.Configuration.Install.Installer
{
private string SampleServiceName = string.Empty;
private string SampleLogName = string.Empty;
public SampleServiceInstaller()
{
this.SampleServiceName = "SampleService";
this.SampleLogName = this.SampleServiceName + "Log";
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Password = null;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
ServiceInstaller serviceInstaller = new ServiceInstaller();
//This must be identical to the WindowsService.ServiceBase name
// set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = this.SampleServiceName;
serviceInstaller.DisplayName = this.SampleServiceName;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Description = "Sample Windows Service";
// kill the default event log installer
serviceInstaller.Installers.Clear();
// Create Event Source and Event Log
// This recreates the log every time the service is reinstaled (old messages are lost)
if (EventLog.SourceExists(this.SampleServiceName)) EventLog.DeleteEventSource(this.SampleServiceName);
System.Diagnostics.EventLogInstaller logInstaller = new System.Diagnostics.EventLogInstaller();
logInstaller.Source = this.SampleServiceName; // use same as ServiceName
logInstaller.Log = this.SampleLogName; //can't be the same as service name
// Add all installers
this.Installers.AddRange(new Installer[] {
serviceProcessInstaller, serviceInstaller, logInstaller
});
}
public override void Install(System.Collections.IDictionary savedState)
{
base.Install(savedState);
}
protected override void OnBeforeUninstall(System.Collections.IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
}
service code:
public partial class SampleService : ServiceBase
{
/// <summary>
/// Designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Event log for the service
/// </summary>
EventLog serviceLog;
/// <summary>
/// Public Constructor for WindowsService.
/// - Initialization code here.
/// </summary>
public SampleService()
{
InitializeComponent();
}
/// <summary>
/// The Main Thread: list of services to run.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new SampleService() };
ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Startup code
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
base.OnStart(args);
//
// run your own start code here
//
serviceLog.WriteEntry("My service started", EventLogEntryType.Information, 0);
}
/// <summary>
/// Stop code
/// </summary>
protected override void OnStop()
{
//
// run your own stop code here
//
serviceLog.WriteEntry("My service stopped", EventLogEntryType.Information, 0);
base.OnStop();
}
/// <summary>
/// Cleanup code
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
//
// do disposing here
//
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Pause code
/// </summary>
protected override void OnPause()
{
base.OnPause();
//
// code to run if service pauses
//
}
/// <summary>
/// Continue code
/// </summary>
protected override void OnContinue()
{
base.OnContinue();
//
// code tu run when service continues after being paused
//
}
/// <summary>
/// Called when the System is shutting down
/// - when special handling
/// of code that deals with a system shutdown, such
/// as saving special data before shutdown is needed.
/// </summary>
protected override void OnShutdown()
{
//
// code tu run when system is shut down
//
base.OnShutdown();
}
/// <summary>
/// If sending a command to the service is needed
/// without the need for Remoting or Sockets,
/// this method is used to do custom methods.
/// int command = 128; //Some Arbitrary number between 128 & 256
/// ServiceController sc = new ServiceController("NameOfService");
/// sc.ExecuteCommand(command);
/// </summary>
/// <param name="command">Arbitrary Integer between 128 & 256</param>
protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
//
// handle custom code here
//
}
/// <summary>
/// Useful for detecting power status changes,
/// such as going into Suspend mode or Low Battery for laptops.
/// </summary>
/// <param name="powerStatus">The Power Broadcast Status
/// (BatteryLow, Suspend, etc.)</param>
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
//
// handle power events here
//
return base.OnPowerEvent(powerStatus);
}
/// <summary>
/// To handle a change event
/// from a Terminal Server session.
/// Useful if determining
/// when a user logs in remotely or logs off,
/// or when someone logs into the console is needed.
/// </summary>
/// <param name="changeDescription">The Session Change
/// Event that occured.</param>
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
//
// handle session change here
//
base.OnSessionChange(changeDescription);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// first 8 letters should be unique
this.ServiceName = "SampleService";
// if you want to log service event to log registered while installing the service
string newLogName = this.ServiceName + "Log";
string newSourceName = this.ServiceName;
if (!EventLog.SourceExists(newSourceName))
{
EventLog.CreateEventSource(newSourceName, newLogName);
}
serviceLog = new EventLog();
serviceLog.Source = newSourceName;
serviceLog.Log = newLogName;
// Causes log to be disposed when the service is disposed
components.Add(serviceLog);
// Flags set whether or not to handle that specific type of event.
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
}
}
I'm in face on the following problem: An application developed on Microsoft Visual Studio 2013 in .NET 4.5, needs to work in Window XP Platforms. I'm rebuild the software using .NET 4.0 and make some modifications to add compatibility, but when i click in a button the app crash and don't show a clear error message and the Trace resource don't log anything. On application start i had a little window that ask user to put your name, and this feature works fine. Anybody have any suggestion of what can i do ?
EDIT 1:
The follow code is the root of problems, this code was compiled using .NET 4.0:
SerialManager.cs
using System;
using System.Windows;
using TestSat;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace TestSat.DataModel
{
/// <summary>
///
/// </summary>
public class SerialManager : INotifyPropertyChanged
{
#region Events
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
///
/// </summary>
/// <param name="propertyName"></param>
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
/* [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public sealed class CallerMemberNameAttribute : Attribute
{
}*/
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="field"></param>
/// <param name="value"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
#endregion
#region Private Fields
private static SerialPort PortaSerial;
private ObservableCollection<String> mPorts;
private String mSelectedPort;
private int mBaudRate = 115200;
private int mDataBits = 8;
#endregion
#region Public Fields
public StringBuilder logText;
#endregion
#region Properties
/// <summary>
///
/// </summary>
public ObservableCollection<String> COMPorts
{
get { return mPorts; }
set { SetField(ref mPorts, value); }
}
/// <summary>
///
/// </summary>
public String TextoLog
{
get { return logText.ToString(); }
set
{
if (logText.Length >= logText.MaxCapacity)
{
logText.Clear();;
logText.Append(value);
}
else
{
logText.Append(value);
//MainWindow.last = value;
}
OnPropertyChanged("TextoLog");
}
}
/// <summary>
///
/// </summary>
public String SelectedPort
{
get { return mSelectedPort; }
set {SetField(ref mSelectedPort, value); }
}
#endregion
#region Construtors
/// <summary>
///
/// </summary>
public SerialManager()
{
InitComponents();
}
/// <summary>
///
/// </summary>
private void InitComponents()
{
RefreshPorts();
/*Initialize the log variable*/
logText = new StringBuilder();
/* Update selected port */
SelectedPort = COMPorts.Count > 0 ? COMPorts[0] : "";
}
#endregion
#region Public Methods
/// <summary>
///
/// </summary>
public void RefreshPorts()
{
// Update ports
string[] pPorts = SerialPort.GetPortNames();
// Sort alphabetically
Array.Sort(pPorts);
// Sort by string length
Array.Sort(pPorts, (x, y) => x.Length.CompareTo(y.Length));
// Create collection
COMPorts = new ObservableCollection<string>(pPorts);
}
/// <summary>
///
/// </summary>
/// <param name="mSelectedPort"></param>
public void ConnectSerial(String mSelectedPort)
{
PortaSerial = new SerialPort();
PortaSerial.PortName = mSelectedPort;
PortaSerial.BaudRate = mBaudRate;
PortaSerial.Parity = Parity.None;
PortaSerial.DataBits = mDataBits;
PortaSerial.StopBits = StopBits.One;
PortaSerial.Handshake = Handshake.None;
PortaSerial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Trace.WriteLine("DataReceived definida");
try
{
PortaSerial.Open();
}
catch (SystemException)
{
MessageBox.Show("A porta serial esta sendo usada em outra aplicação.", "Erro", MessageBoxButton.OK);
throw new SystemException();
}
}
/// <summary>
///
/// </summary>
public void DesconnectSerial()
{
if (PortaSerial.IsOpen)
{
PortaSerial.Close();
}
}
/// <summary>
///
/// </summary>
public void writeSerial(String text)
{
if (PortaSerial.IsOpen)
{
if (text.Length > 0)
{
/* char[] array = text.ToCharArray(0,text.Length);
foreach(char ch in array)
{
PortaSerial.Write(ch.ToString());
Thread.Sleep(50);
}*/
PortaSerial.WriteLine(text);
}
else
{
PortaSerial.WriteLine("");
}
}
else
{
MessageBox.Show("Porta serial não esta aberta.", "Erro", MessageBoxButton.OK);
Console.WriteLine("Porta serial não esta aberta");
}
}
/// <summary>
///
/// </summary>
public bool IsOpen()
{
return PortaSerial.IsOpen;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
MainWindow.StartRawData = true;
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
TextoLog = indata;
/*Omited code only logical operation*/
}
#endregion
}
}
If i don't do any instance or reference to serial port the applications don't crashes. Exist a way to force this part of code compiled by .NET 3.5? Or exist another suggestion of solution for this problem?
The solution i found at http://blogs.msdn.com/b/bclteam/p/asynctargetingpackkb.asp , Issue 8. Installing the .NET 4.0 update the problems don't occured anymore.
I have created and installed C# FileSystemWatcher, this part works but nothing happens when I add a file to the source folder, the file I add to the source folder should be copied to a destination folder, but that does not happen.
This is my Filesyswatcher.designer.cs
using System;
using System.Configuration;
using System.IO;
namespace HotFolderWatch
{
partial class FileSysWatcher
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.FSWatcher = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.FSWatcher)).BeginInit();
//
// FSWatcher
//
this.FSWatcher.Changed += new FileSystemEventHandler(FSWatcher_Changed);
this.FSWatcher.Created += new FileSystemEventHandler(FSWatcher_Created);
this.FSWatcher.Deleted += new FileSystemEventHandler(FSWatcher_Deleted);
this.FSWatcher.EnableRaisingEvents = true;
this.FSWatcher.NotifyFilter = ((System.IO.NotifyFilters)((((((System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName)
| System.IO.NotifyFilters.Size)
| System.IO.NotifyFilters.LastWrite)
| System.IO.NotifyFilters.LastAccess)
| System.IO.NotifyFilters.CreationTime)));
//
// FileSysWatcher
//
this.ServiceName = "FileSysWatcher";
((System.ComponentModel.ISupportInitialize)(this.FSWatcher)).EndInit();
}
#endregion
private System.IO.FileSystemWatcher FSWatcher;
/* DEFINE WATCHER EVENTS... */
/// <summary>
/// Event occurs when the contents of a File or Directory are changed
/// </summary>
private void FSWatcher_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
//code here for newly changed file or directory
}
/// <summary>
/// Event occurs when the a File or Directory is created
/// </summary>
private void FSWatcher_Created(object sender,
System.IO.FileSystemEventArgs e)
{
//code here for newly created file or directory
try
{
System.IO.File.Copy(e.FullPath, DestinationPath + e.Name, true);
}
catch (Exception ex)
{
//Util.WriteToErrorLogFile(ex);
}
}
/// <summary>
/// Event occurs when the a File or Directory is deleted
/// </summary>
private void FSWatcher_Deleted(object sender,
System.IO.FileSystemEventArgs e)
{
//code here for newly deleted file or directory
}
}
}
And this is my FileSysWatcher.cs file..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace HotFolderWatch
{
partial class FileSysWatcher : ServiceBase
{
private string _userName;
public FileSysWatcher()
{
InitializeComponent();
}
public static string DestinationPath;
public const string MyServiceName = "FileSysWatcher";
private FileSystemWatcher watcher = null;
protected override void OnStart(string[] args)
{
FSWatcher.Path = ConfigurationManager.AppSettings["WatchPath"];
DestinationPath = ConfigurationManager.AppSettings["DestinationPath"];
_userName = Environment.UserName;
// Begin watching.
FSWatcher.EnableRaisingEvents = true;
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
}
I have also tried to attach to the process while debugging but the event does not seem to occur, anyone see any mistakes that could cause this?
I have tested your code on my system (Windows 7 64): it works. A few things you should check:
Is the WatchPath / DestinationPath a local path on the system where the service is running ? Be aware that accessing a network share by a service typically requires that the service runs under a (domain) user account. Also, a mapped network drive is not seen by the service, so you should use UNC paths if acessing (SMB) file shares.
At the moment where the created event triggers, the file could be in use, so your copy may fail.
and the logging is commented beacuse that part isnt executed either.
Ok, but nevertheless, you should add more logging.
My Windows services usually contain:
protected override void OnStart(string[] args)
{
// this is not just a simple message, this has to be called very early before any worker thread
// to prevent a race condition in the .NET code of registering the event source
EventLog.WriteEntry("XxxxService is starting", EventLogEntryType.Information, 1000);
I was recently faced this issue, found this code below in above answer,
FSWatcher.EnableRaisingEvents = true;
This line of code was throwing exception previously, but later on, after adding above line, my watcher was triggering events, which makes no sense.
I did everything from Example: What is the correct way to create a single-instance application? by Matt Davis.
However, I have an application to open files. I have this code:
static Mutex mutex = new Mutex(true, "{MyApplicationTest}");
[STAThread]
static void Main(string[] args)
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(args.Length == 0 ? new Form1(string.Empty) : new Form1(args[0]));
mutex.ReleaseMutex();
}
else
{
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
}
How does open the next file in the case when the program is already running. The first file automatically opens. In contrast, the next click will only appearance of the application window on top of the screen.
Problem solved, thanks xxbbcc http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
namespace SuperSingleInstance
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string[] args = Environment.GetCommandLineArgs();
SingleInstanceController controller = new SingleInstanceController();
controller.Run(args);
}
}
public class SingleInstanceController : WindowsFormsApplicationBase
{
public SingleInstanceController()
{
IsSingleInstance = true;
StartupNextInstance += this_StartupNextInstance;
}
void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
{
Form1 form = MainForm as Form1; //My derived form type
form.LoadFile(e.CommandLine[1]);
}
protected override void OnCreateMainForm()
{
MainForm = new Form1();
}
}
}
Here is a Utility Class That I wrote a while back for a similar purpose.
(I guess the name GlobalMutexHelper is kind of redundant in this case, the name kind of stuck :)..anyways)
Since it implements IDisposable you can use it like so
using(var Mutexhelper=new GlobalMutexHelper("reasonably unique Name"))
{
//Code goes here
}
Its not necessary that this be implemented as an IDisposable but in my case I need it to be handy
as sometimes the "Single Instanceness" had to be dependent on other factors.
internal class GlobalMutexHelper : IDisposable
{
#region Constants and Fields
/// <summary>
/// The access rule.
/// </summary>
private readonly MutexAccessRule accessRule =
new MutexAccessRule(
new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl,
AccessControlType.Allow);
/// <summary>
/// The obj.
/// </summary>
private readonly Mutex obj;
/// <summary>
/// The sec settings.
/// </summary>
private readonly MutexSecurity secSettings = new MutexSecurity();
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="GlobalMutexHelper"/> class.
/// </summary>
/// <param name="mutexname">
/// The mutexname.
/// </param>
/// <exception cref="TimeoutException">
/// </exception>
/// <exception cref="Exception">
/// </exception>
public GlobalMutexHelper(string mutexname)
{
if (mutexname.Trim() != string.Empty)
{
this.secSettings.AddAccessRule(this.accessRule);
bool isNew;
this.obj = new Mutex(true, "Global\\SomeUniqueName_" + mutexname, out isNew);
this.obj.SetAccessControl(this.secSettings);
if (!isNew)
{
if (this.obj.WaitOne())
{
Console.WriteLine("Signalled");
}
else
{
throw new TimeoutException("Timedout while waiting for Mutex");
}
}
}
else
{
throw new Exception("The mutex name cannot be empty");
}
}
#endregion
#region Public Methods and Operators
/// <summary>
/// The dispose.
/// </summary>
public void Dispose()
{
this.obj.ReleaseMutex();
this.obj.Dispose();
}
#endregion
}
I have a simple query, i am following the tutorial on this link:
http://www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#book-unique-identifier
My problem is the author on this tutorial configure ninject in the global.asax file and deleted the ninjectwebcommon.cs file. i am trying to integrate the justblog into my existing asp.netMVC5 application that is using the ninjectwebcommon.cs file.
Any help would be much appreciated.
Did you use Nuget to add Ninject? You'll need a reference to WebActivatorEx for the bootstrapper to work (obviously along with the other required Ninject references). Add a NinjectWebCommon.cs class in your App_Start folder in your project, looking like this:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourMvcApp.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(YourMvcApp.App_Start.NinjectWebCommon), "Stop")]
namespace YourMvcApp.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel(); // you'll add modules to the parameter list here
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
//RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
///// <summary>
///// Load your modules or register your services here!
///// </summary>
///// <param name="kernel">The kernel.</param>
//private static void RegisterServices(IKernel kernel)
//{
//}
}
}