Windows service stopped without any errors - c#

I created a windows service to host some WCF services,
but when I start it, it stop with a message:
I checked windows log viewer and there isn't any errors
and I tested everything on a console application before and it's working.
My code is:
ServiceHost host1;
ServiceHost host2;
ServiceHost host3;
public ServicesHost()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (host1 != null)
host1.Close();
if (host2 != null)
host2.Close();
if (host3 != null)
host3.Close();
host1 = new ServiceHost(typeof(Service1));
host1.Open();
host2 = new ServiceHost(typeof(Service2));
host2.Open();
host3 = new ServiceHost(typeof(Service3));
host3.Open();
}
protected override void OnStop()
{
host1.Close();
host1 = null;
host2.Close();
host2 = null;
host3.Close();
host3 = null;
}
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<service behaviorConfiguration="MyServiceBehavior"
name="Service2">
<endpoint address=""
binding="basicHttpBinding"
contract="IService2">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Service2" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="MyServiceBehavior"
name="Service3">
<endpoint address=""
binding="basicHttpBinding"
contract="IService3">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Service3" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
Edit:
I have installer:
[RunInstaller(true)]
public partial class ServiceInstaller : System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller process;
private System.ServiceProcess.ServiceInstaller service;
public ServiceInstaller()
{
process = new System.ServiceProcess.ServiceProcessInstaller();
process.Account = System.ServiceProcess.ServiceAccount.NetworkService;
service = new System.ServiceProcess.ServiceInstaller();
service.ServiceName = "WCFHostService";
service.DisplayName = "WCFHostService";
service.Description = "WCF Service Hosted";
service.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
Installers.Add(process);
Installers.Add(service);
}
}

When a console/desktop application runs but not as service, it's mostly a userright problem. This applies on like, using COM/DCOM or the use of files, because the current path for a service is the windows\system32.
Try to wrap the OnStart with try/catch and write the exception to the EventLog -> http://support.microsoft.com/kb/307024
Did you create any installers for the service?
How to: Add Installers to Your Service Application http://msdn.microsoft.com/en-us/library/ddhy0byf.aspx

Well, you are not logging at all. A window service should have a good logging system to survive in the production/mainteinance phase. Add a loggin system, put a try catch in the start, catch and log the exception in a file, this will hel you for the problem you are facing now, but help every day in your project lifetime.

Related

Only an absolute URI can be used as a base address

Please help getting exception at using (ServiceHost host = new ServiceHost(typeof(HelloService.HelloService))) in the below code
Exception : Only an absolute URI can be used as a base address
WCF Host Application
class Program
{
static void Main()
{
using (ServiceHost host = new ServiceHost(typeof(HelloService.HelloService)))
{
host.Open();
Console.WriteLine("Service Started");
Console.ReadLine();
}
}
}
Contract Implementation
public class HelloService : IHelloService
{
public string GetMessage(string Name)
{
return "Hello" + Name;
}
}
Contract
[ServiceContract]
public interface IHelloService
{
[OperationContract]
string GetMessage(string Name);
}
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HelloService.HelloService" behaviorConfiguration="mexBehaviour">
<endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService">
</endpoint>
<endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/HelloService"/>
<add baseAddress="net.tcp//localhost:8090/HelloService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I believe you are missing a colon (:):
<add baseAddress="net.tcp//localhost:8090/HelloService"/>
should be
<add baseAddress="net.tcp://localhost:8090/HelloService"/>
<endpoint address="HelloService"...
Should be
<endpoint address="/HelloService"...
See https://msdn.microsoft.com/en-us/library/ms733749(v=vs.110).aspx
In short this is Error related to base address.so, please check whether you have entered base address properly or not.
Error:
<add baseAddress=" "http://localhost:8732/Design_Time_Addresses/WcfServiceLibraryShop/IStudent/">" />
Solved:
<add baseAddress=" http://localhost:8732/Design_Time_Addresses/WcfServiceLibraryShop/IStudent/" />

Convert WCF Service from .NET 4.5 to .NET 3.5

I have a selfhosted WCF Service written in Visual Studio 2013 Pro (.NET 4.5). It perfectly works when I run it in .NET 4.5. However, when I change my project settings to .NET 3.5, I get an InvalidOperationException.
namespace ProcessingLayer
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(ProcessingService)); // -> InvalidOperationException
host.Open();
Console.WriteLine("Host Open Sucessfully ...");
Console.ReadLine();
}
}
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IProcessingService
{
[OperationContract(IsInitiating = true, IsTerminating = false)]
void startSession(string s);
[OperationContract(IsInitiating = false, IsTerminating = false)]
Feature getFeatureData();
[OperationContract(IsInitiating = false, IsTerminating = true)]
void terminateSession();
}
public class ProcessingService : IProcessingService
{
// Omitted for reasons of pace and space
}
}
app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IProcessingService"/>
</netTcpBinding>
<netNamedPipeBinding>
<binding name="NetNamedPipeBinding_IProcessingService"/>
</netNamedPipeBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ProcessingLayer.ProcessingServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ProcessingLayer.ProcessingService">
<endpoint address="ProcessingService" binding="netNamedPipeBinding" contract="ProcessingLayer.IProcessingService" bindingConfiguration="NetNamedPipeBinding_IProcessingService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="ProcessingService" binding="netTcpBinding" contract="ProcessingLayer.IProcessingService" bindingConfiguration="NetTcpBinding_IProcessingService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9001/ProcessingLayer/ProcessingService/"/>
<add baseAddress="net.pipe://localhost/ProcessingLayer/ProcessingService/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
I need to change it from .NET 4.5 to .NET 3.5 because of compatibility issues with a client application. As I only have VS 2010 and VS 2013, I cannot follow the obsolete .NET 3.5 Tutorials, because those assume VS 2008.
How can I change it from .NET 4.5 to 3.5?

Hosting two WCF services using a single console app

I am trying to host two services using a single console app. However, when I am trying to do so, only one service gets hosted, while the other does not.
Program.cs:
namespace WWWCFHost
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(WWWCF.Login)))
{
host.Open();
Console.WriteLine("Service1 Started");
}
using (ServiceHost host1 = new ServiceHost(typeof(WWWCF.UserRegistration)))
{
host1.Open();
Console.WriteLine("Service2 Started");
Console.ReadLine();
}
}
}
}
App.config
<configuration>
<system.serviceModel>
<services>
<service name="WWWCF.Login" behaviorConfiguration="WWWCF.mexBehaviour1">
<endpoint address="Login" binding="basicHttpBinding" contract="WWWCF.ILogin">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080"/>
</baseAddresses>
</host>
</service>
<service name="WWWCF.UserRegistration" behaviorConfiguration="WWWCF.mexBehaviour2">
<endpoint address="UserRegistration" binding="basicHttpBinding" contract="WWWCF.IUserRegistration">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8090"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WWWCF.mexBehaviour1">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
<behavior name="WWWCF.mexBehaviour2">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
As in the code above, I am trying to host one service on port 8080 and the other on port 8090. When I run the application, the first service starts and then closed automatically and the second service remains started. How can I host both the services simultaneously ?
I have gone through the link : Two WCF services, hosted in one console application
I have gone through other threads as well.But they do not solve my issue.
Will be happy to provide any further details if required.
Your first service jumps out of the using block and so is disposing too early. Try this...
using (ServiceHost host = new ServiceHost(typeof(WWWCF.Login)))
using (ServiceHost host1 = new ServiceHost(typeof(WWWCF.UserRegistration)))
{
host.Open();
Console.WriteLine("Service1 Started");
host1.Open();
Console.WriteLine("Service2 Started");
Console.ReadLine();
}
Take a look at this: http://msdn.microsoft.com/en-us//library/yh598w02.aspx
You're instantly closing the first, since it's in the using. You need to set it up so the first using scope doesn't end until after the ReadLine() call.
Try:
using (ServiceHost host = new ServiceHost(typeof(WWWCF.Login)))
{
host.Open();
Console.WriteLine("Service1 Started");
using (ServiceHost host1 = new ServiceHost(typeof(WWWCF.UserRegistration)))
{
host1.Open();
Console.WriteLine("Service2 Started");
Console.ReadLine();
}
}

"Unable to Download Metadata" when using Two Bindings

In my book, it wants me to expose two endpoints using two bindings: WsHttpBinding & NetTCPBinding and host the service in a host application.
I use the following code in C# to try to connect to my service:
Uri BaseAddress = new Uri("http://localhost:5640/SService.svc");
Host = new ServiceHost(typeof(SServiceClient), BaseAddress);
ServiceMetadataBehavior Behaviour = new ServiceMetadataBehavior();
Behaviour.HttpGetEnabled = true;
Behaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
Host.Description.Behaviors.Add(Behaviour);
Host.Open();
On the service side I have:
[ServiceContract]
public interface IService...
public class SService : IService....
Then in my Config file I have:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WsHttpBehaviour" name="SService.SService">
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="WsHttpBindingConfig"
contract="SService.IService" />
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="NetTCPBindingConfig"
contract="SService.IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5640/SService.svc" />
<add baseAddress="net.tcp://localhost:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
But when I try to add service reference to my host application, it says unable to download metadata from the address. I don't understand what is wrong and the teacher never taught this.. I decided to go ahead and look it up and learn ahead of time. I used the Editor to create the WebConfig shown above.
Can anyone point me in the right direction? I added Metadata behaviour through the editor and I set the HttpGetEnabled to true.
I can find a few issues with your code that can cause this issue:
Host = new ServiceHost(typeof(SServiceClient), BaseAddress). Pass here typeof(SService.SService) instead of typeof(SServiceClient). Change like this:
Host = new ServiceHost(typeof(SService.SService))
<service behaviorConfiguration="WsHttpBehaviour". I guess this should be "Behavior" as you have defined that. Since you have metadata enabled in config, you may remove the lines of code which add a ServiceMetadataBehavior to your servicehost.
Here is a sample that you can use for reference:
<system.serviceModel>
<services>
<service name="ConsoleApplication1.Service">
<endpoint address="" binding="wsHttpBinding" contract="ConsoleApplication1.IService" />
<endpoint address="" binding="netTcpBinding" contract="ConsoleApplication1.IService" />
<host>
<baseAddresses>
<add baseAddress="http://<machinename>:5640/SService.svc" />
<add baseAddress="net.tcp://<machinename>:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(Service));
host.Open();
Console.ReadLine();
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string DoWork();
}
public class Service : IService
{
public string DoWork()
{
return "Hello world";
}
}
}
Metadata will be automatically available at the http baseaddress defined in the config.

Cannot access WCF url hosted as a windows service

I have created a WCFLibrary which has :
[OperationContract]
string TestCall();
And my app.config has this :
<endpoint address="" binding="wsHttpBinding" contract="TestWCF.ITestService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/TestWCF/TestService/" />
</baseAddresses>
</host>
My Windowsservice has this :
protected override void OnStart(string[] args)
{
host = new ServiceHost(typeof(TestWCF.TestService));
}
Have compiled exe and installed it as a service everything is fine till this point.
Now I wanted to check this url from browser :
*http://localhost:8732/Design_Time_Addresses/TestWCF/TestService*
but due to some reasons I cant make a call to my WCF hosted in Windows Service, What might be went wrong am I missing anything ?
protected override void OnStart(string[] args)
{
host = new ServiceHost(typeof(TestWCF.TestService));
host.Open(); // :-)
}

Categories

Resources