I need advice regarding WCF and n-tier - asp.net

First off, i'm fairly new to programming, I've built a few asmx web services but I am a little lost regarding how I should set up a WCF web service. I've tried to research this over the past couple days by reading through a lot of the documentation/articles/videos on MSDN but I'm still a confused.
Since my current web services are hosted on a separate box using IIS, from what I understand I need to create a WCF Library, then reference the Library in another WCF App and then host that app in IIS and reference the WCF App from my Front End? Seems like an extra step to me...? Not to mention a pain when developing on my local box.
Any advice or a point in the right direction would be very much appreciated. Thanks!

WCF gives you the option of sharing common assemblies (i.e. so both your service and clients can use the same domain model library), but it's not a necessary step.
You can host a WCF service through ASP.NET, same as ASMX. The "WCF Service Application" project template in Visual Studio configures it this way by default - as a WCF service hosted in IIS.
For the most common scenarios, it's really no different from ASMX. You create a WCF Service application, deploy it to a web server, and add service references in client applications. The importer will automatically generate classes for you, so you don't need to reference any assembly. No extra steps.
If you haven't already, you really should have a look through Microsoft's Tutorial. You'll find the steps very similar to those for setting up an ASMX-based architecture.

Related

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.

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.

How to add wcf functionality to an existing asp.net website project

I have several asp.net website projects for various sites.
Currently I want to add REST API's to these projects so I can start developing mobile apps (using HTML5/JavaScript/CSS3 and PhoneGap) that make use of these webservices.
Since WCF is far more powerful than regular asp.net webservices (among others with control over the service and authentication/authorization), I'd love to add these to my existing website project.
I did a Google search but cant find anywhere a step-by-step tutorial how this can be done. And also if there's any functionality I'd possibly loose when adding WCF functionality
I was also thinking of creating a new project specifically for WCF, but think I'd rather add it to an existing website project.
Can anyone help me with this?
Depending on exactly what your needs are and how your current web site is configured, there are two approaches.
If you are using a Web Site Project, then you should create your WCF service in a different application:
1) Create a new ASP.Net Web Application Project.
2) Add a new item to the project and select the type of WCF Service or WCF Data Service.
When you deploy this project, you will deploy it to your web server, but not as part of your web site since configuring the web.config will be a large manual effort.
If you are using a Web Application Project, then you could add the WCF Service directly to your existing project. However, I only recommend this approach if you are Silverlight applets within the web site that rely on the user's authenticated credentials.
WCF can be configured with a lot of bindings and it can be configured to return xml or json(.net 4.0). Try to create a wcf service configured to use basichttpbinding or wsHttpBinding and to format the response as json and use jquery to interact with the wcf service. This article might help you http://www.codeproject.com/KB/aspnet/Cross_Domain_Call.aspx

Silverlight and RIA Service without ASP.NET

I'm pretty new to Silverlight/RIA/Asp.Net thing, and I'm trying to figure if an Asp.Net website is required (for hosting the app) if I wish to use RIA services with Silverlight?
You could use a Web Application for hosing the RIA services in which is a compiled version of a Web Site. This is a distinction made by Visual Studio for different types of web projects but that might not be what you mean.
Really though RIA is just web service that takes a certain set of parameters. You technically could use PHP and generate the same content although that seems like it might be a bit of work unless there are frameworks that already do this.
Your RIA service will need to live somewhere online though. You don't need a website with web pages but you will need to run the service in a web server like IIS and that means it probably needs to live in a web site, even if the website doesn't have any web pages.
No, you can not use RIA services without an ASP.NET application. While it is true that you can create the backend data service with PHP (or a number of other web technologies), that would not be making use of RIA services.
The basic idea of RIA services is to provide a super simply means of plumbing your data classes through ASP.NET. Mostly it is used in conjunction with an ORM and additional metadata classes in your web application. You then provide a service to manipulate your data. When you compile the solution than the work you did in the web project gets pushed into your Silverlight application (through code generation).

WCF Project vs. A folder in the existing website project?

What way makes the most sense? I have a ASP.NET app... and maybe a Silverlight app in the future.. I want both to talk to web services..
At first, I like have the WCF project be by it self for the seperation..
But then I thought.. What is the point since I can just as easily have a 'WEBSERVICES' folder that contains all the .svc files and code in the EXISTING website project. ... Atleast that way.. deploying to a remote host will be a little easier since everything is in one project..
any other considerations ?
Why not have:
your WCF service and data contracts in a Contracts assembly
your WCF service implementations in a Services assembly
reference those two assemblies from your web site or web app
put the *.svc files into a WebServices directory
That way, you have
clean and nicely organized separation of concerns
the deployment files (*.svc) are in your web site / web app as you want
you can still extend / use your WCF code in other ways (e.g. self-hosting) later on without much fuss
Two things to consider:
Security - are your services only going to be used by your application, or do they have the potential to be used anywhere else (after all they are services). If so, you will be granting access to your application in order to grant access to your web services, or at least tweaking the access to that specific folder, which might lead to security breaches.
Deployment - If you do changes on your service that doesn't break the contract, in order to deploy the changes you will have to deploy the whole application.
I would prefer to go with the approach that Marc suggested (e.g. have an assembly for contracts, one for the service implementation) and have the webservice hosted as its own application and reference it from the ASP.NET app. This way, you have proper separation of concerns and can maintain both separately.

Resources