扩展OData服务

时间:2011-08-04 10:02:23

标签: multithreading wcf odata

在工作中,我们正在使用OData WCF服务来创建我们的新API。为了完全实现我们的API,我们已经开始使用自定义函数扩展服务,这些函数允许我们触发无法通过OData的常规方式公开的特定功能。

一个例子是将Workspace实体切换到高级模式。这需要大量的检查和数据混合,我们选择将其移动到单独的功能。这是我们的Api.svc服务的完整代码:

using System.Net;
using System.ServiceModel.Web;

namespace TenForce.Execution.Web
{
    using System;
    using System.Data.Services;
    using System.Data.Services.Common;
    using System.Security.Authentication;
    using System.ServiceModel;
    using System.Text;
    using Microsoft.Data.Services.Toolkit;
    using Api2;
    using Api2.Implementation.Security;
    using Api2.OData;

    /// <summary>
    /// <para>This class represents the entire OData WCF Service that handles incoming requests and processes the data needed
    /// for those requests. The class inherits from the <see cref="ODataService<T>">ODataService</see> class in the toolkit to
    /// implement the desired functionality.</para>
    /// </summary>
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class Api : ODataService<Context>
    {
        #region Initialization & Authentication

        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.UseVerboseErrors = true;
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            Factory.SetImplementation(typeof(Api2.Implementation.Api));
        }

        /// <summary>
        /// <para>This function is called when a request needs to be processed by the OData API.</para>
        /// <para>This function will look at the headers that are supplied to the request and try to extract the relevant
        /// user credentials from these headers. Using those credentials, a login is attempted. If the login is successfull,
        /// the request is processed. If the login fails, an AuthenticationException is raised instead.</para>
        /// <para>The function will also add the required response headers to the service reply to indicate the success
        /// or failure of the Authentication attempt.</para>
        /// </summary>
        /// <param name="args">The arguments needed to process the incoming request.</param>
        /// <exception cref="AuthenticationException">Invalid username and/or password.</exception>
        protected override void OnStartProcessingRequest(ProcessRequestArgs args)
        {
#if DEBUG
            Authenticator.Authenticate("secretlogin", string.Empty, Authenticator.ConstructDatabaseId(args.RequestUri.ToString()));
#else
            bool authSuccess = Authenticate(args.OperationContext, args.RequestUri.ToString());
            args.OperationContext.ResponseHeaders.Add(@"TenForce-RAuth", authSuccess ? @"OK" : @"DENIED");
            if (!authSuccess) throw new AuthenticationException(@"Invalid username and/or password");
#endif
            base.OnStartProcessingRequest(args);
        }

        /// <summary>
        /// <para>Performs authentication based upon the data present in the custom headers supplied by the client.</para>
        /// </summary>
        /// <param name="context">The OperationContext for the request</param>
        /// <param name="url">The URL for the request</param>
        /// <returns>True if the Authentication succeeded; otherwise false.</returns>
        private static bool Authenticate(DataServiceOperationContext context, string url)
        {
            // Check if the header is present
            string header = context.RequestHeaders["TenForce-Auth"];
            if (string.IsNullOrEmpty(header)) return false;

            // Decode the header from the base64 encoding
            header = Encoding.UTF8.GetString(Convert.FromBase64String(header));

            // Split the header and try to authenticate.
            string[] components = header.Split('|');
            return (components.Length >= 2) && Authenticator.Authenticate(components[0], components[1], Authenticator.ConstructDatabaseId(url));
        }

        #endregion

        #region Service Methods

        /*
         * All functions that are defined in this block, are special Service Methods on our API Service that become
         * available on the web to be called by external parties. These functions do not belong in the REST specifications
         * and are therefore placed here as public functions.
         * 
         * Important to know is that these methods become case-sensitive in their signature as well as their parameters when
         * beeing called from the web. Therefore we need to properly document these functions here so the generated document
         * explains the correct usage of these functions.
         */

        /// <summary>
        /// <para>Switches the specified <see cref="Workspace">Workspace</see> into advanced mode, using the specified
        /// Usergroup as the working <see cref="Usergroup">Usergroup</see> for the Workspace.</para>
        /// <para>The method can be called using the following signature from the web:</para>
        /// <para>http://applicationurl/api.svc/SwitchWorkspaceToAdvancedMode?workspaceId=x&usergroupId=y</para>
        /// <para>Where x stands for the unique identifier of the <see cref="Workspace">Workspace</see> entity and y stands for the unique
        /// identifier of the <see cref="Usergroup">Usergroup</see> entity.</para>
        /// <para>This method can only be invoked by a HTTP GET operation and returns a server response 200 when properly executed.
        /// If the request fails, the server will respond with a BadRequest error code.</para>
        /// </summary>
        /// <param name="workspaceId">The unique <see cref="Workspace">Workspace</see> entity identifier.</param>
        /// <param name="usergroupId">The unique <see cref="Usergroup">Usergroup</see> entity identifier.</param>
        [WebGet]
        public void SwitchWorkspaceToAdvancedMode(int workspaceId, int usergroupId)
        {
            Api2.Objects.Workspace ws = Factory.CreateApi().Workspaces.Read(workspaceId);
            Api2.Objects.Usergroup ug = Factory.CreateApi().UserGroups.Read(usergroupId);
            if(!Factory.CreateApi().Workspaces.ConvertToAdvancedPrivilegeSetup(ws, ug))
                throw new WebFaultException(HttpStatusCode.BadRequest);
        }

        #endregion
    }
}

代码有点大,但基本上这些额外函数的作用是检查每个请求提供的头文件,并使用提供的用户名和密码对应用程序进行身份验证,以确保只有有效用户才能使用我们的OData服务。

我们在底部声明的新函数中存在问题。 API需要设置usercontext才能执行该功能。这通常通过Authenticator类完成。

使用调试器,我跟踪了一个请求并检查了Authenticator是否正在调用它。但是,当触发SwitchWorkspaceToAdvancedMode函数时,此上下文将丢失,并且在没有人登录时显示。

函数调用如下:

  • 创建新的Api.svc实例
  • 触发OnStartProcessingRequest
  • 触发Authenticate方法触发
  • SwitchWorkspaceToAdvancedMode方法

但最后一个从API收到错误,指出没有登录,也没有设置用户上下文。这意味着我们将当前线程主体设置为登录的线程。

从错误消息中,我得出结论,SwitchWorkspaceToAdvancedMode的执行请求正在另一个线程上运行,因此似乎没有登录,因为这是从另一个线程完成的。

我是否正确地参与这个假设,如果是这样,我可以阻止它或解决它吗?

1 个答案:

答案 0 :(得分:0)

我通过向DataService添加新的ServiceBehavior解决了这个问题:

[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.PerSession)]

这解决了我遇到的明显的线程问题