简体   繁体   English

在Windows服务上使用.NetCore类库

[英]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. 我想创建一个Windows服务,该服务可重用在.NETCore类库上创建的类。 I have a sample project.json from one of my .NETCore class libraries as follows: 我有一个来自.NETCore类库的示例project.json,如下所示:

{
  "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/ . 我一直在遵循这种方法http://taskmatics.com/blog/run-dnx-applications-windows-service/ I have created a console application and changed its project.json file as indicated. 我创建了一个控制台应用程序,并按照指示更改了其project.json文件。 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: 这是我用于Windows服务应用程序的project.json文件:

 {
  "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? 有没有一种方法可以为我的服务使用.NETCore类?

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) 那是因为NETStandard.Library 1.6.0不支持4.5.1,它仅支持4.6.3 +(。NET Framework的)

Your class library need to down to .netstandard 1.2. 您的类库需要降至.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. 问题是, netcoreapp1.0类库框架是不兼容的dnx451在控制台应用程序框架。 Replacing both with net451 should work in this particular case . 在这种特殊情况下,net451替换两者都可以。

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. 注意:如果不需要是NetStandard库,则可以删除netcoreapp1.0dependencies节点。

{
  "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. dnx 现在已过时 ,已被新的.Net CLI取代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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