none
WCF assigning BindingConfiguration to Service EndPoint created in code RRS feed

  • Question

  • The application uses a function in the Global.asax.cs file to create the WCF Service endpoints in code.

    RouteTable.Routes.Add(new ServiceRoute("Svc1", new WebServiceHostFactory(), typeof(DataService1)));

    MSDN won't let me post a link to the article - but an explanation can be found by searching "RESTful WCF Services with No svc file and No config".

    Now to enable JSONP I need to be able to assign the endpoint to a BindingConfiguration contained in the web.config that sets crossdomainscriptaccessenabled="true".

    <bindings>
    <webhttpbinding> 
       <binding name="webHttpBindingWithJsonP" crossdomainscriptaccessenabled="true" />
    </webhttpbinding>
    </bindings>

    Any ideas how to assign the binding config "webHttpBindingWithJsonP" to the service endpoints being created in code?

    Saturday, January 9, 2016 5:27 PM

Answers

  • Hi Brian Hale,

    According to this case, in my understanding is that you want to create a WCF Restful Service

    via code. You want to use a virtual path to call the service with no svc suffix.

    you can add the global file like you post code.

    If you want to configure some section with code, you can refer to the following code:

     WebHttpBinding web =new WebHttpBinding();
                web.CrossDomainScriptAccessEnabled=true;                      
                using(WebServiceHost host = new WebServiceHost(typeof(OrderProcessorService),new Uri("http://localhost:8000")))
                {                
                    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IOrderProcessor), web, "");
                    ep.EndpointBehaviors.Add(new WebHttpBehavior { HelpEnabled = true, AutomaticFormatSelectionEnabled=true });                         
                    ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>();                             
                    stp.HttpsHelpPageEnabled = false;
                    host.Open();
                    Console.Read();
                    
                }

    For how to use the jsonp, please refer to the following articles:

    1.Using JSONP

    2.How to: Create a Basic WCF Web HTTP Service

    I hope that will be helpful to you.

    Best Regards,

    Wanjun Dong


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, January 11, 2016 6:24 AM
    Moderator

All replies

  • Hi Brian Hale,

    According to this case, in my understanding is that you want to create a WCF Restful Service

    via code. You want to use a virtual path to call the service with no svc suffix.

    you can add the global file like you post code.

    If you want to configure some section with code, you can refer to the following code:

     WebHttpBinding web =new WebHttpBinding();
                web.CrossDomainScriptAccessEnabled=true;                      
                using(WebServiceHost host = new WebServiceHost(typeof(OrderProcessorService),new Uri("http://localhost:8000")))
                {                
                    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IOrderProcessor), web, "");
                    ep.EndpointBehaviors.Add(new WebHttpBehavior { HelpEnabled = true, AutomaticFormatSelectionEnabled=true });                         
                    ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>();                             
                    stp.HttpsHelpPageEnabled = false;
                    host.Open();
                    Console.Read();
                    
                }

    For how to use the jsonp, please refer to the following articles:

    1.Using JSONP

    2.How to: Create a Basic WCF Web HTTP Service

    I hope that will be helpful to you.

    Best Regards,

    Wanjun Dong


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, January 11, 2016 6:24 AM
    Moderator
  • Hi Wanjun - we're hosting this WCF service in IIS.  The issue I'm facing is how to set the crossdomainscriptaccessenabled="true" parameter on the BindingConfiguration for the endpoint created via:

    RouteTable.Routes.Add(new ServiceRoute("Svc1", new WebServiceHostFactory(), typeof(DataService1)));

    Monday, January 11, 2016 5:44 PM
  • Hi Brian Hale,

    As far as I know we can configure it to web.config file or with code.

    But, I am not sure that can be configured to Global.cs file.

    Best Regards,

    Wanjun Dong

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, January 13, 2016 9:24 AM
    Moderator