如何在.NET核心应用程序中使用.NET核心类库?

时间:2017-05-04 03:06:01

标签: c# .net-core

特别是在ubuntu上,这似乎不起作用。

例如,如果我这样做:

dotnet new sln -n HelloWorld
dotnet new classlib -n HelloLib
dotnet new console -n HelloApp
dotnet sln add ./HelloApp/HelloApp.csproj
dotnet sln add ./HelloLib/HelloLib.csproj
dotnet restore
dotnet build

cd HelloApp/
dotnet add reference ../HelloLib/HelloLib.csproj

将Program.cs修改为:

using System;
using HelloLib;

namespace HelloApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = new Class1();
            Console.WriteLine("Hello World!");
        }
    }
}

然后应用程序将编译,生成这些工件:

HelloApp/bin$ du -a
4   ./Debug/netcoreapp1.1/HelloApp.deps.json
4   ./Debug/netcoreapp1.1/HelloApp.runtimeconfig.json
4   ./Debug/netcoreapp1.1/HelloApp.pdb
4   ./Debug/netcoreapp1.1/HelloLib.pdb
8   ./Debug/netcoreapp1.1/HelloApp.dll
4   ./Debug/netcoreapp1.1/HelloApp.runtimeconfig.dev.json
4   ./Debug/netcoreapp1.1/HelloLib.dll
36  ./Debug/netcoreapp1.1
40  ./Debug
44  .

...但执行应用程序失败:

$ dotnet HelloApp.dll 

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'HelloLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

Aborted (core dumped)

如果我删除using HelloLib行,则可以:

$ dotnet HelloApp.dll 
Hello World!

该怎么做?

我认为它与项目文件中令人困惑的不兼容性有关:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework> <-- **THIS**
  </PropertyGroup>

</Project>

VS

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework> <-- **And THIS**
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\HelloLib\HelloLib.csproj">
      <Project>{1607a379-5bae-423b-8efc-796a06556be0}</Project>
      <Name>HelloLib</Name>
    </ProjectReference>
  </ItemGroup>
</Project>

我认为这只是一个错误。

...但其他人似乎没有太多麻烦使用.NET核心。

那么,我做错了吗?

或者是人们使用&#39; .NET核心只是不使用类库,因为它们不起作用?

(编辑:版本:

Microsoft .NET Core Shared Framework Host

  Version  : 1.1.0
  Build    : 928f77c4bc3f49d892459992fb6e1d5542cb5e86

$ dpkg -l |grep dotnet
ii  dotnet-dev-1.0.3                                                 1.0.3-1                                      amd64        .NET Core SDK 1.0.3

$ cat /etc/issue
Ubuntu 16.10 \n \l

1 个答案:

答案 0 :(得分:5)

完成后

func shareCardDetail(_ cardModel: CardModel) {
        let shareText = "What do you think about this?"
        let branchUniversalObject = BranchUniversalObject(canonicalIdentifier: ("cardId/\(cardModel.id)"))
        branchUniversalObject.title = cardModel.title
        branchUniversalObject.contentDescription = ""
        branchUniversalObject.imageUrl = cardModel.photoURLsProperties.placeVenuePhoto

        branchUniversalObject.addMetadataKey(CardKeys.cardID.rawValue, value: cardModel.id)

        if let placeAvatarURLString = cardModel.photoURLsProperties.placeVenuePhoto {
            branchUniversalObject.addMetadataKey("placeAvatarURLString", value: placeAvatarURLString)
        }
        branchUniversalObject.addMetadataKey("title", value: cardModel.title)
        branchUniversalObject.addMetadataKey("isAutoGeneratedCard", value: "false")

        let fullLocationName = cardModel.location.fullLocationName
        branchUniversalObject.addMetadataKey("fullLocationName", value: fullLocationName)

        branchUniversalObject.addMetadataKey(CardKeys.ownerID.rawValue, value: cardModel.ownerID)
        branchUniversalObject.addMetadataKey(ParametersKeywords.type.rawValue, value: ModeKeywords.shareCard.rawValue)

        branchUniversalObject.addMetadataKey("availableSeats", value: cardModel.peopleProperties.availableSeats.description)

        if let coordinate = cardModel.location.coordinate {
            branchUniversalObject.addMetadataKey("latitude", value: coordinate.latitude.description)
            branchUniversalObject.addMetadataKey("longitude",value: coordinate.longitude.description)
        }


        let linkProperties = BranchLinkProperties()
        linkProperties.feature = "sharing"
        linkProperties.addControlParam("$desktop_url", withValue: "http://www.example.com")
        linkProperties.addControlParam("$ios_url", withValue: "example://")

        guard let topViewController = UIApplication.topViewController() else { return }

        branchUniversalObject.showShareSheet(with: linkProperties, andShareText: shareText, from: topViewController) { (text, success) in
            debugPrint("done showing share sheet!")
        }
    }

你必须做一个额外的

dotnet add reference ../HelloLib/HelloLib.csproj

因此项目将正确跟踪引用。如果您按照相同的步骤操作,但在dotnet restore 步骤后添加dotnet restore则可以正常工作。

每当您更改或更新您的引用时,您必须在之后致电dotnet add reference或将dotnet restore声明更改为dotnet build,以便它在您构建之前执行还原。