views:

539

answers:

1

I'm trying to write a re-usable .NET Assembly that implements WCF.

My current problem is how to take the 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 compatible Bare XML, JSON and SOAP WebServices, but right now I'd be happy with getting Bare XML to work. Right now all I get is 404 Resource cannot be found).

Another issue is that the API has many methods. As such, it is going to be painful to have to create a 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 theoretically write a .NET Console app that hosts this assembly over netTcp.

The only 3 ways that I have found to implement this are I've come 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 putting appropriate Factory attribute in the markup. This is not desirable at all.
    The other approach is to create a `WebServiceHost` class somewhere (do I use Global.asax?), although I do not know how to do this, or if this is a valid solution at all.
    Use ASP.NET MVC to implement a solution, although I think the same problems as the svc approach will come in to play.

Thanks in advance for any help.

+2  A: 

You need basically two things:

  • create and compile your service into an assembly
  • find a way to host that service (in IIS, self-hosting in console app)

I believe you've done step 1 already, so now you'll need to decide where to host this thing.

Hosting in IIS
In that case, you need to create a "MyService.svc" file somewhere in a virtual directory, and specify the "WebServiceHostFactory" as its host factory - that's it!

Create a file "MyService.svc" in a virtual directory somewhere, and use this contents:

<%@ 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 all the rest.

Self-hosting
In this case, you need to create a host EXE - either a console app or a Windows service. Inside it, you'd instantiate the WebServiceHost and load up your service class - that's 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 made up your mind on hosting already?

Once your REST service is up and running, any app and project should be able to use it by navigating to the base service URL - either the one you specified when you created and open the WebServiceHost in your self-hosting, or the path of the virtual directory where your MyService.svc file lives (including the *.svc file - e.g. something like

http://localhost/VirtualDir/YourService.svc

There's a really good screencast series by Aaron Skonnard available online which shows the various aspects of the WCF REST starter kit and how to use it in your projects very easily. Highly recommended !

Marc

marc_s