I want to write a full-text search server based on WDSQuery.
This server is .NET Remoting enabled. I wrote following code to do the task:
SearchDesktopClass wds = new SearchDesktopClass();
wds.ExecuteSearch (...); // InvalidCastException
When I tried to run this code by remote method call ,
An Exception 'System.InvalidCastException : QueryInterface failed for Microsoft.Windows.DesktopSearch.Query.ISearchDesktop' throwed.
It was ok if I run this code by local method call.
I tried to build WDSQuery.dll myself by TlbImp and make it to be a PIA.
but problem was still there. I don't know if this is a security issue or multi-thread issue.
Could anybody help me Thx..

WDSQuery: 'System.InvalidCastException' throwed when "ExecuteQuery" invoked remotely
KurtM
ApartmentState should be assigned before thread starting. So you cannot change current thread's AppartmentState to STA. Following is the key part of my solution:
public class WinodowsDesktopSearchAgent {
....
public DataSet SearchByKeyword (string keyword) {
....
if (Thread.CurrentThread.ApartmentState != ApartmentState.STA)
{
Thread thread = new Thread(new ThreadStart(this.STAThread_DoFullTextSearch) ) ;
thread.ApartmentState = ApartmentState.STA;
thread.Start();
thread.Join();
}
else
{
this.STAThread_DoFullTextSearch();
}
....
}
....
}
As you can see, I create a new thread if current thread is not in STA. But it causes another problem : how to pass search conditions and get the result from the new thread and how to catch the exception throwed from another thread. I solved these problems by wrapping all in a class named WinodowsDesktopSearchAgent and sharing data by WinodowsDesktopSearchAgent's members among two threads (current and the new one). Following shows how easy to use this class:
WindowsDesktopSearchAgent wdsAgent = new WindowsDesktopSearchAgent();
DataSet dataSet = null;
try
{
dataSet = wdsAgent.SearchByKeyword(sKeyword);
}
catch (Exception ex)
{
// .....
}
That' all. With WindowsDesktopSearchAgent I don't have to worry about whether current thread is in STA or not.
Micky G
Hi Matthew - I'm trying to create a search server based on WDS using Remoting to a Windows Service. I've met the same problem with the InvalidCastException. I tried setting the AppartmentState as you suggest but it made no difference. Can you give me some more details of what you did Thanks....
Optima Warehouse Solutions
Rachit Aggarwal
Remark:
Is it possibly a COM MTA/STA Issue
BTW.. I found the exception also throwed when I invoked ExecuteQuery from a new thread.
... no and then!!
I need some more help !
I'm adding a WDS service to an existing Windows Service accessed using Net Remoting. It runs under LocalSystem. The objective is to provide text searching of a common database of text files to workgroup users.
With help from Matthew Yeh I've got the code to interact with the WDS service but instead of returning the results it gives the error
UnauthorizedAccessException: Access is denied.
at Microsoft.Windows.DesktopSearch.Query.SearchDesktopClass.ExecuteQuery(String lpcwstrQuery, String lpcwstrColumn, String lpcwstrSort, String lpcwstrRestriction)
I'm not a COM expert and I assume that this is an issue related to what is allowed to access the WDS service. Is this an issue of Users my Windows Service runs under LocalSystem and the WDS stuff is under a local user.
Is it possible to install WDS under the LocalSystem account and would it solve the problem Any ideas very welcome....
Efergus
Hi Matthew
Thanks for your prompt reply and for the details of your solution. I will get work on this today ! Thanks again...
RWH_1
Answer myself,
I had solved it myself. This is MTA/STA a problem.
All I have to do is to change the value of Thread's property "AppartmentState" to "ApartmentState.STA".