I'm working with this small web application (1 page) built by someone else that does a specific task after pressing a button on the web page.
Now the requirements have changed slightly, and we need to automate this to run weekly without the need of user interaction.
What would be the best way of doing this, minimizing the changes done to the code?
I was thinking on adding a console app to the project that then references internally the web app but that doesnt seem to work.
Or maybe converting the web app to a to console app, if that is actualy possible?
Is there any straightforward way of doing this?
Thanks
First, make sure the "specific task" is broken out from the Web application so it resides in its own .NET project. Even if this project just contains one class you're "separating concerns" between the Web-based UI and the task itself.
Then you can create another "wrapper project" to call this new project as you wish. A console application might well do the job -- you can run that using a Scheduled Task -- or you may prefer to use a Windows service.
It really depends on how well the existing code is structured. A common approach is to divide business logic from the presentation layer. In VS, it's normally done by creating a class library project and keeping all the business logic in there. A web application project would then just instantiate business logic classes and run their methods.
If it is done like that, you just need to reference the class library project. If, on the other hand, you have all the logic in the web application project, probably there's no fast way of doing that, as you're not supposed to instantiate Page classes manually (well, you can do that as well, but that is clumsy and not recommended).
So in that case, you should create a class library project and move there all the logic you need to use in your console app. I would imagine that would require quite a bit of refactoring.
Related
I have a three tier asp.net web app backed by SQL server express, with business logic in C#, and a web UI. I have a small collection of actions that exist as methods on objects in my business logic layer that need to run on a configurable, periodic basis. These actions rely on many other objects in my current app along with needing my data access layer to talk to SQL. Currently I manually log in to the admin site and kick off the actions via my UI as there are only two at the moment but that will grow.
A couple options I've considered but wanted thoughts on before I proceed...
I could create some scheduled tasks in Windows server to kick these actions off periodically but I want to know how would I expose these actions in the best way. I thought of creating a web service exposing them and building a tiny exe to call that web service but I would have to make sure that web service was locked down with security. Another option which I know a little less about would be exposing those actions via export and then building an app that could use them by referencing the DLL. It seems that app would get kind of large if it has to pull everything in to use unless I could componentize my app binaries more so it would only need a small binary or two.
Any thoughts on how I should approach this or pointers on content discussing this type of issue?
I had gone the way of tiny EXE that calls a WebService from main app and it seems to work well, but that was before I discovered Quartz.net.
Now I'd suggest use Quartz.net as a scheduler. From the site:
Jobs can be any .NET class that implements the simple IJob interface,
leaving infinite possibilities for the work Jobs can perform.
I am new in C# console application.
I have a question: I want to create a MVC project using Entity Framework, I have made some logic to import data one table to another.
Now as per client requirement I need to create a console application for same logic.
Now my question is : how can I use my mvc application logic in my console application?
So if I need to do any changes in my code, I have to change only in web applicaion not in console app.
Please suggest a good way to make efficient my logic
Thanks
You'll end up with 3 projects;
The Web Application
The Console Application
A Shared Library
What you would do is,
Refactor the shared logic so it exists in separate methods and doesn't depend on any shared state specific to the web application.
Test the web application still works.
Take out the shared logic and put that in the shared library.
Add a reference to the shared library from your web application.
Change the web application to call the functions in your shared library from the web application.
Test to ensure it still works.
Create your console application.
Add a reference to your shared library from the console application.
Call your functions in the shared library as needed from the console application.
Test to ensure it works
Yay, you're done! \o/
To elaborate a bit on George's answer, you need to implement SOC (separation of concerns) in your app. MVC is a framework that helps you do that, there is also MVP which could be even a better solution in your case.
How can I use the debug a service within Visual Studio 2010? What I would like to do is step thru the code as the request is being processed.
I am running the service by right clicking on the web site project (C:...\APIServiceSite) within the solution and selecting "View in Browser". Everything runs OK and I get the expected results back from the service but I cannot find a way to step thru the code which is in a another project (APIService) within the same solution.
Off the top of my head: Right click the service project and choose Debug >> Start New Instance. Then start the debug on your web "test" application. You should now be able to accomplish what you desire. (since this is off the top of my head, if it fails, it is very close to the right answer).
A better option is to move the functionality into a class library and make the web service project very thin (essentially returning calls from the class library assembly). You can then test the business functionality by using a unit test library, like MSTest (built in with most team system SKUs of Visual Studio) or nUnit (etc). Sure the tests will likely be integration tests, but you accomplish two good things:
You get the UI out of the testing equation so you can focus on the business logic
You create repeatable tests (think science, not art)
I find it a really bad pattern to have moving parts in a UI project, unless the moving parts are focused on presentation of data. And, yes, I see web services as a UI, even though the user is an application rather than a human body.
I debug a service by making a standard wpf application that uses it. By adding Existing item / Add as link all source file from the service into a new project (wpf application), into a separate folder of this application. So i can test it, and debug the same way i test/debug an application.
Note that you might use the microsoft log service to log issues that your service may have when running as a service. For handling the errors, i made one MustOverload (in vb) function that handle errors, and i overload it as a MessageBox when using the service in my test app, and overload it as ms log when it runs as service
(quick info on log here :
http://msdn.microsoft.com/en-us/library/aa984385(v=VS.71).aspx
)
In my Visual Studio solution I m having following types of project:
Class Library - BusinessLogicLayer
(I m in doubt how to seperate functionality in BLL)
Class Library - DataAccessLayer
(I m in doubt how to seperate functioanlity in DAL)
Class Library - DataModels
(Contains various models like User,TimeTable,Address, etc.)
WCF Service App - To create common WCF service which can be consumed from jQuery(Web App) and WPF App
ASP.net WebForms Project - Web Pages
WPF Project - Windows application for same (As it is the requirement)
Setup project - Septup project to create installer for Windows app
UnitTest project - Project to make NUnit basd test cases
Can u please tell me whether or not I m going right way?
This is my first n-tier based application.
I m actually not clear to seperate functionality in layers even in my very first screen that is login screen.
It could be like this way from code behind file login.aspx.cs in OnClick_submit event I should create instance of UserBLL class and then I should call obj.validate(username,password) which returns a model of UserInfo. While that BLL class should itself call UserDB.Validate(username,password) method which returns model back to PersonBLL class.
If I use this scenario then every operation needs a seperate db conenction.
I also want to asks whether or not creating applications in this layered approach results in any extra memory consumption.
Please explain the scenario to me if you are familiar with this.
I don't know about others but I find working code to be a far better way of getting a handle on best practices. Therefore , I'd strongly recommend downloading the Patterns and Practices Data Access drop on Codeplex. It's a little old now but will provide you with a comprehensive reference sample for a Web (albeit MVC), WPF and tiered Services application.
I am currently programming a ASP.NET MVC3 application, using Entity Framework 4.
There are certain tasks (processing 10K+ of records, for instance) that has to run in the background. I am writing a console application to do the job (and maybe run it as a thread or window service).
Both the console app and MVC3 EF4 project
Since I have written lots of services and model code inside the MVC3 application, I would like to reuse that in my console project.
Is that possible?
(Due to time constraint, I cannot refactor our the services into another DLL/code library)
Moving the services and model code into another DLL shouldn't take more than about 10 minutes, if that. You don't even need to change the namespaces if you don't want to - just create the new project, move the files over, add a reference to the class library from both the console project and the MVC project, and you should be done.
It depends on how you have built the MVC system and which parts you are going to use in your console application.
Eg If you are going to reuse Controller logic in your console app you need to provide fake implementations of numerous classes. Eg HttpContextBase.
Further more if you have use Session, Cache, HttpContext you will have hard time using the code base for the console app.