I have developed (in VS2010 - on a machine with Windows 7) a small solution
which contains a WCF Service library and a Windows Service for hosting that WCF
service. The App.config which is in my WCF service is:
This file is also in my windows service with the same configurations.
My service code is something very simple:
File IService1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibrary1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
}
and file Service1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibrary1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.ServiceModel; using WcfServiceLibrary1; using System.IO; namespace WindowsService1 { public partial class Service1 : ServiceBase { internal static ServiceHost myServiceHost = null; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { StreamWriter sw; try { if (myServiceHost != null) myServiceHost.Close(); myServiceHost = new ServiceHost(typeof(Service1)); myServiceHost.Open(); } catch (Exception exc) { sw = new StreamWriter(@"C:\Log\Err.Txt"); sw.Write(exc.Message); sw.Close(); } } protected override void OnStop() { StreamWriter sw; try { if (myServiceHost != null) { myServiceHost.Close(); myServiceHost = null; } } catch (Exception exc) { sw = new StreamWriter(@"C:\Log\Err.Txt"); sw.Write(exc.Message); sw.Close(); } } } }
I have added an Installer where I configured properties: Account: NetworkService for serviceProcessInstaller
and StartType: Automatic for serviceInstaller.
I built the project, after that I install the windows service with InstallUtil. After that I looked in my log file if something is wrong. Unfortunately, I didn't find anything. Atfer that I enter in Computer Management (I have Windows 7 and VS2010) and I started my Windows Service. After that I looked again in my log file and in Logs from Event Viewer (from Application), but again I didn't find anything.
Unfortunately, when I want to consume (to register) that address, I can't do this thing. I tried with Add Service refference and after that with SvcUtil but the result is the same.
When I want to add service address net.tcp://localhost:8001/Service1 with
Add Service Refference it appears on the screen An error (Detail) occured while
attempting to find services at 'net.tcp://localhost:8001/Service1'.
I checked the metadata addressed, but it seems that it's OK. Maybe I'm wrong of course.
I run netstats -ap tcp and I didn't see my TCP protocol: 8001.
Please help me and tell me what it's wrong.
Sincerely Yours,
Florin G
Florin GPosted Jul 29, 2013, 2:10 PM
Yes, is it about your project. But it's the same error in my project. too.
Yes, I commented that line in Program.cs and I built up the project in Release mode.
I have attached the error file log. I saw that if I uninstall the windows service with InstallUtil /u ... then the log it's empty, wchich means that there aren't any kind of error. But, if i Stop the windows Service from Computer Management (click on Stop button) in this case will appear those errors which are in the log file.
But, the project can be consumed and all it worked fine.
Sincerely Yours,
Florin G
Hemant SrivastavaPosted Jul 29, 2013, 8:35 AM
(1) The problem you faced in the service, is it my service or yours?
(2) If service is mine, then before installing as a windws service did you make necessary change in the program.cs file( otherwise without any change this service is ready to run in debug mode not in windows service mode)?
(3) Where is attached file?
Florin GPosted Jul 29, 2013, 2:46 AM
Thank you for your response. I have run your sample and it's working, but now I have another problems. First of all I install my your windows service, after that I open the test client, remove the refference and Add service refference. All was ok and it worked very fine. But finally I closed the client aplication and after that I open Computer Manager and I Stop the Windows Service.
After that I open C:\Log and I found this file with some errors. I have attached the log error file WCF_Errors.svclog. Can you open and help me with which is the solution?
Sincrely Yours,
Florin G
Hemant SrivastavaPosted Jul 26, 2013, 12:12 PM
I created a new sample WCF Service named 'TestService' with netTcpBinding hosted as a Windows service. It's running fine and have same functionality what you had in your service. You can find it in the attached folder.
In the same folder, you can find a testClient to test the service too.
In the TestService, I added a feature with which you can also debug the windows service in running mode and if you want to install the windows service host, then you have to make some changes in the program.cs file.
Basically you can follow the same instruction what I gave in the link below:
http://www.c-sharpcorner.com/UploadFile/0f68f2/how-to-debug-a-wcf-service-hosted-in-windows-service/
After successfully running, you could compare your service with this TestService and make changes accordingly.
Hemant SrivastavaPosted Jul 26, 2013, 10:54 AM
"The service cannot be started. This service has no endpoint defined. Please add at least one endpoint for the service in config file and try again."
Although without host, it's running fine.. I am looking into ...why its happening
Florin GPosted Jul 26, 2013, 1:52 AM
I have resolved the problem. I rename my Service class.
Thank you!
Florin GPosted Jul 25, 2013, 3:25 PM
I have attached my rar arhive.
Some suplimentar details:
- I built the project in Debug mode and after that in Release mode, but unfortunately the result is the same.
- I tried the project on two different machines (with Win 7 and VS 2010) and the result is the same.
So, I think that the problem is in my solution.
Sincerely Yours,
Florin G
Florin GPosted Jul 25, 2013, 3:20 PM
Here is the rar arhive!
Than you!
Hemant SrivastavaPosted Jul 25, 2013, 2:51 PM
Florin GPosted Jul 25, 2013, 2:45 PM
Anyway in my case because I have only one wcf service I mace copy paste.
But when you have more wcf services and one windows service in that case you must assure tu have the same configurations for every wcf service (individual).
But, in this case it's the same content.
Hemant SrivastavaPosted Jul 25, 2013, 2:32 PM
Florin GPosted Jul 25, 2013, 2:21 PM
First of all I want to thank you for your advice.
I followed your steps and fortunately in debugging mode my service it's working, but unfortunatetly after that it not working.
More said, if I put that code to run in debug mode my windos Service and after that I stop after I open my WCF SERVCE, and after that I open a client app and add the refference for my address all it's working very well.
So, in conclusion I don't know which is the cause.
Anyway, if you look I put a try catch and any exception it can be catch in a log file, but it doesn't enter in catch block.
Can you tell me which is the problem?
Hemant SrivastavaPosted Jul 25, 2013, 10:31 AM
Step 1- So firstly you need to check, service itself is getting started and running properly or not without your window service host.
Step 2 - After this, you need to check service runs fine with your windowservice host.
For step-1, You could check 'service running' by removing your windows installer and making your service as a startup project.
For step-2, you need to debug and check service running in the following way:
http://www.c-sharpcorner.com/UploadFile/0f68f2/how-to-debug-a-wcf-service-hosted-in-windows-service/
Let me know your issues.... as I am working on currently a WCF service hosted as a Windows sevice so I might help you.