StrataFrame Forum

Accessing BOs from a Console App.

http://forum.strataframe.net/Topic30305.aspx

By Charles Blake - 9/6/2011

We have developed a StrataFrame application and have created and tested some code that does file moves and table updates.  At this point it makes more sense to move some of these methods to a console application that can be called from a cron type service to be run at specific intervals.  Since these classes don’t use forms or invoke message boxes it would appear that we could easily port this code to a console application.


My question is can I access and use my StrataFrame BOs from the console application; most of the startup code that I see is for Win Forms?


By Edhy Rijo - 9/6/2011

Hi Charles,
Charles Blake (9/6/2011)
My question is can I access and use my StrataFrame BOs from the console application; most of the startup code that I see is for Win Forms?

Sure you can, or if this is part of some automated process you can build a Windows Service and a Front End GUI form to configure the settings like connection string, etc.
The SF Business dll will give you all the access you need to work with your data from any .Net project application.
By Charles Blake - 9/6/2011

Well I guess the issue I am running into is initialization.  As soon as I access a BO I get the following error:


“An error occurred while creating a new DataLayer for the business object.”


I have included a reference to my BO and do see a copy in the debug folder.  I also supplied a data source prior to the access as follows:


DataLayer.DataSources.Add(new SqlDataSourceItem("", "Server=localhost;Integrated Security=SSPI;Initial Catalog=MyDatabase;"));


Is there other initialization code required to access the BO?
By Trent L. Taylor - 9/6/2011

This can definitely be done.  So most likely you are not defining your data sources (connections) before the first time that the BO attempts to access the DAL.  Before you ever create an instance of the BO, be sure that you data source is created.  In a standard WinForms app, this is done in the SetDataSources method of the Appmain.vb or program.cs files.  So you will need to do this manually yourself.  I generally do this first thing at the top of the Main when creating console apps.  We use a lot of console apps and services for maintenance or "tasked" operations.

Also, make sure that your DataSourceKey that is defined in your BO has a matching Datasource that has been created.

MicroFour.StrataFrame.Data.DataBasics.DataSources.Add(new SqlDataSourceItem("", "Connection String Here"));
By Charles Blake - 9/6/2011

If the following is all I need than I will re-validate the datasource, although I have used that datasource to access the table directly.

DataBasics.DataSources.Add(new SqlDataSourceItem("", "Server=localhost;Integrated Security=SSPI;Initial Catalog=MyDatabase;"));

           
APP_BO.tbl_test boTest;
boTest = new APP_BO.tbl_test();
decimal initialSize = boTest.Get_Size();
By Trent L. Taylor - 9/6/2011

It is if you haven't set the DataSourceKey property to another value on the BO itself.  The DataSourceKey on the BO allows you to have as many different connections as you need and then tell the BO which connection to use through the DataSourceKey.

Also, if this doesn't work, I would like to see the full stack-trace so we can see at what point the error is occurring.  Finally, are you just running this at the moment through a console app or as an actual service?
By Charles Blake - 9/6/2011

Thanks, I was able to get connected by using the ConnectionManager and supplying it with the same keys and data we use in the application.