Giter Site home page Giter Site logo

Comments (9)

ragnarol avatar ragnarol commented on September 6, 2024 1

I have a similar error and turn out to be the way I was accessing my appsettings.json.

Take into account that the base folder your service will be running from won't be the folder were the assembly is.

In my case I changed:

                var configuration = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsetings.json", false, false)
                    .Build();

For

                var configuration = new ConfigurationBuilder()
                    .AddJsonFile("appsetings.json", false, false)
                    .Build();

And worked like a charm

from dotnetcore.windowsservice.

ragnarol avatar ragnarol commented on September 6, 2024 1

Yeah, the service is running from "C:\Program Files\dotnet", i wonder if it would be better to wrap the executable in a cmd and run it as the service so the base path is the one where the dll lives.

from dotnetcore.windowsservice.

ragnarol avatar ragnarol commented on September 6, 2024 1

I have ended up doing

Directory.SetCurrentDirectory(PlatformServices.Default.Application.ApplicationBasePath);

At the start of my process and that seems to make the service behave as I was expecting. I will try to create a pull request to include this in the base service start.

from dotnetcore.windowsservice.

xVishera avatar xVishera commented on September 6, 2024

I'm using at the moment

            #region appSettings Initialization
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("appsettings.json")
               .AddEnvironmentVariables();

I'm compiling right now with

var builder = new ConfigurationBuilder()
               .AddJsonFile("appsettings.json")
               .AddEnvironmentVariables();

To test.

from dotnetcore.windowsservice.

xVishera avatar xVishera commented on September 6, 2024

As you said, worked like a charm

from dotnetcore.windowsservice.

gitfortee avatar gitfortee commented on September 6, 2024

@ragnarol could you please help me understand what does Directory.SetCurrentDirectory do in the context of this issue and why it is needed.?

My code looks like shown below. I have so far not deployed it outside of my dev machine. Can you suggest if the code shown below needs any changes and will it be helpful to put Directory.SetCurrentDirectory as the first instruction in the InitializeConfiguration method?

private static void InitializeConfiguration()
{
	var builder = new ConfigurationBuilder()
	 .SetBasePath(Directory.GetCurrentDirectory())
	.AddJsonFile("appsettings.json");

	Configuration = builder.Build();
}

public static void Main(string[] args)
{
	InitializeConfiguration(); 			
}

from dotnetcore.windowsservice.

xVishera avatar xVishera commented on September 6, 2024

@gitfortee Just remove .SetBasePath(Directory.GetCurrentDirectory())

The SetBasePath helps in dev to specify the path where the engine will find the appsettings, but when you publish it the service "starts" at a differently place, If you call "setbasePath" it cant find the appsettings and timesout.

from dotnetcore.windowsservice.

ragnarol avatar ragnarol commented on September 6, 2024

Latest version (2.0,1) should handle the path by itself and use the directory were the assembly is located as the base path, no need of setting it manually.

from dotnetcore.windowsservice.

gitfortee avatar gitfortee commented on September 6, 2024

Thanks.

from dotnetcore.windowsservice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.