Best method for using Entity Framework with web services - asp.net

I have a legacy ASP.Net web application that is basically used to process web services.
I am adding some new functionality and would like to start using EF4.1. I'm fairly new to EF, and I'm not quite sure the correct path to take here.
The client using this web service is a Linux based client running Apache, so data will be passed back and forth using SOAP.
My question is, what is the best practice for getting EF models into and out of a SOAP data class? Would using EF POCO classes be the best for this?

You could use the Entity Framework for data access with either a ASMX or WCF web service that will use SOAP.
Using Entities with Web show give you some direction and ideas.

Related

Is it Good Idea to Call WCF from WebApi ? In which Situation , We need to consider this in Design Architecture?

I am developing E commerce product where I will expose eCommerce catalog data to web,mobile platform to using asp .net webapi. I need to decide what architecture design I will use
Should I design webapi and wcf both and Need to call wcf serve method from webapi controller method or Should I directly call my Business layer Services from webapi ?
Not necessary. Another option which you can try is to use .net mvc, and then you'll be able to call methods of controller in similar a similar way to calling wcf service. It depends more on factors who develops mobile part and web site, and what he knows better. If he/they know better mvc, and know how to use it, then apply mvc, if they know better how to use wcf then use wcf.
Unless your WCF service serves a purpose only it can serve, such as a state server, I wouldn't go out of my way to create a service.

Migrate Existing Web Form Application to ASP.Net MVC Single Page Application

I have an application which is built in ASP.Net WebForms and Silverlight.
There is another WCF services application which is accessed by silverlight part of my application.
Now I have to Re create the whole application in an MVC Single Page Application(SPA) and of course the WCF one because now I would need Web API for that.
An initial solution comes into mind is to add a web api project into wcf services application and start creating web methods there and call wcf methods from there if that functionality was already there.
And for webform start a new spa with mvc project from scratch and use durandal or any other spa client framework(hell of coding).
Any useful solution/advise? as I am looking for simplicity.
I searched around and found some questions but really did not helped as they are about merging with existing apps.
Note: application is a large application with a lot of functionalities
You have to decide how confident your team is with client side frameworks. Development of Single page applications may not be easy in the beginning, but you will get used to it very quickly. It's very important to give your team the right training.
In my opinion, Durandal is the simpliest, most elegant and complete framework to build enterprise level SPAs. It is important to know how knockout, requre and durandal's router and composition work. For data centric application I would use breezejs to manipulate and cache data on the client side and request it from the server side. Breeze works best with Entity Framework on the server side, but it's not necessary. If you can use Entity Framework, it will simplify your development, because it can generate metadata of your database structure for breeze. If Entity Framework is not an option, metadata can be written by hand.
I would make server side as thin as possible. Serving data from your database using web api with ORM of your choice and some cshtml pages.
Durandal Get Started is a good place to start.
Durandal Auth is a very good template for enterprise level SPA. It may not suit you completely, but you can use it as a guidance to build different modules of your application.
Breeze samples I suggest you consider using breeze. There are good courses available on pluralsight.
Knockoutjs has good training on data-binding.

Need to develop a RESTful API (both JSON and XML)

I'm looking to make a RESTful API on ASP.NET for a website. My problem is that I need it to be integrated into the website and not as a separate project.
I understand that WCF makes this really easy and its the ideal way to do it, but I don't think you can combine a WCF Service Project and a ASP.Net Website, Is this correct?
Is there a way we can do this using a webservice (asmx) file (since I know that asmx services use SOAP no?)
The reason I need this to be in the same project is that the customer will be able to purchase ssl for their domain (which the website is going to use) and I need to make the API secure as well, but the customer should not be asked to purchase two ssl or even a wildcard one.
Knowing this, is there a better easier way of doing this using WCF?
Take a look at the new MVC4 Beta, it's set to go live sometime between March and April this year and should be able to accommodate your requirement to build a RESTful web service alongside a web application. I haven't spent too much time with MVC4 to go into the details, but it's definitely worth a look. Links: Get MVC4; MVC4 and WebAPI blog.
Hope this helps!
You can use ASPNET MVC to build an API along with your website.
See How can I implement a site with ASP.NET MVC without using Visual Studio? for some details on building a basic MVC site.
ASPNET MVC services can respond in JSON or XML, or both.
There will be no special requirement for two SSL certs.
I have an ASP.NET MVC 3 application that exposes both WCF REST services. I'm using .NET 4. You'll have to pay attention to how you configure your routing. For example, my WCF services are prefixed with something like "api/v1/" while all other requests are handled by ASP.NET MVC 3.
I had a problem because IIS refused to serve some "localhost" requests (like when your MVC 3 controllers try to consume your WCF rest services). That was solved by adding an entry to my hosts file. Also be aware of this when implementing an OAuth 2.0 Resource Server or Authorization Server.
Using WCF for REST services works ok in .NET 4, but the JSON serialization sucks big time. There are issues with default dates and it is rather slow. You may want to look at using a different serializer. With WCF you sacrifice some flexibility for some features you get for free.
ASP.NET MVC 4 (and the WEBAPI) is still in BETA, so I'd avoid that for a project with a short term release date.
I'd actually use NancyFX. Setting up routes is super-easy, and it comes with built in XML and JSON serializers that act based on the data in the headers.

Silverlight ASP.NET MVC RESTful approach

I use Silverlight 3 with ASP.NET MVC. For database operations I query SQL Server database using FOR XML, and send the data as XML over wire to Silverlight client where it is deserialized to business object. Is this approach good? I do not find much resource on Internet about using Silverlight, ASP.NET and XML together.
Why not use a SOA approach? I'll first tell you my approach then a link under that for a direct ASP.NET MVC approach. My approach is only because I need to expose a web service to other devices.
I have a WCF library which acts as the DAL and some Business Logic. I then have my asp.net reference this dll. Nothing in the Model's folder. So the ASP.NET works the same.
For the silverlight, I use a service reference to the wcf service. Some features which uses the same data as the asp.net does, calls into the ASP.NET controller that is specified as how Tim Huer did it here.

ASP.NET MVC & Web Services

Does adding a Web Service to my ASP.NET MVC project break the whole concept of MVC?
That Web Service (WCF) depends on the Model layer from my MVC project to communicate with the back-end (so it looks to me like it needs to be part of the MVC solution).
Should I add this to the Controller or Model layer?
It sounds like you should split out your model into its own assembly and reference it from your MVC-application and WCF-application.
YourApp.Data -- Shared model and data access maybe
YourApp.Web -- If you want to share more across your web-apps
YourApp.Web.Mvc
YourApp.Web.WebService
If you want to do WebServices MVC-style maybe you should use MVC to build your own REST-application.
Is there a specific reason you need to add web services to your MVC application? Unless there is a specific reason you should use your controllers in a RESTful manner just as you would a RESTful web service.
Check out this post from Rob Connery for more information:
ASP.Net MVC: Using RESTful architecture
Separating the Model into it's own project is not breaking the "MVC" pattern. First off, it is just that -- a pattern. The intention of the MVC pattern is to clearly delineate between your data, the data handlers, and the presenters and the way you interface between them. The best way to do it is how Seb suggested:
YourApp.Data
YourApp.Web.Mvc
YourApp.Web.WebService
Something that might help you out is the MVC Storefront that Rob Conery put together. Go watch the video's here:
MVC Storefront Video Series
And if you want to look at the actual code in your browser to quickly see how he did it, go here:
MVC Storefront Codeplex Code Browser
I don't think separating the model into it's own assembly has any bearing on whether or not you're using MVC, you still have a model. Where it is is irrelevant surely?
I've had a go at doing this.
See my result at my blog
ps: I don't believe that this will break the MVC concept so long as you think that a web service is the model of a repository because all a web service does is returning a XML dump.
I have added web services to my application and it works well. I don't believe it violates MVC because it is an alternative interface to your model. MVC is not appropriate for web services because web services don't have a view.
Think of web services and databases as one in the same. Under this analogy, I think it makes sense to place your web service ingteractions where you place your database logic.

Resources