Skip to content

HowTo: Add a new feature to the Vocaluxe server

lukeIam edited this page Feb 22, 2016 · 1 revision

#HowTo: Add a new feature to the Vocaluxe server The Vocaluxe server uses WCF, so you can easly add new features to the server.

Note: WCF uses the term service for a whole instance with a lot of Contracts. Here I will use feature for a contract of our main service.

##Adding a new feature

  • Basic data types works fine as parameter or result: int, String, ENums, ... and Arrays of those.

But if you want to send or receive non-basic datatypes or packed data you have to declare a for it [DataContract] first. You may put your new struct in DataStructs.cs.

Example:

[DataContract]
   public struct SProfileData
   {
       [DataMember] public CBase64Image Avatar;
       [DataMember] public string PlayerName;
       [DataMember] public int Type;
       [DataMember] public int Difficulty;
       [DataMember] public int ProfileId;
       [DataMember] public bool IsEditable;
       [DataMember] public string Password;
   }

Note: We can use CBase64Image here because it has also a DataContract defined.

  • Add your new feature to ICWebservice:
[OperationContract, WebInvoke(Method = "[GET | POST]",
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "/[uriForYourFeature]?[[UriParameterName={FunctionParameterName}][&...]]")]
[RetrunTypeOfYourFeature] [FeatureHandlerName]([FunctionParameterType FunctionParameterName] [, ...]);

Example:

[OperationContract, WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "/getProfile?profileId={profileId}")]
SProfileData GetProfile(int profileId);
  • Now you have to implement your function in CWebservice

  • Now every time someone requests your feature uri WCF will:

  • validate the request

  • deserializes the data

  • cast the parameters

  • call your function

  • serializes the return value

  • sends it back as response

##Session functions in Vocaluxe server When you have to handle a request you may need information about the current user:

  • _GetSession() gives you the sessionID of the current user
  • CSessionControl.GetUserIdFromSession([sessionID]) gets you the UserId of the user with sessionID
  • _CheckRight([EUserRights]) gets you whether the current user has the specified right (if not the response status code is automatically set to `HttpStatusCode.Forbidden``

If you just want to check if the user session valid you can do:

Guid sessionKey = _GetSession();
if (sessionKey == Guid.Empty)
{
    if (WebOperationContext.Current != null)
    {
        WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden;
        WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
    }
    return;
}
//Do your work