How to Implement WCF REST in ASP.NET
I am trying to write a reusable .NET assembly that implements WCF.
My current problem is how to take an assembly (appropriately decorated with ServiceContract, DataContract, and WebGet attributes) and reference it in my existing ASP.NET WebForms application and expose it using REST.
Ideally, I like different endpoints for JQuery compliant Bare XML, JSON, and SOAP WebServices applications, but for now, I'd love to get a job with Bare XML. Right now all I'm getting is 404 Resource Not Found).
Another problem is that the API has many methods. So it will be painful to create an svc file that wraps the assembly.
Here is my system.serviceModel in my web.config
So, once I get this to work, I could then in theory write a .NET Console application that hosts this assembly on top of netTcp.
The only three ways I found to implement is this I came up with 3 possible ways to do this
- Create a new svc file that wraps calls around my `WcfService1.WcfService` and implements` WcfService1.ITest` and puts the appropriate Factory attribute in the markup. it
- Another approach is to create a `WebServiceHost` class somewhere (am I using Global.asax?), Although I don't know how, or if this is actually a valid solution.
- Use ASP.NET MVC to implement the solution, although I think the same problems as the svc approach will play out.
Thanks in advance for your help.
source to share
You basically need two things:
- create and compile your service into assembly
- find a way to host this service (in IIS, self-hosting in a console application)
I believe you have already taken step 1, so now you need to decide where to place this thing.
Hosting in IIS
In this case, you need to create a file "MyService.svc" somewhere in the virtual directory and specify "WebServiceHostFactory" as your host factory - that's it!
Create a file "MyService.svc" in a virtual directory somewhere and use this content:
<%@ ServiceHost Language="C#" Debug="True"
Service="YourNamespace.YourService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
That should be all you need to do - the new .NET 3.5 WebServiceHostFactory takes care of the rest.
Self-hosting
In this case, you need to create an EXE host - a console application or a Windows service. Inside that file, you have to create an instance of the WebServiceHost and load your service class - which is basically all there is:
WebServiceHost wsh =
new WebServiceHost(typeof(YourNamespace.YourService),
new Uri("http://localhost/YourServiceBaseUrl"));
wsh.Open();
So where is your problem? :-) Have you already decided on hosting?
Once your REST service is up and running, any application and project should be able to use it by navigating to the base service URL of the service - either the one you specified when you created and open the WebServiceHost in your own hosting, or the path to the virtual directory where your MyService.svc file lives (including the * .svc file - for example, something like
http://localhost/VirtualDir/YourService.svc
There's a really good screencast series from Aaron Skonnard available online that shows the various aspects of the WCF REST Starter Kit and how to use it in your projects very easily. Highly recommended!
Mark
source to share