WCF Application under mojoPortal root installation not working

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
11/12/2011 12:18:41 PM
Gravatar
Total Posts 3

WCF Application under mojoPortal root installation not working

I'm using WCF to handle AJAX calls from the client.  I'm using mojoPortal for rendering the client.

So, the webpage URL is something like: http://www.mysite.com/Default.aspx.

Originally, I configured a separate IIS site called http://api.mysite.com to host the WCF application.

This URL scheme runs into cross-domain issues, so it's better to use a url like: http://www.mysite.com/api

However, creating an application under the root mojoPortal installation doesn't work.

Even calling just the WCF service (http://www.mysite.com/api/MyRestService) returns a 400 Bad Request error.  So it's not specific to a method in the service, but the entire service itself.

There is a web.config conflict with 'UrlRoutingModule'.  WCF uses System.Web.Routing.UrlRoutingModule.  However, mojoPortal has its own routing module called mojoPortal.Web.Routing.RoutingModule.

Has anybody successfully setup a WCF application to run as a sub-folder application in a mojoPortal installation?  If so, can you provide the configuration that you used?  Or any other advice on why this isn't working?

Thanks!
Travis

11/12/2011 4:47:09 PM
Gravatar
Total Posts 3

Re: WCF Application under mojoPortal root installation not working

As is usually the case, posting the issue publicly helps me find the solution faster; quite the odd phenomenon. :)

Anyway, I figured out how to get the WCF application to work properly as an IIS application under mojoPortal.

1. In IIS Manager, create a new Application under your mojoPortal website.  Right-click -> Add Application.  Select the folder of your WCF project.  I used 'api' for the Application (virtual folder) name.

2. Here's the test WCF Service class:

              namespace WcfTest
              {

               [ServiceContract]
               public interface ITestRest
               {

                [OperationContract]
                [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
                string TestGetNoParameters();

                [OperationContract]
                [WebInvoke(Method = "*", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
                string TestInvokeAnyMethodNoParameters();

                [OperationContract]
                [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
                string TestPostNoParameters();

               }

               [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
               [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
               public class TestRestService : ITestRest
               {
                public string TestGetNoParameters()
                {
                 return "success";
                }

                public string TestInvokeAnyMethodNoParameters()
                {
                 return "success";
                }

                public string TestPostNoParameters()
                {
                 return "success";
                }
               }
              }

 

3. Here's the web.config of the WCF project:

            <?xml version="1.0"?>
              <configuration>

                <system.web>
               <compilation debug="true" targetFramework="4.0" />
                </system.web>

                <system.serviceModel>
               <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
               <standardEndpoints>
                 <webHttpEndpoint>
                <standardEndpoint name="TestRestService/TestGetNoParameters" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat ="Json" crossDomainScriptAccessEnabled="true"/>
                <standardEndpoint name="TestRestService/TestInvokeAnyMethodNoParameters" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat ="Json" crossDomainScriptAccessEnabled="true"/>
                <standardEndpoint name="TestRestService/TestPostNoParameters" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat ="Json" crossDomainScriptAccessEnabled="true"/>
                 </webHttpEndpoint>
               </standardEndpoints>
                </system.serviceModel>

              </configuration>

 

4. This is what the global.asax.cs should look like:

              namespace WcfTest
              {
               public class Global : HttpApplication
               {
                void Application_Start(object sender, EventArgs e)
                {
                 RegisterRoutes();
                }

                private void RegisterRoutes()
                {
                 RouteTable.Routes.Add(new ServiceRoute("TestRestService", new WebServiceHostFactory(), typeof(TestRestService)));
                }
               }
              }

 

5. Navigate to the TestGetNoParameters method and you should get 'success'.

        http://www.mymojosite.com/api/TestRestService/TestGetNoParameters

 

Hopefully this helps somebody out; most likely me when I forget in 2 months. :)

11/13/2011 6:59:39 AM
Gravatar
Total Posts 18439

Re: WCF Application under mojoPortal root installation not working

Glad you figured it out.

There are actually several approaches one could use to integrating WCF services.

One could implement them in a custom web app project and use post build events to copy the dll and other needed files up to mojoPortal web project (the same approach we use for custom features that plugin to mojoPortal), and we have a not yet documented and not yet well tested way that you can add routes. As you know routes must be registered in application start event in global.asax but one should not modify the mojoPortal code so you cannot directly add code to our global.asax, but we have a system where you can plugin routes by xml config files under /Setup/Routes and mojoPortal will register the routes based on the configuration. There are some examples commented out in a file in that folder. Using this approach would mean that in your custom web app you cannot have a global.asax because there can be only one and your code would run in the context of the mojoPortal web which already has one. Then of course you still have to register your end points in web.config which means a bit more maintenance of web.config during upgrades of mojoPortal.

The other approach is the one you have done using a separate app in a sub folder (or a separate site). The benefits of that approach are that you don't have to maintain the endpoints in the mojoPortal web.config and you have the ability to move that service to a different machine if you ever needed to. Of course this approach may still require some modification to mojoPortal Web.config like adding a location element around things if there are any conflicts. 

Best,

Joe

1/20/2012 4:15:29 PM
Gravatar
Total Posts 11

Re: WCF Application under mojoPortal root installation not working

Hello

 

When creating the separate app under the mojoportal, this new app has its own web.config right? I 've done this and it appears its also using the web.config under the main mojoportal site... is this correct? So the apps web.config has its own settings and it also accesses the main portals web.config too?


Thanks

Harold

1/21/2012 7:10:05 AM
Gravatar
Total Posts 18439

Re: WCF Application under mojoPortal root installation not working

Hi Harold,

See the first item in our developer FAQ.

If you have mojoportal running in the root you should wrap a <location element around both the system.web and system.webServer sections of the mojoPortal web.config to prevent inheritence in the apps in sub folders.

Hope that helps,

Joe

1/21/2012 9:35:20 PM
Gravatar
Total Posts 11

Re: WCF Application under mojoPortal root installation not working

ok, that did it Joe, thanks! To get around it before, I copied the dll's from the main site's bin folder to the subapplications bin folder. Wrapped that tag around those sections, and I could delete the dll's.. worked great.

 

Thanks very much!

Harold

You must sign in to post in the forums. This thread is closed to new posts.