通过app.config配置Odata服务

时间:2012-10-30 15:16:57

标签: wcf-data-services odata

我是自我托管的OData应用程序。这当前涉及很多硬编码:在我的DataService类中:

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;
        }

初始化时:

 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
 }

我认为这可以与" yuk"进行充分的总结。现在我可以通过App.config整齐地配置其他WCF服务。是否有任何开箱即用的数据服务,或者我应该滚动我自己的配置类?

2 个答案:

答案 0 :(得分:3)

您可以在app.config中执行所有配置。这有点尴尬......:

<?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>

在您的代码中,您可以在没有任何特定网址的情况下实例化主机。它将像往常一样从配置中选择它:

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

有关详细解答,请参阅this question

答案 1 :(得分:1)

WCF数据服务当前未从配置文件中读取任何配置。因此,滚动自己的解决方案是可行的方法。