locked
How do I create a Data Service host programtically? RRS feed

  • Question

  • Hi,

    I have a working WCF Data Service that was created using VS template (web.config, service1.svc, data.edmx, etc.). I need to migrate the Service to one of my test project, and like to start the service before tests run (I will use AssemblyInitiateAttribute).  Is there a way to create a data service programatically? (I believe I need to create a host, specify end points/channel, etc.),  any sample code?

    Thanks!

    Sam

    Wednesday, November 3, 2010 9:58 PM

Answers

  • Hi,

    You can use WCF to host your test context. Please refer to

    http://msdn.microsoft.com/en-us/library/system.data.services.dataservicehostfactory.aspx

    I believe you can simply new up a DataServiceHost and call open() on it as well.

    You can also use the following code to setup the host such that it will throw actual exception messages rather than the nicely formatted ones. It's recommended for test hosts. Do this before you open the connection to the host:

     

            public static void EnableServiceDebugBehavior(System.ServiceModel.ServiceHost serviceHost)
            {
                var debugBehavior = serviceHost.Description.Behaviors.Find<System.ServiceModel.Description.ServiceDebugBehavior>();
                if (debugBehavior == null)
                {
    
                    debugBehavior = new System.ServiceModel.Description.ServiceDebugBehavior();
    
                    serviceHost.Description.Behaviors.Add(debugBehavior);
    
                }
    
                debugBehavior.IncludeExceptionDetailInFaults = true;
            }
    

     

    Regards,

    PQ


    Peter Q. http://blogs.msdn.com/peter_qian
    Wednesday, November 3, 2010 10:15 PM
    Answerer