Service Fabric error: "Service does not exist" - c#

I am getting error FabricServiceNotFoundException: Service does not exist. and I can't figure out why. The service name I am creating is exactly what it is deployed in my cluster.
This is the code where I create my service:
return ServiceProxy.Create<ICheckoutService>(
new Uri("fabric:/ECommerce/ECommerce.CheckoutService"),
new ServicePartitionKey(0));
This is the explorer view. The name of the service matches my code. I'm doing it with other services with no problem.
I tried a complete restart but I got the same error:
Deleted application from the cluster
Unprovisioned type from the cluster
Restarted the cluster
Restarted Visual Studio
Rebuild and deployed application
Update
After testing around I found out that the error occurs depending on the order on which I call services through my API methods.
If I deploy the application and call methods checkout and get basket they give "Service not found" error.
However, if I call other methods first that perform some change (POST), then it works... weird right? This is my repo to help take a look at the code.
https://github.com/epomatti/azure-servicefabric-productcatalog

With help of #maf748 I turned on "Break When Thrown" configuration for all CLR exceptions, and I discovered that the actual exception was not "Service does not exist".
In my case, I left the following auto-generated method for an Actor service, which was setting my state in a wrong state, and it was later failing in my own code.
All I needed to do was to remove this method that Visual Studio created from my method and it worked properly.
/// <summary>
/// This method is called whenever an actor is activated.
/// An actor is activated the first time any of its methods are invoked.
/// </summary>
protected override Task OnActivateAsync()
{
ActorEventSource.Current.ActorMessage(this, "Actor activated.");
// The StateManager is this actor's private state store.
// Data stored in the StateManager will be replicated for high-availability for actors that use volatile or persisted state storage.
// Any serializable object can be saved in the StateManager.
// For more information, see https://aka.ms/servicefabricactorsstateserialization
return this.StateManager.TryAddStateAsync("count", 0);
}

Related

Create a Windows service without the use of a timer [duplicate]

I made a Window service and let it work automatically and under localsystem account, when the service starts it fires this message for me and then stops
The [service name] service on local computer started and then stopped. Some Services stop automatically if they are not in use by another services or programs.
What's the problem and what's the solution?
Either you are not starting any threads on the OnStart method to do work, or there is an exception raised within your OnStart method.
If an exception is thrown, it will appear in the Windows Event log. The Windows Event log is a good place to start in any case.
Generally an OnStart method looks like this:
Thread _thread;
protected override void OnStart(string[] args)
{
// Comment in to debug
// Debugger.Break()
// Do initial setup and initialization
Setup();
// Kick off a thread to do work
_thread = new Thread(new MyClass().MyMethod)
_thread.Start();
// Exit this method to indicate the service has started
}
This particular error message means what it says - that your service has started but then quite soon it exited for some reason. The good news is that your service is actually doing something, so you have the executable configured and running as a service properly.
Once started, for some reason it is quitting. You need to find out why this is. Add some debugging to tell you its up and running and known exit cases. If that doesn't reveal the problem then add some debugging to let you know it's still running and work backwards from when that stops.
Are you tracing out any debug information? Most likely an exception is being thrown during your initialization. I would trace out all your exceptions and use Debugview to view them.
I had a similar problem that occurred because my Event Logs were full and the service was unable to write to them. As such, it was impossible to debug by looking for messages in the Event Viewer. I put a try/catch and dumped the exception out to a file. I had to change the settings on my logs to fill as needed instead of every 7 days and this allowed the services to start.
Of course, the root of the problem for me is that I have a nVidia driver issue that is flooding my event logs and now I'm probably beating on the disk, but that's another issue.
Maybe you need to run the service as Local System Account. See this post by Srinivas Ganaparthi.
I had the same issue starting JBoss, then I changed the JAVA_HOME variable, it worked for me. It was the JBoss version that doesn't support the 1.6, it supports 1.5.
I had similar problem and it turned out in my case that the program simply crashed in OnStart method. It tried to read some file that it couldn't find but I suppose that any other program crash would give the same result. In case of Windows forms application you would get some error message but here it was just "your service started and stopped"
If you ever need, like me to read some files from the directory where Windows Service .exe is located, check this topic:
Getting full path for Windows Service
In my case, a method in my service, was being called recursively (as no terminate condition being true) and after specific time my service was being stopped.

WCF service hosted in Windows service - not working

I've created the WCF service and some simple WPF application consuming it. When I'm running the project from within Visual Studio, the WCF Test Client opens and the application works just fine, method defined in service work.
But I need to host this WCF service in a Windows Service. I've followed this, installed the services using Installutil.exe and the ran the service. Everything went fine, it's working.
Yet, when I'm trying to open the executable file with WPF application directly from the debug folder of the app, I'm getting this error:
zad8. has stopped working
After choosing the option to debug it with new instance of VS I get
XamlParseException occured in PresentationFramework.dll
The stack trace shows something like:
connection can't be started, because the target computer is actively refusing it
Do you have any idea what could go wrong?
Fortunately, I've managed to come up with solution. I think I should post it, maybe one day it will help somebody:)
I actually did two mistakes, but one of them was unfortunately caused by the mentioned tutorial (here) in connection with my temporary blackout.
In step 5, point 8 of this tutorial, there's an example of overriding OnStart() method:
protected override void OnStart(string[] args)
{
if (myServiceHost != null)
{
myServiceHost.Close();
}
myServiceHost = new ServiceHost(typeof(Service1));
myServiceHost.Open();
}
Beware, that Service1 is ambiguous in this context, because it's name of the Windows Service project class as well as the name of WCF Service class. It should be written with fully qualified name (here it is WcfServiceLibrary1.Service1). In my case, the service name was different, and I just put the Service1 in there in a hurry. Anyway..
In case, someone has it all behind and still encounters the same problem (with app stopped working), I think that you should try open the project in Visual Studio and try to debug the client consuming application as a new instance (right click on the project-> Debug -> Start as new instance...).
It might seem trivial, but when u hit F5 or Ctrl+F5 then even if u have only those project set as startup project, VS will host it's client anyway. In my case it did matter, because I needed to use isolation storage file. And as it was kept on the service side, then I had this file created in IIS server created by VS. Somehow, my method of creating such file had set FileMode.Open() and it was causing the crush, because in Windows Service it didn't exist and the new one couldn't be created and that was neccessary to run it correctly.
What's more it just showed me that this question couldn't be answered properly, cause the data I've provided was not enough and it was delicate.
Cheers:)

What is the proper way for a Windows service to fail during its startup

I need my service to check for existence and structure of certain files during its startup and exit/fail/stop if some conditions aren't met.
I read this thread: What is the proper way for a Windows service to fail?
but it does not help.
I set the ServiceBase.ExitCode property non-zero and then call ServiceBase.Stop. But I get 5 event log entries. See below:
Starting service. (I log this event via code)
Config.xml file not found. (I log this ERROR event via code)
Service stopped successfully. (SCM logs this message)
Service started successfully. (SCM logs this message)
Service cannot be started. The handle is invalid (SCM logs this message)
As you see everything goes OK except for the last two entries. Why are they there? What can I do to properly shutdown the service during startup? Why doesn't SCM see the service as stopped/failed?
You don't provide enough code to really know, but I suspect you are trying to validate the service and stop it in either the constructor or the OnStart. The way I like to handle services is start my timer in the OnStart. In the first interval of the timer I can validate all the code, if its invalid close the Service. If its valid, reset the interval of the timer to how frequently I want it to run then set a bool that tells it not to check for validity of files again.
What is the return code you are using for your ExitCode? If it matches the corresponding windows ExitCode, then that is what will be recorded by SCM. I'm assuming you are returning a 6 for your ExitCode.
The other thing is if you can run on Default values do that, let Config.xml be missing and just record the problem in the EventLog. "Configuration file Missing"
If you really want it to just abort during OnStart, set your ExitCode and then Throw an Exception (InvalidArgumentException, InvalidOperationException) for example
This article also has some good advice. .NET: Which Exception to Throw When a Required Configuration Setting is Missing?
You are trying to start second instance of service (another service registered for the same .exe)

Parser Error: Could not create type 'xyz' in ASP.NET Generic Web Handler

I have a ASP.NET Generic Web Handler (*.ashx) in my Web Application Project. It's path on the web server is /Services/PayPal/IPNListener.ashx.
IPNListener.ashx
<%# WebHandler Language="C#"
CodeBehind="IPNListener.ashx.cs"
Class="Portal.Services.PayPal.IPNListener" %>
IPNListener.ashx.cs
//-----------------------------------------------------------------------------
// <copyright file="IPNListener.cs" company="DCOM Productions">
// Copyright (c) DCOM Productions. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Web;
using Portal.PayPal;
namespace Portal.Services.PayPal
{
/// <summary>
/// Summary description for IPNListener
/// </summary>
public class IPNListener : IHttpHandler
{
/// <summary>
/// Receives the HttpWebRequest via an HttpContext and processes the request
/// </summary>
public void ProcessRequest(HttpContext context) {
// I gutted this, because it still produces the same error
context.Response.Write("Hello World!");
}
/// <summary>
/// Gets a System.Boolean value that indicates whether the generic handler may be reused
/// </summary>
public bool IsReusable {
get {
return false;
}
}
}
}
When I attempt to navigate to the handler, I receive the following error.
Parser Error
Description: An error occurred during the parsing of a resource required to service
this request. Please review the following specific parse error details
and modify your source file appropriately.
Source Error
Line 1: <%# WebHandler Language="C#"
CodeBehind="IPNListener.ashx.cs"
Class="Portal.Services.PayPal.IPNListener" %>
Source File: /portal/services/paypal/ipnlistener.ashx Line: 1
Here is what I have done so far:
(1) Gutted the Handler. I gutted it to a simple Hello World handler as seen in the code I posted to eliminate the possibility of an error in my code. The handler was previously working just fine, and I'm actually not sure what has caused it to stop working.
(2) Checked Version Control. Like I said previously, the handler was working fine a day or so ago. I checked version control for the file's history to see if anything had changed, and it has not.
(3) Application Pool. I checked the application pool in IIS 7, and it is running ASP.NET v4.0 as it should be.
(4) Verified Build Actions. I verified that IPNListener.ashx's build action is set to Content, and that IPNListener.ashx.cs's build action is set to Compile.
(5) Verified DLL Integrity. I verified using IL Disassembler that the type Portal.Services.PayPal.IPNListener does exist in the compiled DLL file.
(6) Virtual Application Directory. The folder /Services/PayPal is not a virtual application, and I've never had to make it one for a Generic Handler to work, but I tried it anyway and the same error is still produced.
(7) Clean Rebuild. I have tried cleaning the solution, then rebuilding. There are no compiler errors. Then proceed to publish the web application, everything is fine.
(8) Checked Dependencies. I checked to make sure all required files, and dependencies are deployed. Everything checks out.
I really have no clue as to why this stopped working. Everything was tested and ready to go to publish everything live today for my first software product, and when I did one last test run on the payment services it was returning a 500 Internal Server Error, in which further investigation showed that it is being caused by this problem.
I'm actually really stumped. I searched through all other posts regarding ASHX handlers and this issue here on StackOverflow, as well as all over the web and found nothing useful to my specific issue here.
It might be something really simple I am overlooking, but I can't seem to put my finger on it.
(Update) 5/29/2012 10:57 PM. Going off of Ray Cheng's comment and mine, I realized that the service used to live in /PayPal/IPNListener.ashx, and was moved to /Services/PayPal/IPNListner.ashx. When the move was done, all the namespaces, and class locations were updated in the ASHX file.
Out of curiosity, I copied the handler to /PayPal/IPNListener.ashx, updated the namespaces and classes in the ASHX files, and navigated to the location, and the service works fine.
Essentially
/Services/PayPal/IPNListener.ashx
produces a 500 Internal Server Error due to Could not create type, and
/PayPal/IPNListener.ashx
works just fine. So why does having the handler one folder hierarchy up make a difference in working?
The problem was something simple. I have two IIS 7 servers, one for local testing, and the other live on the internet.
The local IIS server had the parent directory /Services set as an virtual application. I found that having a virtual application somewhere in the path to the generic handler causes the ASP engine to fail to create the handler type. This explains why moving the service from /PayPal/IPNHandler.ashx to /Services/PayPal/IPNHandler.ashx broke, because the former path was not a virtual application, and the latter was, breaking the handler.
Removing the virtual application for /Services resolved the problem on the test server, and it now works as expected.
There wasn't supposed to be a virtual application in that path anyway, but it was an oversight because /Services is used for more than just this handler.
The problem on the live server, turns out, was an service address to a WCF Service that is used in the handler was pointing to the test server using the localhost alias, thus being an invalid address. The live server did not have any problems with virtual application configuration or anything, it was an incorrect address.
Updating the incorrect address resolved the 500 Internal Server error. This is irrelevant as to the original issue of the handler causing a parse error, but was the second issue found and resolved before being able to have everything work.

XML namespace problem in Visual Studio generated service reference

I'm connecting to a web service hosted by a third-party provider. I've added a service reference in my project to the web service, VS has generated all the references and classes needed.
I'm connecting with this piece of code (client name and methods anonymized):
using (var client = new Client())
{
try
{
client.Open();
var response = client.Method(...);
return response.Status;
}
catch (SoapException ex)
{
throw CreateServiceException(ex);
}
finally
{
client.Close();
}
}
When reaching the client.Open(), I get an exception with this message:
The top XML element '_return' from
namespace '' references distinct types
System.Boolean and
Service.Status.
Use XML attributes to specify another
XML name or namespace for the element
or types.
In reference.cs, I can see that the "_return" variable is decorated with
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]
Is there a problem with the wsdl, the generated service reference or in my code?
Update: Generating the service as an old school Web Service solves the problem. I've marked Sixto's answer as accepted for now, but I'm still curious what could've caused the problem and if any parameters to the service generator could solve the original problem.
If you were able to create a service reference then the WSDL is valid. The exception message is saying you have namespace/type ambiguity problem with _return. The generated code is probably using it in some context as a boolean and in another context as a Service.Status type.
I don’t call the ClientBase.Open method before invoking a service method because I’ve never seen the need for it. I do always call the Close & Abort methods as appropriate. The Open method basically just changes the state of the client to no longer be configurable. I’m not sure how that would trigger code in the generated class since it is an inherited method. I’d try just removing that line and see if you get the same exception. Otherwise, if you haven’t already done so, search the generated code for all the places _return is used and see if you can manually sort-out the appropriate type. You may need different names for each context.
Another way to troubleshoot the WSDL is to create a Web Reference (assuming it’s an HTTP based service) and see if the generate code works as expected. If it does work, go with the ASMX client unless you have a need for WCF proxy capabilities.

Categories

Resources