简体   繁体   中英

Use .NetCore class library on windows service

I want to create a windows service that reuses the classes that I have created on .NETCore class libraries. I have a sample project.json from one of my .NETCore class libraries as follows:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [ "dnxcore50", "portable-net451+win8" ]
    }
  }
}

I have been following this approach http://taskmatics.com/blog/run-dnx-applications-windows-service/ . I have created a console application and changed its project.json file as indicated. However, whenever I try to declare my other class libraries as a dependency I get an error indicating

'The dependency could not be resolved'

This is the project.json file that I am using for my windows service application:

 {
  "version": "1.0.0-*",

  "dependencies": {
    //"MyClassLibrary": "1.0.0-*",
    //When uncommenting everything fails
  },

  "frameworks": {
    "dnx451": {
      "dependencies": {
      },
      "frameworkAssemblies": {
        "System.ServiceProcess": "4.0.0.0"
      }
    }
  }
}

Is there a way that I can use .NETCore classes for my service?

Yep! That's because NETStandard.Library 1.6.0 doesn't support 4.5.1, it only supports 4.6.3+ (of the .NET Framework)

Your class library need to down to .netstandard 1.2. More details, you can see at here

The problem is that the netcoreapp1.0 framework in the class library is not compatible with the dnx451 framework in your console application. Replacing both with net451 should work in this particular case .

Console App

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "MyClassLibrary": "1.0.0-*"
  }

  "frameworks": {
    "net451": {
      "frameworkAssemblies": {
        "System.ServiceProcess": "4.0.0.0"
      }
    }
  }
}

Your Class Libraries

Note: You can remove the netcoreapp1.0 and dependencies nodes if this doesn't need to be a NetStandard library.

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [ "dnxcore50", "portable-net451+win8" ]
    },
    "net451": { }
  }
}

The tutorial you are following is quite old. dnx is now obsolete and has been replaced with the new .Net CLI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM