WP Silverlight后台任务与Azure的通信

4
你好,我有一个WP8.1的银光项目是一个游戏。我使用backgroundTask来提醒用户他上次玩的时间,如果用户反应过慢,我希望从backgroundTask调用服务器以退出游戏。
这个backgroundTask是使用stackoverflowthis blog做的,只是backgroundTask的基本实现。
然后我安装了NuGet包Microsoft.Azure.Mobile.Client,一切正常,编译成功。
应用程序安装并显示闪屏,但在App.xaml.cs中初始化MobileServiceClient时出现主要项目错误。
 public MobileServiceClient MobileService = new MobileServiceClient(
        "https://xxxxx.azurewebsites.net"
    );

上面一行代码中的异常说明:

'System.IO.FileNotFoundException' 类型的异常在 mscorlib.ni.dll 中发生,但未在用户代码中处理 额外信息:无法加载文件或程序集 'System.Runtime.InteropServices, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其其中一个依赖项。系统找不到指定的文件。

唯一的更改是在 backgroundTask 项目中安装了 NuGet 包。上述异常的堆栈跟踪输出。

在System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)处 在System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)处 在System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)处 在System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)处 在System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)处 在System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean isDecoratedTargetSecurityTransparent)处 在System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType)处 在System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit)处 在System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit)处 在Microsoft.WindowsAzure.MobileServices.PlatformInformationExtensions.GetVersionFromAssemblyFileVersion(IPlatformInformation platformInformation)处 在Microsoft.WindowsAzure.MobileServices.PlatformInformation.get_Version()处 在Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.GetUserAgentHeader()处 在Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient..ctor(IEnumerable`1 handlers, Uri applicationUri, String installationId)处 在Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor(Uri mobileAppUri, HttpMessageHandler[] handlers)处 在Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor(String mobileAppUri, HttpMessageHandler[] handlers)处 在BC_Menu.App..ctor()处
我无法解决这个问题。 额外信息 我可以通过使用建议的代码来执行正常的http调用,这里进行复制:
BackgroundTaskDeferral _deferral;

public async void Run(IBackgroundTaskInstance taskInstance)
{
    _deferral = taskInstance.GetDeferral();
    // your async code
    _deferral.Complete();
}

根据Adrian Halls的回答更新

代码可以运行,我的前端应用程序可以与服务器通信,一切正常。backgroundTask可以从设备中提取信息,一切都很好,一切都编译和运行。

然后我将NuGet包添加到backgroundTask中,突然出现了上述错误,当我将此解决方案部署到客户端时。

我正在运行VS2015,Win10,并安装了Microsoft.Azure.Mobile.Client的v2.0.1版本。我曾经在win10之前看到过一些奇怪的silverlight项目行为。

1个回答

2
错误提示表明您的.NET设置存在问题 - 错误出现在mscorlib(.NET的基本库)中。尝试添加对您需要的特定库的引用。另外,请确保您已安装v2.0.1(或更高版本)或Microsoft.Azure.Mobile.Client库。

这是已安装的版本,我会尝试进行修改。因此,在一个项目中安装软件包将影响另一个项目的依赖关系?只是为了澄清一下? - JTIM
我现在有点不确定,因为我再次阅读了您的答案。代码可以工作,我的前端应用程序可以与服务器通信,一切都很好。backgroundTask可以从设备中提取信息,一切正常,一切都编译和运行。然后我将NuGet包添加到backgroundTask中,突然出现了上述错误。我正在运行VS2015,Win10,并安装了Microsoft.Azure.Mobile.Client的v2.0.1版本。 - JTIM
@JTIM 我认为后台任务必须始终是 WPA,而不是 Silverlight?你可以尝试将其放入单独的程序集中,并将目标更改为非 Silverlight 吗? - lindydonna
@lindydonna-msft 对不起,我会尽量更清楚。backgroundTask是一个WindowsPhone8.1项目(不是Silverlight)。主要项目是Silverlight项目。两者都正常工作。将nuget包添加到backgroundTask(非Silverlight项目)会影响主应用程序(Silverlight项目),并出现指定的错误。 - JTIM
@lindydonna-msft,你们中有人对此有经验吗? - JTIM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接