Consume web service from same web application - asp.net

I seem to have struck a complete blank and will most likely have a homer moment when someone points me in the right direction.
I have a WCF Web Service which is consumed happily across asp.net, windows phone, windows services, etc, etc. I'm adding a page to the same application in which the service resides, basically a "test" page that the user can go to and it'll do a quick call to the service to see if the server is up.
Now normally, you'd just throw in a Service Reference and bobs your uncle, but for the life of me, I just can't seem to click what I'm supposed to do when I'm accessing the service from within the same application, do I still add a service reference or is there a quicker way to do it?
Thanks!

Try to reference the web service by its name in the code and see if you'll be able to create new object from that web service and then consume in the code, instead of creating a reference.

Related

Starting point of Azure WebApp

I opened a new .Net Framework WebApp and I am using it with Azure.
At the very first point, when I run the App from Visual Studio, it opens the browser and shows the default page:
I was looking for a "main" class or method but could not find one. I am wondering where does it start from? how this thing works?
If added a new method for which I want to run together with the WebApp, where should I call it from ?
I am wondering where does it start from? how this thing works?
This is about how does asp.net webapp run.When the application starts up, it runs Global.asax’s Application_Start() method. In this method, you can add Route objects to the static RouteTable.Routes collection. These will be inspected later when each request is received. Each Route object defines a URL pattern to be matched and the controller to be used in this case.
For more detail about how the Asp.net webapp run, you could refer to this article and this one.
If added a new method for which I want to run together with the WebApp, where should I call it from ?
When you publish to azure and add a new method in a controller, you could call it like yourappname.azurewebsites.net/Controllername/methodname. It mainly depend on your Rounte, you could refer to this article.
Azure App Service Web Apps is a service for hosting web applications, REST APIs, and mobile back ends. You can develop in your favorite language, be it .NET, .NET Core, Java, Ruby, Node.js, PHP, or Python. Applications run and scale with ease on Windows-based environments.
With App Service, you pay for the Azure compute resources you use. The compute resources you use is determined by the App Service plan that you run your Web Apps on.

ASP.NET Web Service on localhost?

At the minute I am building an ASP.NET MVC application to learn the technology, and I want to incorporate web services as I have never used them before and I want to have experience with them.
I was wondering would it be possible to create a web service and run it on the Visual studio local host along with my MVC application an then consume it with the MVC application.
I am hoping that the web service will basically pull data from the the same db as the app and then allow the mvc app to consume the service. Would this be possible?
Sorry if this is a pointless question but it is for a college project. Any help greatly appreciated
Thanks
Yes, it is very possible with Visual Studio - in fact, it is easy. Best to follow some tutorials - one such tutorial is http://www.asp.net/mvc/tutorials/mvc-music-store.
For more specifics about the service from within VS, this link may be more helpful - msdn.microsoft.com/en-us/library/cc668184(v=vs.100).aspx
If you are talking a WCF service, this is very easy to do. You'll want to add the service to the same solution as your MVC project, right click on References in the MVC project, then click add service reference. On the dialog that comes up, click discover to the right and it will find the service in the solution allowing you to add it locally.
Once you have a service reference, you can right click and configure to see the path. Should be set to localhost in this case. Doing this will allow you to set breakpoints and debug the service through your MVC application.

Add reference to service within same project

I'm wondering if it is okay to add a reference to a web service within the same project (web application), i.e. I'm adding a reference in the same project where the service is defined.
The reason for doing so is because
Hosting the service becomes easier (gets automatically hosted when hosting the web app).
Invoking the service is done dynamically, i.e. the service url is fetched (from db) at run time and methods on that particular service are invoked. (this is a web app which is hosted on many domains. each app knows the service url of other apps (urls stored in db). since I have a reference to the service, I can change the url at runtime by updating the Url of the proxy and invoke it.)
Also, I'm not sure if this is the way to go about it. I have seen a lot of people suggest using WCF instead of a web service, but I don't see how I could accomplish the same thing with WCF.
There is already a question regarding this on SO - Add Service Reference to WCF Service within Same Project, but i don't think it is valid for my situation.
If the service is already in your project, you can use it directly without requiring a web service proxy.
After all, Web services are for exposing your application functionality to the outside world and not to other parts of your system.

Automatic initialization of IIS-hosted WCF service

I have an ASP.NET web-site and a WCF service which is called from ASP. The problem is, that during the first client request the site loads aufully slow, cause some time-consuming static objects are being created inside the WCF service. Is it possible to call any service method (by doing this the wcf object will be created), when the site gets loaded in IIS? (I know there is a solution for this problem in ASP 4 and IIS 7.5, but i'd like to know what's about IIS6-7). It is something like "user emulation") Maybe i can add some event handlers in global.asax? Any help is appreciated.
A very simple solution is to use wget within a scheduled task in Windows to simply issue a HTTP GET to your service. This will keep your application 'warm' and ticking over.

Is there a way to use a web service (WCF) WITHIN a web application?

Is there a business case for using WCF WITHIN your web application (ASP.NET/SQL Server)?
I'm not talking about a service to be consumed by others, but rather a service to be consumed WITHIN the web app. So far, the only use for web services WITHIN the web app I came up with is when I need to call the database from the client (through AJAX).
Please don't give me the copy/paste lesson on web services, all I'm looking for is a CONCRETE EXAMPLE of WCF benefiting the web application, and NOT the other apps consuming the services.
Quick clarification: by "web application" I mean a self-contained CRM of sorts. It does a lot, but it DOES NOT talk to any external apps/db's, and DOES NOT need to have service points open for other apps to consume.
We have a Windows service that gathers live data from the web. Because the data is ephemeral (it's only really valid for around 3 minutes), there's no point in recording it in a database. To retrieve data, the Windows service offers a (TCP transport) WCF interface which is only exposed to the web-server, from which our web-app requests data.
I think it very much depends on how you define "application".
If you're talking about a single web application then with the exception of provision ajax data as you've already noted I'm struggling to think of a generic reason why you'd want to as a web service (even more so for WCF) is just an exposed API and if you're in the application surely it would be better (in all kinds of senses) to go straight to the API without the additional overhead of wrapping and unwrapping the calls and the data - which is pretty much redundant.
On the other hand if by "application" you mean an assembly of "stuff" that delivers a solution to a set (or even various sets) of end users then yes, of course... there is definitely a case where the flexbility of having components communicate via services outweights the overhead issue. Where it gets grey is if there is an assumption that a single application will (or at least might) later become multiple applications wired together - but if you're not doing it now then you're just causing yourself pain you don't need so shouldn't bother. If you need to split it deal with that as a self contained exercise as and when you need to split it.
One concrete example from my practice: On one web site we use actively tables which are implemented with respect of jqGrid plugin to jQuery. The contain of grids will be loaded per ajax. So the most pages of the web site are very simple and have clear XHTML code. The business logic with the interface to the database is inside of WCF service. Inside of WCF service we get data also from other information stores of the organization. In the same way if one will need later to have an input from our project the WCF service will be perfect.
So the usage of WCF WITHIN in our web application helps to archive clear interface, clear separation business logic from the HTML markup and better testability (with utit tests integrated in Visual Studio).

Resources