C# 以.exe dll未匹配的形式运行程序?(未找到方法)
我需要从服务内部获取计算机上智能卡上的姓名 我有一个程序将所有智能卡的名称写入一个文件C# 以.exe dll未匹配的形式运行程序?(未找到方法),c#,dll,windows-runtime,C#,Dll,Windows Runtime,我需要从服务内部获取计算机上智能卡上的姓名 我有一个程序将所有智能卡的名称写入一个文件 using System; using System.Threading.Tasks; using Windows.Devices.Enumeration; using Windows.Devices.SmartCards; namespace DeviceEnumerationConsoleTest { class Program { static void Main(st
using System;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.SmartCards;
namespace DeviceEnumerationConsoleTest
{
class Program
{
static void Main(string[] args)
{
EnumerateDevices().Wait();
}
static async Task EnumerateDevices()
{
try
{
string selector = SmartCardReader.GetDeviceSelector();
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
string ret = "start1 ";
foreach (DeviceInformation device in devices)
{
ret = ret + device.Name + " _ ";
}
System.IO.File.WriteAllText(@"C:\ProgramData\IDNORTH\cards.txt", ret);
}catch(Exception e)
{
System.IO.File.WriteAllText(@"C:\ProgramData\IDNORTH\error.txt", e.Message);
}
}
}
}
当我自己运行它时,这项工作非常棒
然而,当我从外部运行它时,它有时工作,有时返回
未找到方法:Void System.Threading.Tasks.Task.addToActivityTasks(System.Threading.Tasks.Task)
从第行开始:
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
我相信从错误来看,DLL存在一些问题。
在项目中,我添加了参考资料
C:\Program Files(x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd
及
C:\Program Files(x86)\Microsoft SDK\WindowsPhoneApp\v8.1\Tools\MDILXACompile\Framework\System.Runtime.WindowsRuntime.dll
我从服务调用exe,并且没有添加或更改其中的任何引用。它与添加到Project1中的windows参考相同。不确定这是否是过程,但我认为应该将其标记为已回答,因为它现在可以工作。 已将引用更改为C:\Program Files(x86)\reference Assembly\Microsoft\Framework.NETCore\v4.5\System.Runtime。WindowsRuntime.dll
感谢默认 我不确定这是否对你有帮助。因为你说它不起作用,但有时。这清楚地表明了某种“时机”问题。1.当您没有指定线程类型时,默认情况下它是“前台”线程,因此即使您没有“等待”,创建的线程仍然会等待“前台线程”完成。2.现在,windows服务“OnStart”有一个时间限制,它应该在15秒内完成其工作,并将控制权交还给操作系统,否则它将被视为失败,windows将中止该过程。我猜您的工作需要更多的时间,由于OnStart事件的时间限制,进程终止。是否正在同步project1.exe中完成的工作?如果在启动新的进程之前等待
进程.Start
完成,是否会出现同样的问题?Project1.exe或服务中是否出现异常?是否针对多个.net Framework?