【问题标题】:Configure Odata service through app.config通过app.config配置Odata服务
【发布时间】:2013-03-15 15:45:19
【问题描述】:

我正在自托管 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 服务。数据服务是否也有开箱即用的东西,还是我应该推出自己的配置类?

【问题讨论】:

    标签: wcf-data-services odata


    【解决方案1】:

    您可以在 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>
    

    然后在您的代码中实例化没有任何特定 url 的主机。它会像往常一样从配置中选择它:

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

    详细答案请见this question

    【讨论】:

      【解决方案2】:

      WCF 数据服务当前不从配置文件中读取任何配置。所以推出自己的解决方案是可行的方法。

      【讨论】:

      • 是的 - 有点真实。尽管我不会将主机的实例化视为 WCF 数据服务的一部分(这完全属于通用 WCF 的所有权)。但这几乎是您可以通过配置文件进行配置的唯一内容。
      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      相关资源
      最近更新 更多