AutoCAD can't work with document object from external thread. That's the root of the problem. If I write method and put an CommandMethodAttribute - it will be work, but only from AutoCAD console... But, what if I need to do this operations from external application? First of all, need to specify the attribute on service behavior class
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
Thus be used only one thread for all operations.
The next step, in Initialize()
method get the CurrentDispatcher object and put it in static variable.
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();
}
by this way, control of autocad execution context can be achieved. The next step is invoke the method through dispatcher
public void GetPdfVersion(string filePath, string exportFilePath)
{
dispatcher.Invoke(new Action<string, string>(this.GetPdfVer), filePath, exportFilePath);
}
So, by using this method, I can run code, contained in GetPdfVer
method from external application and get all benefits of using WCF instead of COM-interaction.