简体   繁体   English

引用ASP.NET Core服务中的Service Fabric Actor类的编译器错误

[英]Compiler error referencing Service Fabric Actor class from ASP.NET Core service

This has been bugging me all day and I have yet to figure out why this is happening. 这整天困扰着我,但我还没有弄清楚为什么会这样。

Basic scenario 基本方案

  1. Created a new Service Fabric Application with an ASP.NET Core service and added support for returning MVC views so that it can act as the UI for my SF App. 使用ASP.NET Core服务创建了一个新的Service Fabric应用程序,并增加了对返回MVC视图的支持,以便它可以用作我的SF App的UI。

  2. Added an Actor service for users to the solution which created the UserActor and UserActor.Interfaces projects and ensured that the Interfaces library targets x64 as required by SF. 在创建UserActorUserActor.Interfaces项目的解决方案中,为用户添加了Actor服务,并确保Interfaces库按SF的要求针对x64。

  3. Defined a Task<UserProfile> GetProfileData() method on the UserActor where the UserProfile type is a concrete class defined in the Interfaces project with the [DataContract] and [DataMember] attributes so that it can be serialized (all members are primitives). 在UserActor上定义了一个Task<UserProfile> GetProfileData()方法,其中UserProfile类型是在Interfaces项目中使用[DataContract][DataMember]属性定义的具体类,以便可以对其进行序列化(所有成员都是基元)。

  4. Added a project reference to the UserActor.Interfaces from the ASP.NET Core project so I could create the actor proxy and invoke the method. 从ASP.NET Core项目添加了对UserActor.Interfaces的项目引用,因此我可以创建actor代理并调用该方法。

  5. Added the code in the ASP.NET Core Controller class to connect to the actor instance and invoke the GetProfileData method 在ASP.NET Core Controller类中添加了代码以连接到actor实例并调用GetProfileData方法

At this point, I receive the following compiler error: 此时,我收到以下编译器错误:

error CS0246: The type or namespace name 'UserProfile' could not be found (are you missing a using directive or an assembly reference?) 错误CS0246:找不到类型或名称空间名称'UserProfile'(您是否缺少using指令或程序集引用?)

The relevant lines of code are as follows: 相关的代码行如下:

using UserActor.Interfaces;

var user = ActorProxy.Create<IUserActor>( new ActorId( model.email ), FabricAppUrl );
UserProfile profile = user.GetProfileData().Result; // compiler error here

If I hover over the UserProfile type on the last line, Visual Studio shows me it sees the type as UserActor.Interfaces.UserProfile and doesn't show any errors in the editor. 如果我将鼠标悬停在最后一行的UserProfile类型上,Visual Studio将向我显示它将类型视为UserActor.Interfaces.UserProfile ,并且在编辑器中不显示任何错误。 It's only when I compile that this error shows up. 只有在我编译时才会出现此错误。

I didn't receive this error when trying to return a primitive type and it had no problem resolving the IUserActor interface for the Actor Proxy either. 尝试返回原始类型时,我没有收到此错误,并且为Actor Proxy解析IUserActor接口也没有问题。

Does this have something to do with the fact the the Actor projects are standard .NET projects and the ASP.NET Core project is compiled using dotnet build (set to .NETFramework,Version=v4.5.2 ; not dotnet core). 这是否与Actor项目是标准.NET项目以及ASP.NET Core项目是使用dotnet构建 (设置为.NETFramework,Version = v4.5.2 ;不是dotnet core)进行编译的事实有关

I also noticed in the build output that the ASP.NET Core project is building as 'Debug Any CPU' while all of the other projects are set to 'Debug x64'. 我还在生成输出中注意到,ASP.NET Core项目正在以“调试任何CPU”的形式进行构建,而所有其他项目都设置为“ Debug x64”。 If this were the issue I would expect to receive an error about mis-matched architecture types, but I'm not getting that either. 如果这是问题,我希望收到关于不匹配的体系结构类型的错误,但是我也没有得到。

How can I resolve this? 我该如何解决? Should I be returning the data from the Actor in a different manner (such as json serializing it before returning it as a string primitive)? 我是否应该以其他方式从Actor返回数据(例如将json序列化后再作为字符串基元返回)?

Edit 1: 编辑1:

The code sample above was too simplistic. 上面的代码示例过于简单。 The 2 compiler errors I am seeing are around the type being passed to a helper method in the same controller as follows: 我看到的2个编译器错误与在同一控制器中传递给helper方法的类型有关,如下所示:

private bool TryGetUserProfile( LoginModel model, out UserProfile profile )
{
    var user = ActorProxy.Create<IUserActor>(new ActorId(model.Email), FarbicAppUrl);
    var profile = user.GetProfileData().Result;

    return !string.IsNullOrEmpty(profile.Name);
}

OK, after starting the whole scenario out on a different machine I found what the problem is and believe it to be a Microsoft tooling issue. 好的,在另一台计算机上启动整个方案之后,我发现了问题所在,并认为这是Microsoft工具问题。 Here are the steps to reproduce the problem along with what needs to be done to correct it. 这里是重现问题的步骤以及需要采取的纠正措施。

  1. Create a new Service Fabric project and select ASP.NET Core as the service. 创建一个新的Service Fabric项目,然后选择ASP.NET Core作为服务。 If prompted, select the Web Application Template (but I think this would be an issue if you selected the Empty or Web API templates as well). 如果出现提示,请选择Web应用程序模板(但是,如果您还选择了Empty或Web API模板,我认为这将是一个问题)。 If you debug this, it will work just fine (provided you have a local cluster setup). 如果您对此进行调试,那么它将正常工作(前提是您具有本地集群设置)。

  2. Add a new Actor Service to the solution. 将新的Actor服务添加到解决方案。 This will create the UserActor and UserActor.Interfaces projects for you. 这将为您创建UserActorUserActor.Interfaces项目。 If you try to compile at this point, you will receive the following error message: 如果您此时尝试进行编译,您将收到以下错误消息:

C:\\Program Files (x86)\\MSBuild\\bin\\Microsoft.Common.CurrentVersion.targets(724,5): error : The OutputPath property is not set for project 'UserActor.Interfaces.csproj'. C:\\ Program Files(x86)\\ MSBuild \\ bin \\ Microsoft.Common.CurrentVersion.targets(724,5):错误:未为项目'UserActor.Interfaces.csproj'设置OutputPath属性。 Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. 请检查以确保为该项目指定了配置和平台的有效组合。 Configuration='Debug' Platform='AnyCPU'. 配置=“调试”平台=“任何CPU”。 This error may also appear if some other project is trying to follow a project-to-project reference to this project, this project has been unloaded or is not included in the solution, and the referencing project does not build using the same or an equivalent Configuration or Platform. 如果某个其他项目试图遵循该项目的项目间引用,此项目已被卸载或未包含在解决方案中,并且引用项目未使用相同或等效项构建,则也可能会出现此错误。配置或平台。

Checking the Configuration Manager shows that there is only an 'x64' target defined for the project. 检查Configuration Manager显示仅为项目定义了“ x64”目标。 To get around this, use the Configuration Manager and add a new 'AnyCPU' target and copy it from the 'x64' target. 要解决此问题,请使用配置管理器并添加一个新的“ AnyCPU”目标,然后从“ x64”目标中复制它。 Ensure that the target platform is still set to 'x64' for the interfaces project before closing the Configuration Manager since Service Fabric requires x64 assemblies. 由于Service Fabric需要x64程序集,因此在关闭配置管理器之前,请确保接口项目的目标平台仍设置为“ x64”。

  1. Now when you compile, you will receive the following error message: 现在,当您进行编译时,您将收到以下错误消息:

C:\\Program Files (x86)\\MSBuild\\Microsoft\\VisualStudio\\v14.0\\DotNet\\Microsoft.DotNet.Common.Targets(262,5): error : C:\\...\\Web error CS0006: Metadata file 'C:\\...\\UserActor.Interfaces\\bin\\Debug\\UserActor.Interfaces.dll' could not be found. C:\\ Program Files(x86)\\ MSBuild \\ Microsoft \\ VisualStudio \\ v14.0 \\ DotNet \\ Microsoft.DotNet.Common.Targets(262,5):错误:C:\\ ... \\ Web错误CS0006:元数据文件'找不到C:\\ ... \\ UserActor.Interfaces \\ bin \\ Debug \\ UserActor.Interfaces.dll'。

The clue here is that the web project is looking in the OutputPath location defined for the 'AnyCPU' target and not the location defined for the 'x64' target (bin\\x64\\Debug). 这里的线索是,Web项目正在查找为“ AnyCPU”目标定义的OutputPath位置,而不是为“ x64”目标定义的位置(bin \\ x64 \\ Debug)。 If you manually edit the interfaces project file and set the OutputPath for the 'AnyCPU' target to the same path as the 'x64' target, everything will compile properly. 如果您手动编辑interfaces项目文件,并将“ AnyCPU”目标的OutputPath设置为与“ x64”目标相同的路径,则所有内容将正确编译。

You can also deploy the project and everything will work as it should. 您还可以部署项目,一切都会正常进行。 I'm not sure which target file from Microsoft has the problem in it, but it could be both. 我不确定来自Microsoft的哪个目标文件存在问题,但可能两者都有。 I'll look into reporting this as a bug with Microsoft in the hopes of getting it resolved (still present with the latest SDK released on 2/3/2017). 我将研究将此问题报告为Microsoft的错误,以期解决该问题(仍与2017年2月3日发布的最新SDK一起存在)。

The reason the UserProfile type couldn't be found for me was because I had previously built the solution with the wrong OutputPath set before I added the UserProfile class to the project so the compiled DLL that MSBuild was looking at didn't contain the type. 无法为我找到UserProfile类型的原因是因为UserProfile类添加到项目之前,之前曾使用错误的OutputPath设置构建了解决方案,因此MSBuild正在查看的已编译DLL不包含该类型。 If I had removed the bin & obj folders from the solution, it would have become obvious where the problem was right away. 如果我从解决方案中删除了bin和obj文件夹,那么问题出在哪里就变得很明显了。

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

相关问题 从.Net Core无状态服务中调用.Net框架Service Fabric Actor - Calling .Net framework Service Fabric Actor from .Net Core Stateless Service Azure Service Fabric中的ASP.NET Core应用程序中的WebpackDevMiddleware - WebpackDevMiddleware in ASP.NET Core app in Azure Service Fabric 如何在Service Fabric ASP.NET Core 2中使用WebListenerCommunicationListener - How to use WebListenerCommunicationListener in Service Fabric ASP.NET Core 2 如何在基于Service Fabric ASP.NET 5的服务中扩展从StatelessService派生的类? - How do I extend class derived from StatelessService in Service Fabric ASP.NET 5 - based service? 在ASP.NET Core中使用服务类 - Using a service class with asp.net core 服务结构无状态服务Asp.Net Core- Oauth2.0承载令牌错误 - Service Fabric Stateless Services Asp.Net Core- Oauth2.0 Bearer Token Error 在Service Fabric上从一个ASP.NET Core网站公开两个端点 - Exposing two end points from one ASP.NET Core Website On Service Fabric 从服务结构引导 Asp.net web api - Bootstrapping Asp.net web api from service fabric 如何在Azure Service Fabric ASP.NET Core有状态服务中获取PartitionInfo? - How to obtain PartitionInfo in an Azure Service Fabric ASP.NET Core stateful service? 在 ASP.NET Core 中的 Startup 类中访问服务 - Accessing service in Startup class in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM