20% Discount for all products. Limited Availability.
0

How to use Sessions in Web Services ASP.Net

Using sessions in web services is little different from its normal usage. Here we can not access sessions with Session as in Page methods. Instead we use HttpContext.Current.Session.

Sessions should be enabled for web methods.

A sample code snippet would be as follows.

/// Summary description for ReceivingService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
     [System.Web.Script.Services.ScriptService]
    public class ReceivingService : System.Web.Services.WebService
    {
 
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public Dictionary<string, object> removeItem(int Id)
        {
            var response = new Dictionary<string, object>();
            bool found = false;
 
            if ( HttpContext.Current.Session["rec_cart"] != null)
            {
                List<CartItem> cartItems = (List<CartItem>)Session["rec_cart"];
                if (cartItems.Count > 0)
                {
                    foreach(var item in cartItems){
                        if(item.Id == Id){
                            cartItems.Remove(item);
                             HttpContext.Current.Session["rec_cart"] = cartItems;
                            found = true;
                            break;
                        }
                    }
                }
            }
            if (found)
            {
                response["status"] = true;
                response["total"] = GetGrandTotal();
            }
            else
            {
                response["status"] = false;
            }
            return response;
        }
}

I also configured cookieless sessions in web.config to get web service call properly routed.

<sessionState cookieless="true" regenerateExpiredSessionId="true"/>
0

iPhone Push Notifications PHP Server

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', YOUR_.PEM_FILE);
stream_context_set_option($ctx, 'ssl', 'passphrase', YOUR_PASSPHRASE);
 
$fp = stream_socket_client(
 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
 
if (!$fp) {
 exit("Failed to connect: $err $errstr<BR><BR>");
}
 
echo 'Connected to APN<BR>';
 
 
// Create the payload body
$body['aps'] = array(
 'badge' => +1,
 'alert' => 'Testing push notifications',
 'sound' => 'new_wall_paper.wav',
 'action-loc-key' => 'OK'
);
 
$payload = json_encode($body);
 
for ($i = 0; $i < count($tokens); $i++) {
 $msg = chr(0) . pack('n', 32) . pack('H*', $tokens [$i]) . pack('n', strlen($payload)) . $payload;
 fwrite($fp, $msg, strlen($msg));
}
 
echo "Completed sending messages";
fclose($fp);