How feature complete is asp.net web api self-hosting? - c#

I have a requirement that I need to be able to have a stand-alone version of application, as well as an online version. One possible way of doing this would be to have a WPF version, which would satisfy stand-alone, and an MVC Web version.
Obviously, that would require two code bases (though admittedly they should be identical except the front end code). Is Web API self-hosting stable enough that I could just host a full-blown web app inside of it if I needed to?

Web API can run selfhosted but for running ASP.NET (MVC) you require a server like IIS (Express).
What you can do is have IIS Express installed on a machine, host your Web app in that and have Web API self hosted if needed. Of course if you already have IIS Express installed you might simply want to opt to run everything in it: both Web app and Web API.

Provided you're ok with using both internal/external apps using the same database/service you could host a single version of the WebAPI server in IIS, and simply use it for API Controllers.
You could then use two identical ASP.Net MVC sites (rather than Desktop & Web), which make calls into your WebAPI service. One hosted on the intranet, one on the internet - both hitting your WebAPI for business functionality & data persistance.
The goal here is to reduce the amount of code maintained. Essentially, it's two projects - your WebAPI project, and an ASP.Net MVC site.

I use Web API for self-hosting a number of projects that are in production. The web site http://www.hypermediaapi.com is a self hosted Web API.

No need to have two or more code bases for this scenario. What you need is a library what provides the API for your applications. The library can be used directly in the standalone application what I assume is a desktop app.
You can then create a WCF or Web API or even both layer what isn't anything more than a wrapper around the same library. The WCF/Web API contracts can be the same as your DTOs so the WCF service implementation would be something like this:
SomeObject IMyService.DoStuff(string param)
{
var myLibrary = new MyLibrary();
var someObject = myLibrary.DoStuff(param);
return someObject;
}
The only overhead would be that when an interface changes in library the changes have to be repeated in service interfaces too, but no actual business logic would be duplicated.
You can even share the interfaces, i.e. the API would expose the same interfaces what are implemented by the WCF service if you don't mind having the contract attributes on library interfaces.

Related

Web Api and web ui in same application/service

For my AngularJS web application I use Azure Service Fabric as my backend. For that I created a stateless web api service.
Do I have to put the frontend /client-side AngularJS code in the same application type or the same stateless service? Is it useful to create another stateless service for the web ui?
As others have mentioned, you can host your Angular web app anywhere you like; there are no restrictions imposed by Service Fabric in this regard.
That said, in your case a simple solution is to have your front-end web app and your web api in the same stateless service. Basically you treat this as a regular old ASP.NET MVC application, and ASP.NET MVC makes it fairly easy to have an API and web UI in the same MVC project. Here are a couple examples of this:
ASP.NET 5 MVC application with an API and a UI: https://github.com/Azure-Samples/service-fabric-dotnet-getting-started/tree/master/Services/Chatter
Single-page jQuery app with an API self-hosted with Katana: https://github.com/Azure-Samples/service-fabric-dotnet-management-party-cluster/tree/master/PartyCluster
As to the question of why do you need Service Fabric to host a front-end file. There are trade-offs here. Assuming the front-end is a web application that's meant to be part of a larger Service Fabric application - as it is in your case - it's nice to have everything in one place using one set of tools on the same SDK with the same deployment process hosted on the same cluster, rather than having a completely separate process, tools, SDK, and hosting environment for one part of your application. You also get all the Service Fabric goodness like rolling upgrades, health monitoring, high availability and auto failover, etc. The downside is that you don't get the super easy-to-use tools for web applications that come with, say, Azure App Service.

HTML5 with .NET backend App, compatible with both standalone Windows and Azure?

I need to design an HTML 5 responsive, and simple app that should work on both internal Win server and on Azure.Our Client wants to check out Azure but maybe later he will want this app to be on its own on premise servers. Our Developers are almost all .NET back ends, with basic knowledge in HTML 5, Javascript, Jquery, and bootstrap. We accepted the challenge because the project is tiny and interesting, the point is, is possible to have 1 project that can be deployed to azure or IIS with no problem? and what kind of project should we create? I think that a simple asp.net project with some web methods and js will do the job, but I don't know if it will work on azure too. Back n 2010 I did something that way but now I am not sure it's still valid
Important: the web application should be able to query oracle on premise server, via web service but not sure if take azure service bus or azure vpn
It depends on how you build your application. I have built applications in the past that works both on-premise and on Azure. As long as you don't access any Azure specific features, there's no problem to deploying the web application project to an on-premise IIS.
If you use Azure-specific features or services from Azure, such as Azure SQL DB, you have to built an on-premise version. In my case it was simple as changing the connection string and the rest was done by Entity Framework, but you can use an IoC container, such as Unity, to change your implementation based on the environment you're running on. If the Azure environment is available (check through RoleEnvironment.IsAvailable) you resolve the Azure-specific implementation of some features and if not the on-premise implementation. In most cases that are just a few dependencies, for example if you use a worker role on Azure and a Windows Service on-premise.

What's the difference between WCF Web API and ASP.NET Web API

I've done a bit of work in the past using WCF WebAPI and really liked a lot of its features, I'm just playing with ASP.NET Web API at the moment and it seems completely different (IE completely removed from WCF).
Does anyone know which features of WCF WebAPI are included in ASP.NET 4 Web API?
Ive done a little more reading around this and found a few pages by MS people on this:
http://wcf.codeplex.com/wikipage?title=How%20to%20Migrate%20from%20WCF%20Web%20API%20to%20ASP.NET%20Web%20API :
The WCF Web API abstractions map to ASP.NET Web API roughly as follows
WCF Web API -> ASP.NET Web API
Service -> Web API controller
Operation -> Action
Service contract -> Not applicable
Endpoint -> Not applicable
URI templates -> ASP.NET Routing
Message handlers -> Same
Formatters -> Same
Operation handlers -> Filters, model binders
and http://wcf.codeplex.com/discussions/319671
The integrated stack supports the following features:
Modern HTTP programming model
Full support for ASP.NET Routing
Content negotiation and custom formatters
Model binding and validation
Filters
Query composition
Easy to unit test
Improved Inversion of Control (IoC) via DependencyResolver
Code-based configuration
Self-host
From what I've learned, Microsoft did a little bit of naming confusion here.
I'm assuming you know what WCF is all about, this big framework built on top of XML to allow user to build distributed services with a wide variety of technologies (from SOAP to REST to MSMQ etc.).
It's hard as hell to use (for me at least) and requires a lot of bootstrap to have it working, and eventually they realized this and started providing some default configuration for simple http services (WCF REST starter kit anyone?). ASP.NET MVC was gaining momentum and some of the features it provided (automatic arguments matching for example) started to show up in WCF.
Now that's the situation:
Announcement: WCF Web API is now ASP.NET Web API! ASP.NET Web API
released with ASP.NET MVC 4 Beta. The WCF Web API and WCF support for
jQuery content on this site wll removed by the end of 2012.
http://wcf.codeplex.com/wikipage?title=Getting%20started:%20Building%20a%20simple%20web%20api
And that's better imho.
I'm quite sure it should be possible to host asp.net mvc4 webapi on top of WCF (if you ever need that), but i can't find documentation that can prove me right (or wrong).
UPDATE (can't fit as comment):
Wait, there is a huge different between "moving a subset of communication technology from a library/framework to another" and "replace WCF". I personally think that WCF was designed for some kind of communication concept and it has a rather cool design, but the distributed computing is somewhat moving on to new (and simpler) solutions (look the feature-rich SOAP vs the lean e flexible REST, although many people still use REST in a RPC manner), and i think that this kind of programming patterns better fit into the MVC architecture than the WCF one. Effort was put on designing some simple way of building/consuming web services on top of WCF, but they eventually found out that it was not the right solution.
Not to mention that many developers now use ASP.NET MVC and want to do rest web services for their web app, messing with WCF is often overkill for these kind of things, and I've experienced that on my own skin.
I think that the routing mechanism is awesome and the right way to go, and if you look closely, they included part of it (with different names and types, but the pattern was there) in WCF. So yeah, i think that if MS don't dismiss that part of WCF WE should do it. To strictly answer, no, i don't think you'll ever find WebGet/WebInvoke in asp.net mvc*, it just don't fit in.
Yeah self-host is probably the only bit of WCF contained in ASP.NET MVC4 right now.
It looks like WCF itself is somehow dying or at least becoming much less important then it was supposed to be and because of that it also has much less development effort put into its feature set. New features in WCF itself are more cosmetic.
WCF was designed as transport / protocol independent way for inter process communication. Even the idea was independent abstraction it was mostly build on top of SOAP stack. When WCF 3.5 brought support for REST it was mostly hacked in because REST is all about transport dependency. Using transport independent API to support inter process communication which is done through directly using transport features appeared inconvenient. As result MS first released WCF Rest API Starter Kit which never reached RTM but it was preview of features which was later included in WCF 4 and finally in .NET 4.5 or WCF Web API. Because REST is transport dependent and currently used only with HTTP (even it is theoretically possible to use other transport protocol) the API was moved to .NET part which is more suitable for HTTP processing - to currently very popular ASP.NET MVC.
WCF Web API is replaced by ASP.NET Web API which takes features from WCF Web API and merges them with the features from ASPNet MVC. ASP.NET Web API is a new (02/2012) framework for building and consuming HTTP services and a platform for building RESTful service.
Although not in the original question it seems worth noting that WCF is alive and well and its REST support remains useful when you have existing SOAP (WS-*) services you must support but want to add REST to reach more clients.
Reference
CodePlex: WCF Web API is now ASP.NET Web API
CodePlex: Daniel Roth on the Future of WCF
Chanel9: Dan Roth on the new ASP.NET Web API
The following excerpt found on this MSDN page summarizes this dilemma well.
Use WCF to create reliable, secure web services that accessible over a variety of transports. Use ASP.NET Web API to create HTTP-based services that are accessible from a wide variety of clients. Use ASP.NET Web API if you are creating and designing new REST-style services. Although WCF provides some support for writing REST-style services, the support for REST in ASP.NET Web API is more complete and all future REST feature improvements will be made in ASP.NET Web API. If you have an existing WCF service and you want to expose additional REST endpoints, use WCF and the WebHttpBinding.
Here is good article on Web Service, WCF and Web API http://goo.gl/T29A5B
Web Service
Based on SOAP and return XML Data
Support only HTTP protocol. It support only HTTP protocol.
Consumed by client that able to understand xml SOAP Services.
Can host on IIS. It can be hosted only on IIS.
Easy to Learn and understand.
WCF
Based on SOAP and return XML Data. SOAP is heavy compare then JSON and its overhead over network also.
Enhanced version of web services support multiple protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ via configuration.
More reliable when both client and server have .Net.
Its implementation and configuration is complex
Consumed by client that able to understand xml SOAP Services.
Self-hosting, IIS and using windows services.
Web API (Web API 2.0)
Design specifically for building HTTP Restful Services on .Net Framework.
Web API easily readable and handy as JSON.
Support all features of HTTP Like URls, Request/Response, Headers, Caching and Versioning.
Web API support many HTTP Verbs like GET, POST, PUT, DELETE etc.
Web API is stateless.
Web API supports MVC features (controllers, action results, routing, filter, model binders, IOC container or dependency injection)
Web API can be self-hosted, hosted with in the application and on IIS.
OWIN (Open Web Interface for .NET) is used for self-Hosting.
ASP.net web api is lightweight and REST support inbuilt. It is more suitable for mobile applications.WCF is bloated with more options . It depends on the complexity of the system to select one of these.

.net web service hosted within my application

I'm migrating an old Delphi application that I wrote into C#. The application is a datalogger that exposes logged data requests via a SOAP web service interface.
The web service is contained with the delphi graphical windows application, i.e. no need to run a web server like IIS, etc I just run the application and it's up and running under the hood.
I'm looking to do the same in my c# Windows form application, I can find loads of resources on writing web services that are ultimately hosted within IIS but am struggling to find a solution for a self contained web service within my application.
Does anyone have any suggestions or can point me towards any resources on this?
The web service does not neceserily have to be SOAP, REST is fine (in fact probably prefered).
Look into WCF Services.
Hosting and Consuming WCF Services
Hosting WCF services in a Windows Forms Application
The System.Web.Hosting namespace allows you to host ASP.Net pages without using IIS within your applications. I have never used it to host web services but I found a tutorial that seems to provide a guide on doing this-
http://msdn.microsoft.com/en-us/magazine/cc163879.aspx
If you're wanting to host a service inside your application, it's possible with the System.ServiceModel.ServiceHost class. You need to learn WCF first, but that at least answers your question to get you started. If you have any further questions, leave me a comment or two and I'll update my answer to accommodate your inquiries.

Can an asp.net mvc application also have a web service?

I have an asp.net mvc application and now I need to add a web service to go along with it. What is the best solution for this? Just create a new web service project and add it to my solution then deploy it to the same web server using a different url? I would like it to be a part of the mvc application only because I have all my database code in there.
Any suggestions would be appreciated. Thanks.
There's no reason not to add a web service project.
You state that all your database code is in your MVC project. I strongly recommend you remove it from there into a separate class library project. This third project would be used both by the web service and by the MVC application.
I also strongly recommend that you not use ASMX web services for any new development.
Use WCF only, unless you have no choice at all. There's a misconception that WCF services don't do SOAP - they do, and WCF has replaced ASMX.
Web service could mean a soap based web service or a RESTful web service. I can't think of any reason why you would not be able to simply add an asmx file to your project and be in business. That is the soap based route. If you want to be really cool though you can simple return xml from a controller action and implement a RESTful solution right over the MVC framework.
If you want to use a regular ASP.NET asmx web service, it's certainly possible. Here's an example from Scott Hanselman that does just what you are asking about and it throws in some other ASP.NET technologies for good measure.
All you have to do is File -> New Item -> Web Service and it should work like a regular ASP.NET application in your Mvc project.
i think there's a couple of things here.
you can indeed add a web service to an MVC application. you may even consider identifying the web service(s) as a script service to make REST like operations easier to perform via javascript. this may not be necessary due to your circumstances.
i think there is a stronger question as to the underlying architecture. If you are placing the web service withing your mvc application, because, your database code is already there...should it be? it might be a good time to abstract your data layer out a little. However, if you're dealing with a relatively small project and don't need the flexibilty, then certainly, add a web service right in. i guess what it really boils down to is addressing the true needs of your application.
MVC is built on the asp.net framework. You should be able to include a web service within the same project. I haven't done it but I know that you can combine asp.net forms applications with MVC applications in the same project. The same should go for web services.
Unless your application is very small I would recommend you create different projects for each logical part of the application. A good staring point is having a project for each of these:
Domain objects
Data access
Web Services
UI (your ASP.NET MVC app)
This provides a clean separation corresponding to your architecture and supports reuse.

Categories

Resources