0

I'm new to Web Api creation I'm trying to create a Web Api for an app which isn't designed very well (web api is in Asp.Net framework 4.5).

The current App is in .Net framework 4.5, c# / vb.net and is currently accessed through winforms UI or console It requires services to be started which are static (as in the classes) and depend on a connection string to a sql server (also static). There's more static/ shared code in the app.

These services take too long to start so once the user is logged in, The service should remain persistant for that login until he logs out. (each user gets his own service)

When the user logs in they are able to specify a database name & server they wish to connect to. And it'll show the data on that database (same schema, different data on the databases)

The app was designed to be just used on desktop, after an .exe click. It works fine where a single user hits the .exe and he enters a his log in credentials.

The web Api and App will be on same server/machine (unless it's a bad idea)

current structure of project

CompanyName.Product.Project1
CompanyName.Product.Project2
CompanyName.Product.Project3
......
...... (50 more projects)

CompanyName.Product.WebApi

How do i solve this issue of static code in the app which will effectively override when a new user logs in or am i missing a trick in Web Api config I can only think of launching the app for each login/ user (so multiple apps running for each login) and some how call the methods which i need to return data. Once the user logs out i kill the app for that user.

UndeadEmo
  • 433
  • 1
  • 6
  • 15
  • If you pass which user is logged in to the Web API service, as part of each request message, couldn't the web service then discover the appropriate connection string for that user? If it needs to cache the connections per user for performance, it could do that in a dictionary. – Steven Doggart Jun 27 '19 at 16:31
  • If that's not possible, you could have the client pass the connection string to the service, but that's pretty ugly... – Steven Doggart Jun 27 '19 at 16:32
  • main problem, is that the services have lots static variables (as well as taking 40 seconds to initialise each time), one of them is this connection string, Once the user comes along and assigns it he's able to connect to correct database. however if 2 users come along with a request at the same time, the first user could end up getting the data for user 2 or partially fetched data. Even if i was able to fix the connection string there's more static variables. The only safe way i can think of is to launch a new app for each user and get it's process id from process explorer and somehow call it – UndeadEmo Jun 28 '19 at 11:09
  • Yeah, if that’s the case, and rewriting it all to not be static isn’t a possibility, then that’s a bit problem. I suppose, if the likelihood of having multiple users using the service simultaneously is low, then you could devise some way, using `SyncLock` to block the execution of any of the controller methods until after the previous request is done. In other words, force all the web api requests to be processed serially. – Steven Doggart Jun 28 '19 at 11:27
  • I would suggest, though, that fixing them to not be static may be easier than you think. If you simply moved all the statics to another class, and added a method that returned an instance of the class containing all those public properties or fields, then you could simply do a find and replace on every place each variable is used. So, for instance, `MyStatic1 = true` would be replaced with `GetState(user).MyStatic = true`. – Steven Doggart Jun 28 '19 at 11:32
  • [This previous answer of mine](https://stackoverflow.com/a/14895419/1359668) is somewhat related and may be of interest to you. – Steven Doggart Jun 28 '19 at 12:23

0 Answers0