none
Populate routetable of wcf from database RRS feed

  • Question

  • I want to add end points to route table of wcf dynamically.

    Code :

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class PersonService : IPersonService
        {
            [WebInvoke(UriTemplate = "", Method = "GET")]
            public void GetPerson()
            {
                string k = "a";
            }
        }
    
     [ServiceContract]
        public interface IPersonService
        {
            [OperationContract]
            void GetPerson();
    
        }
    
     public class Global : System.Web.HttpApplication
        {
            protected void Application_Start(object sender, EventArgs e)
            {
                RouteTable.Routes.Add(new ServiceRoute("api", new WebServiceHostFactory(), typeof(PersonService)));
            }
        }

    I have some end points saved in database table like below :

    /Customer/{Customer}/Address
    /Customer/{Customer}/Address/{Address}

    Now i want to populate routetable of my wcf service which will be hosted in IIS environment and is already running.

    I have search alot on internet but couldnt find anything like such in which i can dyncamically add end points to routetable of wcf.

    I will really appreciate if anybody have done anything like such and if they can post code/link that will help me.

    Thank you :)

    Monday, April 17, 2017 6:02 AM

All replies

  • Do you want to route different endpoints to different method? If so, I am afraid it is impossible. The method is routed by UriTemplate which is defined in WebInvoke. If you want to route different endpoints to different services, I suggest you try to check whether below link meets your requirement.

    # Using Routes to Compose WCF WebHttp Services

    https://blogs.msdn.microsoft.com/endpoint/2010/01/25/using-routes-to-compose-wcf-webhttp-services/


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 18, 2017 6:56 AM
  • Thank you Edward for replying.I have when web site where user will create this end points but while creating end point user will give only the name of end points and with each end point there will be 1 sql query associated with that end point.

    for eg : User created 1 end point name i.e Customer 

    Sql query : select * from customer.

    After user have created this end point now if user wants to call this end point and see the response which would be like executing sql query associated with that end point so this end point need to return response by executing that sql query.

    So only this part will be fixed in my ur l : 

    http://localhost/api/

    So are you talking about this that it is not possible of what i stated above??

    Tuesday, April 18, 2017 10:08 AM
  • Endpoints are defined by service instead of end user. 

    If you want to run different querys per end user, I would suggest you try UriTemplate like below code:

    IService.cs
            [OperationContract]        
            [WebGet(UriTemplate = "api/{tableName}")]
            string GetTableData(string tableName);
    Service.cs
            public string GetTableData(string tableName)
            {
                if (tableName == "Customer")
                {
                    return "Select * from Customer";
                }
                else
                {
                    return "Select * from other table";
                }
            }
    Request URL:
    http://localhost/WCFRest/RestService.svc/api/Customer
    http://localhost/WCFRest/RestService.svc/api/Other Table



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 19, 2017 5:54 AM
  • Ok but suppose if user created end points like this :

    /Customer/{Customer}/Address/{Address}

    Here user want list of addresses for the customer and if user try to call this end point then.End point creation and sql query is up to user that whatever user want to create.

    Wednesday, April 19, 2017 6:23 AM
  • Will below code meet?

    IService.cs 
           [OperationContract]        
            [WebGet(UriTemplate = "/Customer/{Customer}/Address/{Address}")]
            string GetTableData(string Customer,string Address);
    Service.cs
            public string GetTableData(string Customer, string Address)
            {
                if (Customer == "Customer")
                {
                    return string.Format( "Select * from {0} and {1}",Customer,Address);
                }
                else
                {
                    return string.Format("Select * from {0} and {1}", Customer, Address);
                }
            }
    

    Request URL:

    #http://localhost/WCFRest/RestService.svc/customer/c/address/a

    #http://localhost/WCFRest/RestService.svc/customer/customer/address/address

    Keep in mind, you need to define the UriTemplate at develop time. It could not be crated at runtime.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 20, 2017 2:41 AM
  • But user will create any kind of end points from my website  like below :

    /Customer/{Customer}/Address/{Address}

    /Customer/{Customer}/Product/{Products}

    /Customer/{Customer}/Orders/{Order}

    So when this end points will be saved by user i need to do something so i am trying to figure out that only that how i will manage this because end points are created by user on the fly so i cant define them at developing time like you have done in your answer.

    There is no way to do this task?

    Wednesday, April 26, 2017 2:26 PM
  • >>There is no way to do this task?

    It is impossible. The URL are defined at design time, it could not change dynamically. In a normal process, customer provide a request, and developer develops the REST Service according the request. We could not make sure our service will meet all of the customer requests at first developer time. You need to provide different method with UriTemplate for different request format.

    In my option, we need to follow single duty design mode, there should be different end points for different requests like one function for Address and one for Product. From customer side, there is no difference when calling requests, from developer side, it is more easier to develop and debug while there is something wrong in the response.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 27, 2017 6:02 AM
  • I was thinking like i will define 4 common method for get,post,put and delete.

    Now when any request will come for get like(/Customer or /Products) ,my end point will hit that get method only.

    Something like this but this is still just a thought.

    What do you think about this??

    Friday, April 28, 2017 6:16 AM
  • >>Now when any request will come for get like(/Customer or /Products) ,my end point will hit that get method only.

    This is not supported in WCF Rest Service. There is no way to achieve your requirement in WCF Rest Service.

    For route by http method, I assume you could try Web API, but it also not full supports your requirement. 

    If you are interested in Web api route, I suggest you refer below link.

    #Routing in ASP.NET Web API

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 28, 2017 8:07 AM
  • I am confused here that how this can be done with web api and not with wcf.

    Can you brief me alittle bit about this that how this can be done in web api and not with wcf??

    Friday, April 28, 2017 9:49 AM
  • >>how this can be done with web api and not with wcf

    Web Api is routed by route table, and WCF is not. WCF is accessed by UriTemplate per to request. 

    Mapping Http method to function is a feature in Web API which is not supported in WCF.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 1, 2017 1:53 AM
  • Hi Edward,

               Sorry for the late reply but do you think i would be able to achieve my requirement in web api??

     Is this possible with web api??

    Tuesday, May 30, 2017 5:57 AM
  • I suggest you check whether below link meets your requirement.

    #Using Configuration to Define Web API Routes

    http://arcware.net/using-configuration-to-define-web-api-routes/

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 31, 2017 4:58 AM