我的FooData.svc.cs文件如下所示:(记下未声明的对象CurrentDataSource 。)
public class FooData : DataService<FooEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
// init code
}
[WebGet]
public IQueryable<someStoredProcedure_Result> someStoredProcedure(int param1, int param2)
{
return CurrentDataSource.someStoredProcedure(param1, param2).AsQueryable();
}
}
我的Authorizer.cs文件如下所示:
public class Authorizer : System.ServiceModel.ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{
if (base.CheckAccessCore(operationContext))
{
if (IsAuthorized())
return true;
else
return false;
}
else
return false;
}
private bool IsAuthorized(OperationContext operationContext)
{
// some code here that gets the authentication headers,
// etc from the operationContext
// *********
// now, how do I properly access the Entity Framework to connect
// to the db and check the credentials since I don't have access
// to CurrentDataSource here
}
}
来自Web.config的适用部分,为了更好的衡量标准:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceAuthorization serviceAuthorizationManagerType="MyNamespace.Authorizer, MyAssemblyName" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
所以我的问题与您在Authorizer.cs中的代码中看到所有星号的位置有关。从ServiceAuthorizationManager中连接到Entity Framework的推荐方法是什么,因为我无权访问CurrentDataSource?我只是建立一个单独的“连接”吗? e.g。
using (var db = new MyNamespace.MyEntityFrameworkDBContext())
{
// linq query, stored proc, etc using the db object
}
为了澄清,上面的代码工作得很好。我只是想知道这是不是正确的方法。顺便说一下,这是一个带有.NET 4.5和Entity Framework 5的Visual Studio 2012 WCF数据服务项目。