WCF Module Issue

Topics: General, Troubleshooting, Writing modules
Apr 27, 2011 at 5:27 PM
Edited Apr 27, 2011 at 5:30 PM

I'm getting an InvalidOperationException on the OrchardServiceHostFactory when trying to spin up my WCF Service though routing. Its saying the registration == null so the error is being thrown.

The service is very simple.

 

    [ServiceContract]
    public interface IMyService : IDependency 
    {
        [OperationContract]
        string GetTest();
    }

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService : IMyService
    {
        public string GetTest()
        {
            return "test";
        }
    }

Only 1 route 
        public IEnumerable GetRoutes() {
            return new[] {
                             new RouteDescriptor {   Priority = 20,
                                                     Route = new ServiceRoute(
                                                         "Services",
                                                         new OrchardServiceHostFactory(),
                                                         typeof(MyService))

                             }
                         };
        }

If I flip OrchardServiceHostFactory to ServiceHostFactory, the service loads up just fine. Am I missing something when spinning up the route to the service?

Only other change that is required to make this thing run is you need this:

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <!-- ... -->
  </system.serviceModel>
In your site root web.config. when its in your module web.config you get a routing error. Tried to fix it but only thing that worked was moving this down the root.

Developer
Apr 27, 2011 at 6:39 PM
Edited Apr 27, 2011 at 6:41 PM

The current Orchard WCF support doesnt target aspNetCompatibility mode. I believe you should use ServiceRoutes instead. Orchard's implementation is in all similar to the one provided by Autofac with necessary adaptations to Orchard's host / shell / workcontext structure. You can get examples from here:

http://code.google.com/p/autofac/wiki/WcfIntegration

Naturally our support could be extended to include better OOB routing and authentication story but we still havent had the oportunity to go there. Contributions in this regard would naturally be welcomed.

 

Hope it helps,

Andre

Developer
Apr 27, 2011 at 7:45 PM

Also look at:

http://stackoverflow.com/questions/5795632/orchard-mvc-wcf-service-url-with-area

 

Andre

Apr 27, 2011 at 7:52 PM

I created a hg repo at https://bitbucket.org/e2software/orchardwcf if anyone wanted to see what its doing first hand. 

 

I'm kinda confused.

Normally there is 3 ways to access services: web.config (without .svc file), .svc file, and routing.  

Routing: I can get the route working with

new RouteDescriptor {   Priority = 20,
                                                     Route = new ServiceRoute(
                                                         "Services",
                                                         new ServiceHostFactory(),
                                                         typeof(MyService))

but if I use OrchardServiceHostFactory instead of ServiceHostFactory it throws an error at OrchardServiceHostFactory line 53 because registration is null.

.svc way:

localhost/Your.Orchard.Module/WebServices/MyService.svc I get an routing error saying 

The IControllerFactory 'Orchard.Mvc.OrchardControllerFactory' did not return a controller for the name 'WebServices', so the routing isn't letting me get to the physical svc file.

web.config:

To load up a wcf service though web.config it seems like you have to configure the root config not the area's config file. So that wouldn't work for the module based development.

Apr 27, 2011 at 7:53 PM
andrerod wrote:

Also look at:

http://stackoverflow.com/questions/5795632/orchard-mvc-wcf-service-url-with-area

 

Andre

That was my post as well :) I was seeing if I was doing something wrong with wcf and area's in general. But now it seems like I've narrowed it down to a Autofac/Orchard thing.

Apr 27, 2011 at 8:08 PM
Edited Apr 27, 2011 at 9:41 PM

This works, the key here is that you must call typeof on the object that is referencing IDependency, WorkContextModule.IsClosingTypeOf cant handle the object that consumes the dependancy, it must take the Interface that it is directly called by.

Orchard Root Web.Config

 

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <!-- ... -->
  </system.serviceModel>

 

Routes.cs

        public IEnumerable<RouteDescriptor> GetRoutes() {
            return new[] {
                             new RouteDescriptor {   Priority = 20,
                                                     Route = new ServiceRoute(
                                                         "Services",
                                                         new OrchardServiceHostFactory(),
                                                         typeof(IMyService))

                             }
                         };
        }
Developer
Apr 27, 2011 at 8:18 PM

Awesome :) So there's that alternative as well :) Glad you made it work.

Developer
Apr 28, 2011 at 1:06 PM

@Matt How have you been testing your WCF service? I've used your method and I see that the service is ok (the info screen appears when testing from browser). Unfortunately, I always get "400 Bad Request" error when I try to access the service from a browser (to eg. look inside the generated JSON response).

Cheers

Apr 28, 2011 at 1:51 PM
Edited Apr 28, 2011 at 1:56 PM

@pszmyd WcfTestClient, for normal WCF ServiceHostFactory is the main soap based service, WebServiceHostFactory is the REST Wcf service. I haven't dug deep into it just verified my service could pass data, but it looks like the OrchardServiceHostFactory is extending the ServiceHostFactory.

 

So with ServiceHostFactory you have to specify your REST/web settings and endpoints via code, attributes, and web.config.

Jun 27, 2011 at 8:48 PM

@Matt, Did you ever get this to work without having to modify the root web.config? I've read all of your posts and as far as I can tell, in order to have a WCF service running in Orchard you have to modify the root web.config. I hope you or someone found some other way around this because I want my module to just work without the user having to change their web.config.

Aug 15, 2012 at 10:02 AM

i got the same problem that  i need to  make the mudule as service for wcf  .....

someone  got  the resolution...plaease  send  an  Example    to  me  in email.......my  email  is   able2011@163.com

thanks  very much   ,BR