WP Silverlight BackgroundTask与Azure通信

你好,我有一个WP8.1的Silverlight项目,这是一个游戏.我使用backgroundTask来提醒用户他上次玩的时间,如果他太慢而无法遵守我想从bagroundTask打电话给服务器从游戏中退出.

backgroundTask是使用stackoverflowthis blog创建的,只是backgroundTask的基本实现.

然后我安装了NuGet包Microsoft.Azure.Mobile.Client,一切都很好,然后我做了一个编译,一切都成功了.

出现应用程序安装和初始屏幕,然后在App.xaml.cs中初始化MobileServiceClient时在主项目中出现错误

 public MobileServiceClient MobileService = new MobileServiceClient(
        "https://xxxxx.azurewebsites.net"
    );

上述行中的例外情况说明:

An exception of type ‘System.IO.FileNotFoundException’ occurred in mscorlib.ni.dll but was not handled in user code
Additional information: Could not load file or assembly ‘System.Runtime.InteropServices, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The system cannot find the file specified.

唯一的变化是在backgroundTask项目中安装了NuGet包.上面的异常的堆栈跟踪输出.

at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at 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)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean isDecoratedTargetSecurityTransparent)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType)
at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit)
at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit)
at Microsoft.WindowsAzure.MobileServices.PlatformInformationExtensions.GetVersionFromAssemblyFileVersion(IPlatformInformation platformInformation)
at Microsoft.WindowsAzure.MobileServices.PlatformInformation.get_Version()
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.GetUserAgentHeader()
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient..ctor(IEnumerable`1 handlers, Uri applicationUri, String installationId)
at Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor(Uri mobileAppUri, HttpMessageHandler[] handlers)
at Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor(String mobileAppUri, HttpMessageHandler[] handlers)
at BC_Menu.App..ctor()

我无法解决这个问题.

额外

我可以使用proposed code对网络进行正常的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 before看到了一些关于silverlight项目的奇怪行为.

最佳答案 该错误表明您的.NET设置存在问题 – 错误发生在mscorlib(.NET的基本库)中.尝试添加对所需特定库的引用.另外,请确保已安装v2.0.1(或更高版本)或Microsoft.Azure.Mobile.Client库.

点赞