Martijn
Martijn

Reputation: 12091

Configure Odata service through app.config

I am selfhosting an OData application. This currently involves a lot of hard-coding: In my DataService class itself:

public static void InitializeService(
           DataServiceConfiguration config)
        {
            // Provide read-only access to all entries and feeds. 
            config.SetEntitySetAccessRule(
               "*", EntitySetRights.AllRead);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.AllRead);

            config.UseVerboseErrors = true;
            config.DataServiceBehavior.MaxProtocolVersion = System.Data.Services.Common.DataServiceProtocolVersion.V2;
        }

and when initialising:

 Type servicetype = typeof(MessageDataService);
 Uri baseaddress = new Uri("http://localhost:8000/messageData");
 Uri[] baseaddresses = new Uri[] { baseaddress };
 using ( DataServiceHost dshost = new DataServiceHost(servicetype, baseaddresses))
 {
   dshost.Open();
   //blah
 }

I think this can be adequately summorised with "yuk". Now I can configure other WCF services neatly through App.config. Is there anything out of the box for Data services too, or should I roll my own configuration classes?

Upvotes: 1

Views: 1325

Answers (2)

Rashack
Rashack

Reputation: 4665

You can do all the configuration in app.config. It is just a bit awkward...:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="MyBindingName" >
          <security mode="Transport">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="{you service type name including the namespace i.e. myapplication.myservice}">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyBindingName" contract="System.Data.Services.IRequestHandler">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

In your code you then instantiate the host without any specific url. It will pick it from the config as usual:

var host = new DataServiceHost(typeof(YourServiceType), new Uri[0]);

See this question for detailed answer.

Upvotes: 3

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13310

WCF Data Services currently doesn't read any configuration from the config files. So rolling your own solution is the way to go.

Upvotes: 1

Related Questions