Initial Event in Windows Console - vb.net

I'm creating a Windows Console application written in VB.NET and I have a few processes that need to be called only once during the lifetime of the application. If it was an ASP.NET application, I put these in the Appliction_Start method of the Global.asax.vb file. Since there isn't a Global.asax.vb for Console applications, is there an event I could handle that allows me to call my functions before Main is called?

Is there a problem with just calling them first in main?

Main is the first method where you can grab the needed information / inizialize global stuff.
Why would you need an earlier point? The only thing that is different to Application_Start is that no other method is called automatically (unlike in a web application where the site is opened and the code executed).

Related

Checking for previous instance of application

I am trying to write a code to check previous instance of the application in vb.net, my requirement is that application should prevent for same user and it should allow for different user who wants to access through remote parellel 2x client..Any one please help me on this...Thanks
If you are using VB.Net, You should definetly look into WindowsFormsApplicationBase class, shipped as part of .NET framework.
There is a property (IsSingleInstance) specifically designed to provide single instance behavior to the application.
You can even receive notifications through StartupNextInstance or the counterpart OnStartupNextInstance method when another instance of the application tries to run.
I forgot to mention that My.Application already is an object of the WindowsFormsApplicationBase type (at least in VB.NET WinForms applications).
UPDATE:
Currently, to take advantage of this stuff from a VB.NET project you have to follow these steps:
Edit project properties.
Enable "Make single instance application".
Click "View Application Events".
(optionally) Implement StartupNextInstance event handler.

Could not find endpoint element as referring to main project config settings

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

Silverlight RIA Services: Running DomainContext on background thread

I am working on a OOB application that does document merging with MS Word.
I need to download the latest MS Word template files, and since this can take some time, I am trying to do so in the background.
Merely instantiating a domaincontext on any thread other than the UI thread throws a cross thread exception.
The easy fix would be to instantiate the domaincontext on the UI thread, but that would defeat the object. Any workarounds? Anything I'm missing?
Regards,
Derick
This is because the default constructor uses a relative url for the service. It needs to get the current application to find the base url. If you use one of the overloads that accepts urls and give it an absolute url it will work. (Hopefully :) ).
Pass the domaincontext as a paremater of RunWorkerAsync(context) and you should be ok. This then allows you to perform all query operations within the background thread.

Where best to instantiate and close a Silverlight-enabled WCF Service from the Silverlight app?

When using a Silverlight-enabled WCF service, where is the best place to instantiate the service and to call the CloseAsync() method?
Should you say, instantiate an instance each time you need to make a call to the service, or is it better to just instantiate an instance as a variable of the UserControl that will be making the calls?
Then, where is it better to call the CloseAsync method? Should you call it in each of the "someServiceCall_completed" event methods? Or, if created as a variable of the UserControl class, is there a single place to call it? Like a Dispose method, or something equivalent for the UserControl class.
Thanks,
Jeff
You're better off just having an instance variable for the service. Creating and destroying the service for each call creates a lot of unnecessary overhead. Just create the variable and call the methods, no need to open it since this will be done automatically as of beta 2 (see section #5).
As for close, whether you try to close it for clean up probably depends on how your app is structured. If when the UserControl is closed the whole app is shutting down (the user closed the browser) then you probably don't need to explicitly close it since everything will get cleaned up when the Silverlight host closes. However, if you're creating lots of these user control and closing them while keeping the app open then you might want to create some kind of close method on your control that would clean up by calling CloseAsync.
If all the user controls use the same service, then you could just create a single service wrapper class that is used by all the controls that would handle calling the service. This would keep you from having to close the services when the controls unload as well.
In the case of 2 parallel event handlers in your SL client, you can do the following approach to make sure only one gets invoked:
Assume, we have a global client variable, App.Client, which is being used by everything in the app.
Now, control 1 needs to react on MyOperationCompleted, as does control 2.
Each control uses the eventhandler like this:
...
{
App.Client.MyOperationCompleted += Client_MyOperationCompleted;
App.Client.MyOperationAsync(...);
}
void Client_MyOperationCompleted(object sender, MyOperationCompletedEventArgs e)
{
App.Client.MyOperationCompleted -= Client_MyOperationCompleted;
}
So if you subscribe to the event in one case, as soon as it returns, you remove the subscription to this event. If if you always stick to this, it's quite unlikely (however not impossible) that other controls react to the event. Note that this approach is not 100% concurrency safe. I'm still trying to come up with a really safe method for doing this. But it sure helps.

How to ensure that the same thread is used to execute code in IIS?

We have a third party dll that is used in our web service hosted in IIS6. The problem is that once this dll is loaded into memory, the exception AccessViolationException gets thrown if a thread different then the one that created it tries to execute any code within the dll. The worker process is multi threaded and each call to the web service will get a random thread from the pool. We tried to unload it from memory and reload it each time we needed it, but I guess only the front end is .Net and the rest is unmanaged so it never actually gets completely unloaded from memory. We are using VB and .Net 2.0. Any suggestions?
(Response to Rob Walker)
We thought about creating a new thread and using it to call the dll, but how do we make the thread sit and wait for calls? How do you delegate the call to the thread without having the Dispatcher class supplied by .Net 3.0? Creating a hidden form and putting it in a message loop might work. And then we could call the Invoke() method of the form. But I can see many problems occurring if we create a form inside an IIS hosted web service.
I have read about a class in .net 3.0 called Dispatcher that allows you to put a thread in a loop and then call the method Invoke() using a delegate to execute a method using the thread. But this solution will not work if you cannot update to .Net 3.0. Another solution would be to host the third party dll in another application on the server and use some form of Remoting to access it. But you may still have a problem with the Remoting because it behaves similar to IIS and will also pick a random thread to execute the code . To get around this, you could put a wrapper around the dll and use it to delegate the calls to the UI thread by using the Invoke() method of the form.
I think you need to look at using a wrapper thread that handles all calls to the DLL, and deals with the serialization.
This thread is outside of the managed thread pool, so you control its lifetime. But even this would not be foolproof unless you can prevent IIS from restarting the app domain your web service is in.
You also need to worry about what happens when two web service requests come in at the same time. Is each call into the DLL standalone, or do you have to group together all the calls associated with a single web service request before allowing any other request to be serviced?
You could create a service that hosts the extra DLL. Via remoting you access the service, this will dispatch the calls the the thread that manages the DLL.
This way you have control over the thread that calls the DLL, and over the lifetime of the thread.
I'm a bit rusty, but you might try wrapping calls to the DLL in a single threaded apartment COM object. This would ensure that all calls go through the COM object's windows messaging thread. I think you would have to register the component in a server application within Component Services to do this.
Can you run the dll inside different threads as different instances? Like thread1 creates an instance of this third party dll, and thread2 also does, but as long as thread1 doesn't try to use thread2's instance it won't throw that exception? If thats the case, .Net never unloads any code once its loaded, if you load an assembly and then remove it, it still sits in that application pool. If you can create more than one instance at a time, you could load it up in a separate app pool you control per a request, then unload the app pool. Performance might drop though.