引用UWP和ASP.NET 5中的类库 [英] Reference a class library from UWP and ASP.NET 5

查看:217
本文介绍了引用UWP和ASP.NET 5中的类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类库,其中将包含WebAPI(使用ASP.NET 5)和消耗性UWP App的常见对象(主要是DTO).但是,我还没有弄清楚如何创建类库,以便可以在其他两个项目中引用它.

I am trying to create a class library that'll contain common objects (mainly DTOs) of a WebAPI (using ASP.NET 5) and a consuming UWP App. However, I have not yet figured out how to create the class library such that it can be referenced from both other projects.

到目前为止,我已经尝试过:
首先,我尝试了一个类库(包),该类库可在 Web 下找到.可以从ASP.NET项目中毫无问题地引用这种类型的库,但是当尝试从UWP项目中引用它时,我会收到以下消息:

What I have tried so far:
First, I tried a Class Library (Package), which can be found under Web. This type of library can be referenced from the ASP.NET project without problems, but when trying to reference it from the UWP project, I get the following message:

A reference to 'ClassLibrary1' could not be added. 

接下来,我尝试了一个类库(Windows Universal),该类库可在 Windows> Universal 下找到.这可以很容易地从UWP项目中引用,但是当尝试从ASP.NET引用时,我得到:

Next, I tried a Class Library (Windows Universal), which can be found under Windows > Universal. This can easily be references from the UWP project, but when trying to reference it from ASP.NET, I get:

The following projects are not supported as references : 

  - The project ClassLibrary2 has a target framework that is incompatible or has version higher than the current project and cannot be referenced.

因此:如何创建一个可在ASP.NET 5项目 UWP项目中使用的类库?

So: How can I create a class library that can be used in both an ASP.NET 5 project and an UWP project?

推荐答案

应将.Net Standard class library视为可移植类库的后继者.两者都旨在促进在不同的.Net平台之间共享代码(在这种情况下为模型对象/DTO).

The .Net Standard class library should be thought of as a successor to the portable class library. Both aim to facilitate sharing code (in this case model objects/DTOs) between different .Net platforms.

在Visual Studio 2017中,您现在可以选择.Net Standard类库模板.可以将此类库设置为针对.Net Standard的不同版本.您应该针对的.Net Standard版本取决于您希望在哪个.Net平台之间共享代码.

In Visual Studio 2017, you can now choose a .Net Standard class library template. This class library can bet set to target different versions of the .Net Standard. The version of the .Net Standard that you should target will depend upon what .Net platforms you wish share code between.

对于此特定问题,我们希望支持通用Windows平台(UWP)应用程序和Asp.Net Core应用程序.因此,我们应该参考兼容性表,以确定.Net标准的版本,将支持这两个平台.在这种情况下,它将为.Net Standard 1.4.将类库设置为以.Net Standard 1.4为目标之后,就可以从UWP和Asp.Net核心项目中添加对类库的引用.

For this particular question, we wish to support a Universal Windows Platform (UWP) app and Asp.Net Core app. So, we should refer to this compatibility table to determine the version of the .Net Standard that will support both of these platforms. In this case, it would be .Net Standard 1.4. Once you have set the class library to target the .Net Standard 1.4, you can add references to the class library from your UWP and Asp.Net core projects.

为了更好地理解什么是.Net标准以及它如何促进代码共享,我建议您阅读以下内容:

In order to better understand what the .Net Standard is and how it facilitates code sharing, I suggest reviewing the following:

  1. .NET标准简介
  2. .NET Standard上的视频系列(特别好)
  3. .Net标准常见问题解答
  1. Introducing .NET Standard
  2. Video Series on .NET Standard (this is particularly good)
  3. .Net Standard FAQ

这篇关于引用UWP和ASP.NET 5中的类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆