Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DocumentCollection.Open()函数不适用于AutoCAD API_C#_Wcf_Named Pipes_Autocad_Autocad Plugin - Fatal编程技术网

C# DocumentCollection.Open()函数不适用于AutoCAD API

C# DocumentCollection.Open()函数不适用于AutoCAD API,c#,wcf,named-pipes,autocad,autocad-plugin,C#,Wcf,Named Pipes,Autocad,Autocad Plugin,我正在为我的应用程序开发一个AutoCAD插件。我正在使用AutoCAD 2012。这个插件打开了名为pipe的.NET,所以我可以很容易地从我的桌面应用程序连接到它 首先,我创建了一个接口。给你 [ServiceContract] public interface IConnector { [OperationContract] [FaultContract(typeof(Exception))] void GetPdfVersion(string filePath,

我正在为我的应用程序开发一个AutoCAD插件。我正在使用AutoCAD 2012。这个插件打开了名为pipe的.NET,所以我可以很容易地从我的桌面应用程序连接到它

首先,我创建了一个接口。给你

[ServiceContract]
public interface IConnector
{
    [OperationContract]
    [FaultContract(typeof(Exception))]
    void GetPdfVersion(string filePath, string exportFilePath);
}
我的AutoCAD插件源自IExtensionApplication接口,所以我编写了这个初始化方法

this.host = new ServiceHost(typeof(Printer), new[] { new Uri("net.pipe://localhost") });
this.host.AddServiceEndpoint(typeof(IConnector), new NetNamedPipeBinding(), "GetPdfVersion");
this.host.Open();
在其中一个函数中,我需要打开文档并对其进行处理。 因此,我编写了以下代码

var docColl = Application.DocumentManager;
Document curDraw = null;
try
{
    if (File.Exists(@"d:\1.dwg"))
    {
        curDraw = docColl.Open(@"d:\1.dwg", true, string.Empty);
    }
}
catch (Exception e)
{
    Console.WriteLine(e);
}
但是它在
curDraw=docColl.Open(@“d:\1.dwg”,true,string.Empty)上抛出一个COM异常代码,HRESULT=-2147418113


我需要文档对象来处理dwg文件。是否有任何可能的方法来修复该错误?

AutoCAD无法从外部线程处理文档对象。这是问题的根源。如果我编写方法并放置CommandMethodAttribute-这将是可行的,但只能从AutoCAD控制台。。。但是,如果我需要从外部应用程序执行此操作,该怎么办? 首先,需要指定服务行为类的属性

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
因此,所有操作只能使用一个线程

下一步,在
Initialize()
方法中获取CurrentDispatcher对象并将其放入静态变量中

private static Dispatcher dispatcher;
public void Initialize()
    {
        dispatcher = Dispatcher.CurrentDispatcher;

        this.host = new ServiceHost(typeof(Printer), new[] { new Uri("net.pipe://localhost") });
        this.host.AddServiceEndpoint(typeof(IConnector), new NetNamedPipeBinding(), "GetPdfVersion");
        this.host.Open();
    }
通过这种方式,可以实现对autocad执行上下文的控制。下一步是通过dispatcher调用该方法

public void GetPdfVersion(string filePath, string exportFilePath)
    {
        dispatcher.Invoke(new Action<string, string>(this.GetPdfVer), filePath, exportFilePath);   
    }
public void GetPdfVersion(字符串文件路径、字符串导出文件路径)
{
Invoke(新操作(this.GetPdfVer)、filePath、exportFilePath);
}

因此,通过使用此方法,我可以从外部应用程序运行
GetPdfVer
方法中包含的代码,并获得使用WCF而不是COM交互的所有好处。

除了
docColl.Open
之外,您是否尝试过做其他事情?例如,关闭所有打开的文档。如果是,你犯了什么错误?是的。当我尝试时,也发生了同样的错误。我刚刚找到了解决办法。我很快就会写的