我对C#应用程序开发很陌生。
我已经制作了一个C#控制台应用程序,我按照这样执行
dotenet.exe <dll_name>
在系统上执行时,此功能正常。但我需要远程调用它。我是通过Powershell Invoke Command来做到这一点的。但是我收到了这个错误。
System.Runtime.InteropServices.COMException:检索COM类 带有CLSID的组件的工厂 {D66FBAAE-4150-402F-8581-75D1652D696A}由于以下原因而失败 错误:80080005服务器执行失败(来自HRESULT的异常: 0x80080005(CO_E_SERVER_EXEC_FAILURE))。在 System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔值 publicOnly,Boolean&amp; canBeCached,RuntimeMethodHandleInternal&amp;构造函数)
在System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean skipCheckThis,Boolean fillCache,StackCrawlMark&amp; stackMark)at System.Activator.CreateInstance(Type type,Boolean nonPublic)at System.Activator.CreateInstance(类型类型)
请注意,我在网上尝试了许多可能的解决方案:
有关环境的详细信息:
修改
请找到dll的源代码。这用于将文件转换为格式。
using System.Runtime.InteropServices;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.IO;
using System.Linq;
public class SolidWorkFileConverter
{
private SldWorks swApplication;
public SolidWorkFileConverter(SldWorks swApp)
{
swApplication = swApp;
}
public void ToPdf(string searchPath, string logFileName)
{
var logFileDestionation = System.IO.Path.Combine(searchPath, logFileName);
var currentDate = DateTime.Now;
var partFiles = System.IO.Directory.EnumerateFiles(searchPath, "*.sldprt", SearchOption.AllDirectories)
.Where(file => !file.Contains("~$"));
foreach (var drawingFile in partFiles)
{
ToStep(drawingFile);
}
WriteToLog(logFileDestionation, currentDate);
}
public void ToStep(string partFile)
{
string destinationDirectory = Path.GetDirectoryName(partFile);
destinationDirectory = destinationDirectory + "\\output\\";
string destinationFile = Path.GetFileName(partFile);
string destinationFileName = ComposeDestinationFileName(Path.Combine(destinationDirectory, destinationFile));
CreateDestinationDirectory(destinationFileName);
int state = 0;
bool status;
int warningState = 0;
ModelDoc2 swDoc = (ModelDoc2)(swApplication.OpenDoc6(partFile, 3, 0, "", ref state, ref warningState));
int warnings = 0;
int errors = 0;
PartDoc swPart = (PartDoc)swApplication.OpenDoc6(partFile, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
ModelDoc2 swModel = (ModelDoc2)swPart;
ModelDocExtension swModelDocExt = (ModelDocExtension)swModel.Extension;
status = swModelDocExt.SaveAs(destinationFileName, 0, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref errors, ref warnings);
swDoc = null;
swApplication.CloseAllDocuments(true);
}
private string ComposeDestinationFileName(string sourceFileName)
{
return sourceFileName.Replace("PARTS", "STEP").Replace("SLDPRT", "STEP");
}
private void CreateDestinationDirectory(string destinationFileName)
{
string directoryName = System.IO.Path.GetDirectoryName(destinationFileName);
System.IO.Directory.CreateDirectory(directoryName);
}
private void WriteToLog(string logFileDestination, DateTime currentDate)
{
System.IO.File.WriteAllText(logFileDestination, currentDate.ToShortDateString());
}
}
static class Program
{
static void Main(string[] args)
{
string username = "";
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "-user")
{
username = args[i + 1];
}
}
string searchPath = "some path"
string logFileName = System.IO.Path.Combine(searchPath, "UpdatedPDFs.txt");
//SldWorks swApp = new SldWorks();
SldWorks swApp = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as SldWorks;
SolidWorkFileConverter converter = new SolidWorkFileConverter(swApp);
converter.ToPdf(searchPath, logFileName);
}
}
编辑2
用于远程调用的powershell命令
Invoke-Command -ComputerName mycomputer -ScriptBlock { & dotnet.exe convert.dll } -credential mydomain\administrator