On my current project, I'm sending dll files to clients using a WCF service.
The dll files contain UserControls which the clients use to visualize data.
However, sometimes new UserControls are introduced by the service, without the clients knowing about this. When this happend, I send the new dll file to the clients so they have the latest version of the UserControls.
However, when I want to write this new dll file on the clients HD (using a FileStream), I get an exception saying that the file is used by another process (of course, the clients always have an assembly version of the UserControls on their HD, so this has to be overwriten).
Is there any way to overwrite this file without getting an error? I'm using MEF to read the assembly files in the client project. To send the assembly file to the clients, I just send the filename as a string and the file's content in a byte array.
This is usually done with Shadow Copying, but I can't find anything definitive as to whether it works with MEF.
If its acceptable for your new control to be loaded on closing and re-opening the application, then you can copy all your plugins prior to composing with MEF.
Here is a blog post with somebody essentially trying MEF + Shodow Copy, and a particularly relevant comment.
The fundamental problem here is that
.NET doesn't support dynamic assembly
unloading. You could reload the
assembly, but you would never be able
to recover the memory used by the old
one
Related
DllRegisterServer is called, when Windows or OLE wants me to register my classes under HKEY_CLASSES_ROOT\CLSID. But I don't understand why this function has to be implemented, because when Windows/OLE can make calls to my DLL, then my classes are already registered with their CLSIDs and their path to the correct DLL. Can somebody tell me, what I am misunderstanding?
You are confounding the chicken and the egg. In order for COM to help a client app to create objects and marshal calls, it needs to know where your COM server is located. The client app just uses a number, a GUID, to tell COM what object it needs. The mapping from a GUID to code in an executable file requires COM to know where that file is located first. And, if necessary, how to marshal a call on an interface from one apartment to another.
It is registering the server that provides COM with that knowledge. It writes keys in the registry that COM uses to find the file back. Like the CLSID key, its InProcServer32 sub-key provides the path to the file. Etcetera. Or the manifest embedded in the client app if it chooses to use reg-free COM.
Observing this with SysInternals' Process Monitor can provide a lot of insight. You'll see what DllRegisterServer() does and how a client app uses those keys.
For the peoples wondering about Title of Question
Example of COM object is Dynamic Context Menu ( which would be a DLL)
to register it-
First Make a Random GUID under HKEY_CLASSES_ROOT
Under That GUID make key named "InProcServer32"
set value of this key as full path of your DLL
This is called Manual Registration
people's with no knowledge of registry editing should also be able to register your dll too.
for that, you should write codes to make these GUIDS and InprocServer32 keys etc programatically Under the DLLRegisterServer() Function in your DLL
so that , people with no knoledge of registry editing will be able to type -
regsvr32.exe "full path of your DLL"
in command prompt for registring your DLL
when they type that command, regsvr32.exe will do nothing but call that DLLRegisterServer() in your DLL
and by calling this function, codes of writing registry keys such as making random GUID and InprocServer32 keys will be Executed ( which you have written )
so registry entries will be made and that is called Registration.
so , according to me regsvr32.exe was just created for the people who have less knowledge of how to edit registry.
Wonder if anyone can help me as i'm getting the following error 'Could not find endpoint element'.
My setup is similar to the following - A main project vb.net (Starts with say a login.exe that in turn uses class libraries to then do things such as set the menu navigation system which proceeds to call several winform class libraries) One of my winform class library calls another class library which contains some logic that will then in turn call a class library that has the service reference to a WCF service and will handle the WCF service reference function calls.
So if i run my winform as a standalone exe rather than a class library contained in the above setup everything works fine with connecting to the web service contained within the class libraries because i have added the <system.serviceModel> reference information to my winforms app.config as per the below thread suggested.
"Could not find endpoint element with name..."
My issue is that once i turn my winform back to a class library and include in my main project to be called it never finds the the <system.serviceModel> reference contained within my winform .dll as i'm lead to believe by again by the above linked post it will use the main project app.config not my winforms configuration.
What im trying to get at is I don't really want to add the <system.serviceModel> information contained in the app.config of my winform dll to the starting login.exe(being the program that starts the chain) as that just seems messy and just strikes me i must be doing somthing wrong in the first place. Is there a way to use the setting from my winform class rather than going all the back back through to the main project??.
I hope that makes some sort of sense any help would be greatly appreciated as really stuck as a wcf newbie, thanks in advance
Personally I don't think it's messy, that is generally how these things are done there is an application configuration file for your main you only have one copy of your main and usually your application is installed only once meanwhile you could have lots of copies of your DLL used by many apps... The configuration file will usually always contain settings that pertain to that one application meanwhile a DLL could be used by multiple applications. In short you should use the main app.config.
In any case to answer your question you could read in the app.config of the DLL in like any normal file parse it and programmatically setup the end-points as described here:
How to: Create a Service Endpoint in Code
When I register my .NET Assembly with regasm.exe the registry key
HKEY_CLASSES_ROOT\CLSID{111E32AD-4BF8-495F-AB4D-6C61BD463EA4}\InprocServer32
is set to "mscoree.dll".
However, I am trying to mimic an existing COM-Server that was written in C. When registering this old COM-server the InprocServer32 is set to the full path to this component.
Unfortunately the existing system (a plugin host that I can not change) reads and use this value - an is confused by the "mscoree.dll" value.
My solution might be to patch this registry entry manually - but I would like to understand why regasm writes "mscoree.dll" into InprocServer32 .
The explanation is quite easy. When you use a native (unmanaged) COM server in-proc, it is loaded into the consumer process and the consumer process directly calls its functions.
This can't work that easily with a managed code COM-exposed assembly. In case of managed code an intermediate layer is needed that performs the managed/unmanaged interaction. mscoree.dll acts as this intermediate layer. So when the consumer calls CoCreateInstance() mscoree.dll is loaded and emulates the COM server by loading the COM-exposed assembly managed code and forwardind all calls to the latter.
I'm just starting to play with MEF and have a couple questions.
1) I wrote a WCF service that takes in some xml and passes the xml off to a parser. The parsers are composed using MEF. (metadata in the xml lets me determine which parser to use). I can add a new parser, and support new XML, by just dropping the dll in a directory. That part all works. But, WCF services can be instantiated multiple times, I want my parser catalog to be static, that is, if multiple instances of my service are spun up, and they get the same XML, I only need one instance of the parser running, they are written to be thread safe. I can't seem to configure MEF to do this. Anyone know how?
2) I can drop in a new parser into the directory and a catalog refresh will automatically discover it, that works great. But if I try to drop a modified dll into the directory, and that parser has been activated in the service, I get an error saying the file is in use. Is there a way to override this?
1) It sounds like you should make your MEF container and catalogs static so they only get created once. Make sure you specify that the CompositionContainer should be thread safe by using the constructor with the isThreadSafe parameter and setting it to true.
2) You can enable shadow copying which will prevent the file from being locked when the DLL is loaded. However, you can't unload DLLs from an AppDomain in .NET, and furthermore it is not safe to recompose a CompositionContainer that can be used on multiple threads. In other words, using the isThreadSafe parameter only makes the container thread-safe for "reading"/pulling exports from the container, not modifying it via composition/recomposition.
So if you want to add a new parser it's probably best to restart the service.
We are adapting our client side relatively complicated application (ActiveX / .net / Delphi / C++ / COM ) to use SxS to achieve non admin deployment and isolation from older versions of our product.
We were able to achieve this goal for almost all our in proc components such as our .net ui, Delphi ui, and the COM servers we use in proc by composing a manifest file which described all the libraries used by our process, with no registration on the client of any of the components (almost).
And here comes the almost part:
At the moment, our application invokes (from it's c++ portion) an out of proc ActiveX server (Delphi ActiveX EXE), which in turn itself invokes another set of out of proc ActiveX servers (third party plugins, any thing goes here, Delphi, C++, any thing as long as it's out of proc ActiveX EXE and implements our interfaces).
As we know SxS does not support out of proc ActiveX servers. And we can't use these objects as in proc com servers in our main process because that would require a major rewrite of our application and even worst, a break of our public facing API which is used by third party tools and vendors, an api break which we can't allow.
We have stumbled on this article which describes how IHTMLDocument2 can be extracted from an Internet Explorer window running in a separate process. Which made us think of this approach:
We would create a secondary satellite application / process which will run the ActiveX as in process server.
Then we will use LresultFromObject and ObjectFromLresult to transfer a reference of the ActiveX object from the satellite application to the main application process. The satellite application will have it's own manifest file which will allow it to run in SxS mode.
Same approach will be taken to communicate between this Delphi ActiveX EXE and the third party AciveX EXE Plugins
There is an alternative solution, which for the moment we do not prefer over the proposed solution above which is to use .net remoting and .net com proxy classes to open the communication channel between the two processes, by translating the com request to .net remoting, and back to com on the second process.
So here comes the question:
What do you think about this approach ?
Do you see a better solution to the problem ?
It is possible to do. What is needed:
An application needs to start a server itself rather than relying on COM to do it. You don't need the extra indirection provided by the registry, just use CreateProcess().
A server should register its class factories in its main() method with CoRegisterClassObject().
Important: the CLSID it uses for each factory should be altered to be unique for each service instance. This ensures that the client connects to the correct server. I simply XOR the process ID with a class factory CLSID. The client knows the process ID as well so can make the same alteration.
The application should call CoCreateInstance() in a loop with a Sleep() call to wait for the object factory to appear. Don't declare failure until at least 60 seconds have passed (that bit me).
Both the application and the server need a manifest that contains a <file> element for each proxy/stub DLL and <comInterfaceExternProxyStub> elements for each interface that is remoted.
Alex,
nobugz is right, you can access the Running Object Table to create an instance of a COM Object from a currently running process of your Delphi automation exe.
However I have found a big issue that I cant explain. I can only access the object via the variant dispatch method when working this way.
Basically if my Active X exe is not registered, I get an "Interface Not Supported" error if I try to instance the object through interfaces for example:
WebUpdate : IAutomation;
WebUpdate := CoAutomation.Create; <-- Wont Work Error
WebUpdate : Variant;
WebUpdate := CreateOleObject('WebUpdate.Automation'); <-- Works Fine
If I register the active x exe using regserver the problem goes away!!
Go Figure!