I have a Windows service that exposes a WCF service.
I also have an application that talks to the WCF service and sends commands and receives data back.
All this works fine when I run the application from Visual Studio.
However, when I install the application and run it, the application cannot communicate with the service.
Nor can the batch files that the application runs to Stop and Start the service.
I've tried using netsh to 'reserve' the URI but I don't really know what I'm doing :-)
Can you point me in the right direction?
Windows Server code WCF Service code (abridged):
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WCF_Service" in both code and config file together.
[ServiceContract]
public class InternetGauge_IO
{
[OperationContract]
public void Pause()
{
RunningState.paused = true;
}
[OperationContract]
public void Continue()
{
RunningState.paused = false;
}
[OperationContract]
public bool GetRunningState()
{
return RunningState.paused;
}
....
Windows Server code WCF Create endpoint code:
private void InitializeConsoleComms()
{
try
{
//Prepare comms with the Console application
Type serviceType = typeof(InternetGauge_IO);
host = new ServiceHost(serviceType, new Uri[] { new Uri("http://localhost:8080/") });
// Add behavior for our MEX endpoint
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);
// Create basicHttpBinding endpoint at http://localhost:8080/RJB.InternetGauge/
host.AddServiceEndpoint(serviceType, new BasicHttpBinding(), "RJB.InternetGauge");
// Add MEX endpoint at http://localhost:8080/MEX/
host.AddServiceEndpoint(typeof(IMetadataExchange), new BasicHttpBinding(), "MEX");
host.Open();
logger.LogEvent("Console comms ready", "Internet Gauge", 4, 1);
}
catch (Exception e)
{
logger.LogEvent("Failed to open Console comms", "Internet Gauge", 1, 1);
logger.LogEvent("Exception : " + e.InnerException + " Stack Trace: " + e.StackTrace + " Message: " + e.Message, "RJB.InternetGauge.WindowsService.Main", 1, 1);
}
}
The application just uses the generated proxy and methods e.g.
private bool GetRunningState()
{
try
{
InternetGauge_IOClient client = new InternetGauge_IOClient();
isRunning = true;
return(client.GetRunningState());
}
catch (Exception)
{
trayIcon.Text = "Internet Gauge Windows Service does not appear to be running.";
trayIcon.Icon = RJB.InternetGauge.Console.Properties.Resources.ServiceStopped;
isPaused = true;
isRunning = false;
return isPaused;
}
}
The netsh command I tried
netsh http add urlacl url=http://+:8080/InternetGauge_IO user=PC-01\richard
Any ideas?
Thanks
Richard
It is because I am a twit.
Didn't copy the app.config over during the installation program.
All works fine now.
Related
I inherited a C# application and working on it. It creates some firewall rules programmatically. By default it disables everything on a specific interface, then allows a few specified TCP ports access, which is fine. I can't figure out how to modify the code to allow that port to respond to ping commands. However, and couldn't find any code online in other searches that would do that.
Does anyone know how to use C# to create a firewall rule to allow a port to respond to ping commands? The app will be deployed in Windows 7 embedded, 64 bit.
Here is some existing code which creates a rule to open a TCP port, which works OK:
private void SetupFirewallAllowIncomingRule(int port)
{
try
{
_log.Debug("Creating instance of Windows Firewall policy (HNetCfg.FwPolicy2)...");
INetFwPolicy2 firewallPolicy = Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")) as INetFwPolicy2;
if (null == firewallPolicy)
{
_log.Error("HNetCfg.FwPolicy2 instance could not be created!");
return;
}
string name = "Rule Port " + port.ToString();
foreach (INetFwRule2 rule in firewallPolicy.Rules)
{
if (name.Equals(rule.Name))
{
_log.WarnFormat("Windows Firewall Rule ({0}) already exists. It will not be created again.", rule.Name);
return;
}
}
_log.Debug("Creating new Windows Firewall Rule (HNetCfg.FWRule)...");
INetFwRule firewallRule = Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")) as INetFwRule;
if (null == firewallRule)
{
_log.Error("HNetCfg.FWRule instance could not be created!");
return;
}
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = name;
firewallRule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
//NOTE: Must do this after setting the Protocol!
firewallRule.LocalPorts = port.ToString();
_log.DebugFormat("Adding Windows Firewall Rule {0}...", firewallRule.Name);
firewallPolicy.Rules.Add(firewallRule);
_log.InfoFormat("Windows Firewall Rule {0} added.", firewallRule.Name);
}
catch (Exception ex)
{
_log.Error("Windows Firewall Rule could not be added for port " + port.ToString() + "!", ex);
}
}
I have a console application which is an FTP server. This console application works fine. Now, I want to run this FTP Server using Windows Service.
I have an unhandled exception trapper which traps an unhandled exception. After this exception occurs, I want to stop the service, destruct the class for FTP Server, delay it for 10 seconds and restart the service.
Following is my code (The ftp server and service works fine if there is no unhandled exception but I want to successfully stop and restart the service. This code stops the service fine but doesn't restart it). Any ideas?
public partial class FTPService : ServiceBase
{
private static FtpServer _ftpServer;
public FTPService(string[] args)
{
InitializeComponent();
string eventSourceName = "Ftp Server Events";
eventLog1 = new System.Diagnostics.EventLog();
string logName = "Ftp Server Log";
if (args.Count() > 0)
{
eventSourceName = args[0];
}
if (args.Count() > 1)
{
logName = args[1];
}
eventLog1 = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists(eventSourceName))
{
System.Diagnostics.EventLog.CreateEventSource(eventSourceName, logName);
}
eventLog1.Source = eventSourceName;
eventLog1.Log = logName;
if (!System.Diagnostics.EventLog.SourceExists("Ftp Server Events"))
{
System.Diagnostics.EventLog.CreateEventSource(
"Ftp Server Events", "Ftp Server Log");
}
this.ServiceName = "FTP Service";
this.AutoLog = true;
}
public static void Main(string[] args)
{
ServiceBase.Run(new FTPService(new string[0]));
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
var database = new Database(); // Gets database details as FTP server tals to database.
var configurationManager = new ConfigurationManagerWrapper(); // Same as above
_ftpServer = new FtpServer(new Assemblies.Ftp.FileSystem.StandardFileSystemClassFactory(database, configurationManager));
_ftpServer.Start(); //Starts the service (FTP Server works fine if there is no handled exception)
eventLog1.WriteEntry("Started");
FtpServerMessageHandler.Message += MessageHandler_Message;
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
}
protected override void OnStop()
{
_ftpServer.Stop(); // This calls the destructor for FTP Server, to close any TCP Listening connections, etc
base.OnStop(); // Here I stop the service itself.
eventLog1.WriteEntry("Stopped");
Thread.Sleep(10000);
}
protected void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) // unhandled exception handler
{
eventLog1.WriteEntry(e.ExceptionObject.ToString());
Thread.Sleep(5000);
OnStop(); // Calls onstop which stops FTP Server and destroys previous objects of FTP server
var serviceMgr = new ServiceController();
serviceMgr.Start(); // Here I want to restart the service (it doesn't work)
}
}
Try following:
services.msc -> your Service-> Properties -> Recovery
set properties for you
-> Save -> your Service -> restart
I have application that host WCF service:
namespace ServiceLibrary
{
public delegate void StatusEventHandler(Capture capture);
// You have created a class library to define and implement your WCF service.
// You will need to add a reference to this library from another project and add
// the code to that project to host the service as described below. Another way
// to create and host a WCF service is by using the Add New Item, WCF Service
// template within an existing project such as a Console Application or a Windows
// Application.
[ServiceContract()]
public interface IService1
{
[OperationContract]
string ClientMsg(string str);
}
[ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.PerSession)]
public class service1 : IService1
{
public event StatusEventHandler CapturingEvent;
public event StatusEventHandler OnProcessExitedEvent;
public event StatusEventHandler OnFinishEvent;
public string ClientMsg(string str)
{
return DoWork(str);
}
private DoWork(string str)
}
MyClass obj = New MyClass();
obj.Start(str); /// Do my job
}
}
}
The client send string to my server and i am opening instance of my class and this class open process, do my job and return to the client the process id number.
this server received messages from multiple clients so i wonder if i need to open new thread each time i received client message to avoid situation that several clients send message to the server in the same time.
This is how i am open the server connection in main form:
private void connect()
{
try
{
if (!isConnected)
{
// Returns a list of ipaddress configuration
IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());
// Get machine ipaddress
IPAddress _ipAddress = IPAddress.Parse(tbServerIp.Text);
// Create the url that is needed to specify where the service should be started
urlService = "net.tcp://" + _ipAddress.ToString() + ":8000/CapturesService";
// Instruct the ServiceHost that the type that is used is a ServiceLibrary.service1
host = new ServiceHost(typeof(ServiceLibrary.service1));
//ServiceLibrary.service1 serviceInstance = new ServiceLibrary.service1();
//serviceInstance.CapturingEvent += serviceInstance_StartCapturingEvent;
//serviceInstance.OnProcessExitedEvent += serviceInstance_OnProcessExitedEvent;
//host = new ServiceHost(serviceInstance);
host.Opening += new EventHandler(host_Opening);
host.Opened += new EventHandler(host_Opened);
host.Closing += new EventHandler(host_Closing);
host.Closed += new EventHandler(host_Closed);
// The binding is where we can choose what transport layer we want to use. HTTP, TCP ect.
NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.TransactionFlow = false;
tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
tcpBinding.Security.Mode = SecurityMode.None; // <- Very crucial
// Add a endpoint
host.AddServiceEndpoint(typeof(ServiceLibrary.IService1), tcpBinding, urlService);
// A channel to describe the service. Used with the proxy scvutil.exe tool
ServiceMetadataBehavior metadataBehavior;
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
// Create the proxy object that is generated via the svcutil.exe tool
metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetUrl = new Uri("http://" + _ipAddress.ToString() + ":8001/CapturesService");
metadataBehavior.HttpGetEnabled = true;
metadataBehavior.ToString();
host.Description.Behaviors.Add(metadataBehavior);
urlMeta = metadataBehavior.HttpGetUrl.ToString();
}
host.Open();
isConnected = true;
}
else
{
if (asyncWorker.IsBusy)
{
// Notify the worker thread that a cancel has been requested.
// The cancel will not actually happen until the thread in the
// DoWork checks the bwAsync.CancellationPending flag, for this
// reason we set the label to "Cancelling...", because we haven't
// actually cancelled yet.
asyncWorker.CancelAsync();
}
host.Close();
isConnected = false;
}
}
catch (Exception ex)
{
isConnected = false;
MessageBox.Show(ex.Message);
return;
}
}
private int StartTsharkProcess(Capture capture)
{
ProcessExitedEvent += Capture_ProcessExitedEvent;
string args = string.Format("-i {0} host {1} {2} -a duration:300 -w {3}",
Interface.interfaceNumber,
capture.machineIpAddress,
getTsharkFilter(),
Path.Combine(LocalPath.localPath, capture.fileName));
int processId = InvokeProcess(WiresharkProcesses.Tshark, args);
return processId;
}
this server received messages from multiple clients so i wonder if i need to open new thread each time i received client message to avoid situation that several clients send message to the server in the same time.
ServiceBehavior attribute has ConcurrencyMode Property.
This property indicates whether an instance of a service can handle one thread or multiple threads that execute concurrently, and if single-threaded, whether reentrancy is supported.
The default service behavior has ConcurrencyMode with ConcurrencyMode.Single value. So, if it is necessary to allow multiple calls at once, please use ConcurrencyMode.Multiple with notice:
No synchronization guarantees are made. Because other threads can change your service object at any time, you must handle synchronization and state consistency at all times.
Note: if service methods perform long-running tasks, clients might have a timeout.
My symptom is exactly as this post described http://social.msdn.microsoft.com/Forums/vstudio/en-US/302ca96e-a810-4958-9905-90ba1175765d/servicehost-does-not-detech-endpoints-cant-recover-from-a-faulted-state
I wonder if this is a known bug.
My code is slightly different from Dave's whereas his ServiceHost instance (named WebService) is outside of Start() method. My ServiceHost instance (named host) is declared inside. When debugging, I check in Host Description that the address of endpoints has changed to the correct IP. However, the Open() still throws an exception with the old wrong IP address.
private bool InitHost(PtiType type, string serverIp, int portNumber)
{
if (!HostDictionary.ContainsKey(type))
{
Uri addressBase = new Uri(String.Format("net.tcp://{0}:{1}/CommunicationService/{2}", serverIp, portNumber.ToString(), type.ToString()));
var service = new PtiCommunicationService(type);
service.ClientConnected += service_ClientConnected;
service.ClientBroadcasted += service_ClientBroadcasted;
service.ClientSentTo += service_ClientSentTo;
service.ClientDisconnected += service_ClientDisconnected;
var host = new ServiceHost(service, addressBase);
//For publishing metadata only
//Define Metadata endPoint, So we can publish information about the service
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(mBehave);
//Enable debug info in fault
((ServiceDebugBehavior)host.Description.Behaviors[typeof(ServiceDebugBehavior)]).IncludeExceptionDetailInFaults=true;
host.AddServiceEndpoint(typeof(IPtiCommunication), new NetTcpBinding(SecurityMode.None), "");
host.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexTcpBinding(),
"mex");
try
{
host.Open();
//Add host to dictionary to keep track
HostDictionary.Add(type, host);
LogList.Add(String.Format("{0}\tThe service {1} at {2} is ready", DateTime.Now.ToLongTimeString(), service.ServiceType.ToString(), serverIp));
string hostInfo = String.Format("{0}\tHost information:\n", DateTime.Now.ToLongTimeString());
hostInfo += "Enpoints details:\n";
foreach (var endpt in host.Description.Endpoints)
{
hostInfo += String.Format("\t Name:\t\t{0}\n", endpt.Name);
hostInfo += String.Format("\t Logical address:\t{0}\n", endpt.Address);
hostInfo += String.Format("\t Physical address:\t{0}\n", endpt.ListenUri);
hostInfo += String.Format("\t Binding:\t\t{0}\n", endpt.Binding);
hostInfo += String.Format("\t Contract:\t{0}\n\n", endpt.Contract.ContractType.Name);
}
LogList.Add(hostInfo);
}
catch (Exception e)
{
host.Abort();
host = null;
LogList.Add(String.Format("{0}\t{1}", DateTime.Now.ToLongTimeString(), e.Message));
}
return true;
}
return false;
}
Here is my ServiceBehavior for PtiCommunicationService
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
and ServiceContract for the interface
[ServiceContract(CallbackContract = typeof(IPtiCommunicationCallback), SessionMode = SessionMode.Required)]
There is no other configurations in app.config. All are in the code.
Thanks
UPDATE:
I've discovered that both of us use the same overload for ServiceHost Constructor (Object, Uri[]) which make a singleton of web service.
When we create new Service Host with the same singleton, somehow changing endpoint address doesn't take into affect because the instance of the service is still there even though the host has been aborted.
Is there any solution to clean up that singleton when we create new host?
That's my suspicion so far, please correct me if I'm wrong.
Apparently WCF caches some socket information. The best workaround I have found was given by Ivan here https://stackoverflow.com/a/6839265/955400 . You check if you can open the connection before attempting to call host.Open().
I think I've literally checked for all possibilities but I still keep getting this error (written in eventvwr) when I attempt to start my service:
Service cannot be started. System.InvalidOperationException: Service
'NexolNotifierWinService.NexolNotifier' has zero application
(non-infrastructure) endpoints. This might be because no configuration
file was found for your application, or because no service element
matching the service name could be found in the configuration file, or
because no endpoints were defined in the service element.
Service installation goes smoothly using installutil.
I'm genuinely not sure why I'm having this error. It's just a simple Windows Service project, so there's no app.config to mess around with either.
Here's my code:
Program.cs
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new NexolNotifierService()
};
ServiceBase.Run(ServicesToRun);
}
}
NexolNotifierService.cs
public partial class NexolNotifierService : ServiceBase
{
private ServiceHost host;
public NexolNotifierService()
{
InitializeComponent();
this.ServiceName = "NexolNotifierService";
}
protected override void OnStart(string[] args)
{
Type serviceType = typeof(NexolNotifier);
host = new ServiceHost(serviceType);
host.Open();
}
protected override void OnStop()
{
if (host != null)
host.Close();
}
}
ProjectInstaller.Designer.cs (For installing service)
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "NexolNotifierService";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
and my actual service:
NexolNotifier.cs
public class NexolNotifier
{
public NexolNotifier()
{
....
}
Service was added from add new project->Windows Service in Visual Studio 2008.
I'm just trying to get a very simple windows service working. From what I can see, there is zero reason why this shouldn't work.
What do you want to do?
If you want just a plain Windows Service - no communication, nothing - then you don't need ServiceHost! You just need to derive from the ServiceBase class in the .NET framework and implement/override some of the methods - that's all. Read values from a database, do something with them, send e-mails - whatever - you will never need a ServiceHost for this!
If you use ServiceHost then you're using the WCF infrastructure, which means, you're writing a self-hosted web service.
So what do you want to do, really??
What's the task/job that your Windows Service is supposed to do?? ServiceHost has nothing to do with a plain Windows Service! ServiceHost == WCF - always. You don't need a ServiceHost for just a plain Windows service
For just plain Windows service (no WCF), see e.g.
Creating a Basic Windows Service
Simple Windows Service Sample
and many, many more samples. Both samples show just a plain Windows service, no WCF, no ServiceHost anywhere in sight.
Add service endpoint from code like this
Uri baseAddress = new Uri("http://localhost:8095/Service");
serviceHost = new ServiceHost( typeof(YourService), baseAddress );
serviceHost.AddServiceEndpoint( typeof(IYourService), new BasicHttpBinding(), baseAddress );
serviceHost.Open();