1

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

4

2 回答 2

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>

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

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

有关详细答案,请参阅此问题

于 2013-03-15T07:45:19.987 回答
1

WCF 数据服务当前不从配置文件中读取任何配置。所以滚动你自己的解决方案是要走的路。

于 2012-10-30T20:43:49.833 回答