So don't do that. Just slowly cut and paste code from your old program into the new one. And test with F5 debugging in Visual Studio. Error means that your service timed out on start. You're not blocking the thread that runs OnStart are you?
The only thing I can suggest, off the top of my head, is that your Web Service has to be a separate Service, that's the way that we've handled it in our applications. Take a look at my blog post again and note that there are two services being "hosted" and started in the MyServiceHost class. Yes the console app alone works and the sample windows service I created, it works too but when I try to move code from console app to windows service only those 3 lines you can see in my code starts creating issue.
Bonnie or David, please help me out? I am struggling with this issue a lot. I really don;t know how to use the above posted debugging code as I am not a windows service dev but this is a task that I have to complete. Yes, I did look at your blog. The article is great but I didn't try the exact solution because I am able to run my windows service that I created based on learning from your blog and other research online.
I also saw you using SQL server services and I do not have sql server on the machine. The moment I add these 3 lines, the windows service refuses to install and I get "Error".
Without those 3 lines, windows service works fine. But those 3 lines are important because that's how I connect to web services in the console application. Those 3 lines are running in the constructor of your service class.
So during service startup. Don't do that. Also you can start the service and attach Visual Studio's debugger to the process before the timer fires and debug the running service. If you want to use TopShelf, follow the below steps,. Create a Console application. In start method, you could try this way to call your web service class which inserts data in database, I am returning bool to check the web service call is successfully or failure.
In Program. I agree with David that you need to put those 3 problem lines into the OnStart instead of in the constructor. Also, I'm not sure if this is causing a problem too, but I wouldn't make the Program class derive from ServiceBase. I would have a different class that contains your service derived from the ServiceBase class. Something like this:. Question1 - The code that you posted above, should I keep it all in Program.
CS file? Question2 - So if I remove service base from Program class. Should I still add this class in Program. Where should I put this? Also, since I have update my code based on your advise above. I see the Program. CS designer is showing up like this. I did forget something in the sample I posted for you. I should have included the following to lines in the MyService class:.
Sorry about that The reason for the error you're seeing is because you probably have no threading or other mechanism keeping it "alive". Since you do it all with a timer. You'll notice that in my blog post, I use a thread to put my processing in. Perhaps I should have mentioned that as well in my previous reply. In my blog post, look at the OnStart , OnStop and DoWork methods ignore the thing about SqlServer and just always start the service inside a while loop.
I would show you an example more tailored to your code, but in this case I think pointing it out to you in my blog post might be OK plus I wanted to get this reply to you posted quicker. And, I see that you've written two more posts since I was in the process of writing this one. I will read those and comment in another post.
I'm not so sure now if your problem with the Service stopping itself has something to do with threading or not my blog post probably isn't going to help much for this, so let's ignore that for now.
If the Service keeps running fine, then there's a file access issue with where you're trying to write the file. If the Service still stops by itself, then we still don't know what the problem is and we'll have to dig some more probably use threading instead of the timer, but let's not rush into that one yet. In the code you posted, I don't see where you run this as a Console application, only where you run it as a Windows Service.
So, I'm not sure how you're testing this from VS. You should have the code in your Main method that I originally posted:. ReadLine , then stops the service. In other words, your console window will stay up until you hit any key Change it to this:. I can do that, but it won't be for awhile. I'm in the middle of doing some work right now and the boss is cracking the whip! Actually, the boss is my husband and he doesn't usually get on my case for doing stuff other than work, as long as I don't spend all day not working!
I might find some time later today, but I can't guarantee it If not today, then definitely over the weekend Apparently your web service proxy has a class called Thread, and the compiler is getting confused. Just fully-qualify the type name wherever used. Thread threadStart ; myThread. Start ;. David, thanks for the help. I used your code and errors were gone.
I ran the program in VS, got console window and message "service started" and the log file also was created "webserviceLog. This means that the DoWord is triggering. But when I install the windows service, I got this error message.
You may need to put a pause in your service startup logic to give you time to attach the debugger. Read this Introduction to Windows Service Applications. Yes, that;s the error I am talking about. In the debugging in VS for service. What I am looking to attach in the list if my service is not even starting? Yes, the error says it started but then it says it stopped so how debugging will happen when I start the service and it stops within a second?
Sorry just have much experience with debugging a service and I have tried so many things now to convert my console app to windows service, I am so much confused :. I just finished recreating your app from the code you've posted and installing as a Windows Service. It works fine, writing to the Log. Of course, I don't have that web service that you're using, so that part is commented out. Could that still be what's causing the problem, do you think? Comment it out for now and see if it works.
There may be another possible difference between my installation and yours, since I don't know how you did the installation. I always use a. Bonnie, I just sent you a 2nd email with an update that when I comment out the web service lines, the service installs and run fine by creating log file in the defined location. I disagree. The issue is that you can't troubleshoot the service.
You have no idea why it's failing. You have to implement the logging and debugging procedures that will enable you to identify and diagnose failures. A Windows service runs without a user interface, and is expected to run for months at a time.
At a minimum you need to have logging that shows you everything that happened and any exception that occurs. Once you have that, working through the problems is straightforward. Hello David, I completely understand what you are saying.
I even checked windows event but since service start and stops right away, there is no event. Could you please help me with code or guide me with the process of building the logging system so that I am troubleshoot the service and find out the cause of the problem?
I understand this takes to the point if I have config correctly setup or not. I have double check my App. I don't know what to do next as I have my App. It's funny, I woke up in the middle of the night thinking that you probably had a config issue.
You might need to share your config here. Everything must go in the EXE project's app. When your Web Reference gets generated, one of the things it creates is the web settings in Properties, Settings. Those need to be put into your app. So, here's an example of the extra stuff that needed to go into my config:.
In the above part of the app. Sorry about that! Ok Bonnie, I did the exact same thing as mention above. I used the same console application project and updated the code discussed here in Program. CS and added the installer etc. After saving, this stuff got updated automatically in the app. So, we both have the section named "OurApp. Settings", so that part looks the same. Now, look at my setting name. Yours is simply "Setting". I think that's the problem.
I don't know what name should come in so I tried the name similar to "contract" and "name" in binding settings. One more try, Jeff.
I think your setting still doesn't look right. I think it needs to be like this:. I sent you an email too. You might want to think about starting over with adding the web reference if the above suggestion doesn't work You both came up with same point and yes Bonnie, initially I was taking my. How cool is that???? Which one is supposed to be my config file here? Jeff, you don't see it because you have your folder view set to not display file extensions.
You can change that with Folder options. The Config file is the xxxImport. When a project is compiled to an EXE, the app. Config is just something that VS uses, you don't need to bother with that one. There is no batch file, you have to create your own. That's it. My blog post shows both the Install and the Uninstall batch files. Thank you Bonnie.
The batch file is an interesting second way of installing the service and still using "installutil" for it. Another option is ServiceStartMode. I usually set it to Manual in my code, because I don't want my service starting accidentally all by itself server reboots itself or something while I'm still in the process of testing it. It's easy enough to set it to Automatic once it's been installed and I'm done testing and it's ready for "production". To do that, right-click the Service and and choose Properties and change the Startup type the General tab, it's the first one that shows in the Properties window.
You can also change the Recovery options from the Recovery tab. In my case, our Services should never fail so I always set all three failure options to "Restart the Service" and check the "Enable actions for stops with errors" box. Thank you Bonnie for your reply.
Viewed 11k times. Improve this question. What have you tried? I did not get your question Console application and windows service are two different things. You want to add windows service in console application? Add a comment. Active Oldest Votes. WriteLine " 1. Run Service" ; Console. WriteLine " 2. Other Option" ; Console. WriteLine " 3. Exit" ; Console. Start args ; Console. Improve this answer. For logging configuration, we are adding an additional logging provider called event log in addition to console and debug , as we are planning to run as a windows service.
Here are the packages we are have to add. Step4: Now into the interesting part, we are going to add the top shelf part into this console application. For this, first we need to follow the doc from Microsoft, then had to copy some sample code from Microsoft GitHub. Copy ServiceLifetime. Now go back to the doc and refer to section Program. Main updates. Here, we have added flag isService to determine whether the application is running as a console or a windows service.
If it is a service, we have to set the current directory, as described in the doc. Next thing we do here is running the application as Console or Service based on the flag. Step5: Now we are almost at the end state where we need to build our service and inject into ServiceCollection as a hosted service. For this create a class called MyServiceHost as below, which basically executes the processor based on the given processing interval.
THe interesting part to be noted here is, all the dependencies IProcessor , IConfiguration , ILogger are injected in runtime via dependency injection. Now that we have created the ServiceHost, we have to inject it as a hosted service under ConfigureServices action method.
We should also inject its custom dependencies here, for e. IProcessor in our sample, as below. I hope we have made all the code changes necessary for the application, except creation of the configuration file appsettings. So we shall create the file under the project root and apply the below configuration settings.
Step6: Publish and running the application. Use the below dotnet publish command to build and publish the artifacts. Once we publish the application, we can either run the application as a console or install it as a Windows Service.
0コメント