ASP.NET calling methods from another project - c#

I am developing a system comsisting of two ASP.NET projects. The first project is only accessible from a local IP and holds all the methods and has access to the database. The second project only consists of a userinterface and should get the needed data, by calling methods in the first project, which returns the needed data. The second project, is available from any IP.
My question is, how can I connect these two projects, so that the second project can access methods in an object, in the first project, using ASP.NET?
I have tried with JQuery and AJAX, but with those, I can only access static methods.

You can use a "Generic Handler" (a .ashx file).
With this you can expose a HTTP endpoints.

You should convert your first project to a Web API Project which has endpoints exposed to clients to do different things over HTTP protocol. You will call these end points from your second project.
Your first project will act like a web service which does all the business logic and database things. Your second project will be thin UI client, which will communicate to the first as needed for getting data,updating data etc.
You can use HttpWebClient class in your first project to communicate to the end points of your Web API project.

Related

Add a WCF service to ASP.NET MVC app and get advantage of existing classes

I'll try to explain the situation in the best way possible, I hope someone out there can guide me a bit.
We have a working .NET MVC 4 app where we've added a service reference to consume a WCF external service.
We've created several classes and methods to translate our data model to service's one, so we can call it's different methods from within our app using our data model.
Now we have a new requisite: to add our own service, which can be accessed from outside our environment, and who has to replicate some of the methods of the original service.
The idea is that users will post info to our environment through our service, we'll apply several treatments to the info, and then post it to the original service.
So my question here is: How do I create and attach the new service to my MVC app so I can get advantage of the classes and methods that are already developed and the data model of the original service?

Calling Web API vs adding reference to underlying dlls

This is more of an Architecture question, and I want to know all the possible pros and cons of the approach.
In my org, we have an ASP.NET Application, a Web API Project, and underlying DLLs which calls App Tier which is physically on different server. In ASP.NET Application, for a particular piece, we are having a SPA.
For most of the things (I would say 99 % of the things) , we are making ajax call from our SPA to Web-API to access underlying functionality.
SPA and WebAPI are both deployed and hosted on same Web Server as different applications and WebAPI has reference to underlying DLLs so these DLLs are deployed with WebAPI.
For one of the functionality some server side processing needs to be done on ASPX page's code behind.
I am suggesting to my team to do keep calling from SPA to WebAPI using http client and maintain loose coupling that we have between Application and the dlls via WebAPI, but many (I would say everybody else in my team) is in favor of adding direct reference of DLLs to ASP.NET application, so now these DLLs will be deployed with ASP.NET Application alongside.
Is my suggestion not so good provided ease of implementation that we would be getting with adding direct reference for DLLs in ASP.NET Application? Let me know if I am not explanatory enough.
I would be in favor of using a DLL directly if you could get rid of the Web API altogether. Since it does not sound like you are planning to do that, I think your suggestion to continue using the Web API from your application:
Using the DLL from two places creates a deployment liability: every time you update it, both places must be updated
Changing the code in DLL requires testing the change from two paths - A-D and A-W-D
Fixing a bug in the way that you call your DLL would likely have to go to both A and W, instead of going to W alone.
Of course, the path A-W helps you exercise your Web API component, too, helping you detect bugs early.

Add Web Api project to existing project

I'm developing a project that based on columns from a database tables as to present a dashboard.
I've been told to create a Web Api project (added to a windows form project) to obtain the columns data and work it to present the charts.
My windows form project is project1 and the web api is project2.
The idea is when I click a form button, I'll present a html file with the charts. My line of thought is when I press the button project2 will start, with the data already collected in project1, to present a dashboard.
If I'm thinking correctly, my questions are:
How can I call the web api when clicking a button
How can I pass the information already collected to project2
May be you not explaining something right or I don't understand something.
Looks like you adding a Web Api project as part of the solution. Looks like you have some data at Windows application, which then being posted via Web Api to database. If this is correct then:
Web Api is .NET web-centric framework for REST, in which HTTP stack is at the center. In REST you have stateless application running on the web server and you making calls to it. The beauty of REST is that the client can be practically anything - any device, any application, another REST service - anything!
The answer:
So, your Windows App can make a call to your REST layer and post data. In your Windows app you will need to reference System.Net.Http to use HttpClient class. Using this namespace you can compile Http messages and make Http Requests and receive Http Responses.
I think, this is exactly what happening in your solution. Please let me know it it is not what I think.

Reference C# Windows Service into an ASP.NET web application, is it possible?

I have developed a C# Windows Service application with certain methods that I would like to call from an ASP.NET web application (it is important that there are these methods returning the result). My initial guess was that I could import the dll of the Windows Service into the web application, but I can find only the executable of the windows service, there's no dll at all.
Is it possible to compile the Windows Service into a dll? And if not, is there some other way I can reference my Web Service into the Web application?
Extract the common functionality into a class library project and reference it from both projects.
Is your windows service hosting a web service or does it just contain code that needs to be accessible in you windows service and web site?
If the former, then you can connect to it via the normal mechanims for connecting to a web service (e.g. adding a service reference in Visual Studio). If the later, then create a class library that both your windows service project and web project can reference (via Add Reference in Visual Studio).
You have two options. If you just need the code to be invoked, then you can call the windows service functionality via a custom action on the service, invoked from your code.
However, I gather that what you require is more complex, requiring a response as well. In this case, as per sga101, encapsulate the appropriate functionality into a separate class, and use this in both places. This means that the code projects should both be included in the same solution ( it is not strictly necessary, but it makes it easiest to keep the code matching ).

Get the reference of a DLL in remote application?

I have one ServiceStack application with one class named Performance in this application.
Now, I have another application which is used by my end user. This is a console application, downloaded by end user and run as a background task.
I want to POST the performance data from that application to my REST web service and store it in a database.
For that I need to run this in my client application, which posts the performance data to my ServieStack application in JSON format:
JsonServiceClient client = new JsonServiceClient("http://localhost/RestIntro");
var res = client.Post<Performance>("/Performance", c);
I don't have Performance class in my client application. I don't want the client to have the class so that I can update all clients if I need to make a change.
I want to know, how can I get the Performance class or its DLL in to my client application dynamically? So when the client runs it will have the class in order to make the post to the web service.
I recommend that you keep the DTOs of your ServiceStack web services separate in a dependency-free project as this is what represents the contract or API of your services. If it's in its own assembly than you can easily share it with any of your clients or unit tests, etc.
If you don't like the idea of copying a dll around (even though its less work/friction), you can generate your own DTOs from the xsds on the /metadata page, see this question:
How can i convert XSD file to C# Class
What you are talking about is actually available by using a SOAP service. Or, you need to create a "performance" class on the client and deserialize the JSON result to that class.

Categories

Resources