EWS Notification Hub for multiple users - asp.net

I am trying to create a service to push exchange notifications to asp.net applications, eventually using SignalR.
My plan is to create a notification hub that subscribes each user as they log in to the asp application and listen for notifications for them. As a notification is received the second part of the project is to then use signalR to only send the correct notifications to each user. Once they log out or time out the notification hub will unsubscribe them.
So far I have done a little basic testing and can receive notifications in a little console app for myself with my credentials hard coded. What I am struggling with is how to do this for multiple people simultaneously. For example would I have to create separate threads of this for each user or is there a better way?
I guess regardless I am going to have to use impersonation rather than holding each users credentials right? I'm also going to have to work out a way to auto refresh the timeout for each user if they have an active session.
Below is a little code I found and have been playing with, I would be grateful for any ideas or example anyone could share of how I could best achieve this.
Many thanks
Andy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Url = new Uri("https://server/EWS/Exchange.asmx");
service.Credentials = new NetworkCredential("user", "pass", "domain");
SetStreamingNotifications(service);
while (true)
{ }
}
static void SetStreamingNotifications(ExchangeService service)
{
// Subscribe to streaming notifications on the Inbox folder, and listen
// for "NewMail", "Created", and "Deleted" events.
StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(
new FolderId[] { WellKnownFolderName.Calendar, WellKnownFolderName.Inbox },
EventType.Created,
EventType.Modified);
StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 9);
connection.AddSubscription(streamingsubscription);
// Delegate event handlers.
connection.OnNotificationEvent +=
new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
connection.OnSubscriptionError +=
new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
connection.OnDisconnect +=
new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
connection.Open();
Console.WriteLine("--------- StreamSubscription event -------");
}
static private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
// Cast the sender as a StreamingSubscriptionConnection object.
StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
// Ask the user if they want to reconnect or close the subscription.
ConsoleKeyInfo cki;
Console.WriteLine("The connection to the subscription is disconnected.");
Console.WriteLine("Do you want to reconnect to the subscription? Y/N");
while (true)
{
cki = Console.ReadKey(true);
{
if (cki.Key == ConsoleKey.Y)
{
connection.Open();
Console.WriteLine("Connection open.");
break;
}
else if (cki.Key == ConsoleKey.N)
{
// The ReadKey in the Main() consumes the E.
Console.WriteLine("\n\nPress E to exit");
break;
}
}
}
}
static void OnEvent(object sender, NotificationEventArgs args)
{
StreamingSubscription subscription = args.Subscription;
// Loop through all item-related events.
foreach (NotificationEvent notification in args.Events)
{
switch (notification.EventType)
{
case EventType.NewMail:
Console.WriteLine("\n-------------Mail created:-------------");
break;
case EventType.Created:
Console.WriteLine("\n-------------Item or folder created:-------------");
break;
case EventType.Deleted:
Console.WriteLine("\n-------------Item or folder deleted:-------------");
break;
}
// Display the notification identifier.
if (notification is ItemEvent)
{
// The NotificationEvent for an e-mail message is an ItemEvent.
ItemEvent itemEvent = (ItemEvent)notification;
Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);
}
else
{
// The NotificationEvent for a folder is an FolderEvent.
//FolderEvent folderEvent = (FolderEvent)notification;
//Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);
}
}
}
static void OnError(object sender, SubscriptionErrorEventArgs args)
{
// Handle error conditions.
Exception e = args.Exception;
Console.WriteLine("\n-------------Error ---" + e.Message + "-------------");
}
}
}

The way I solved this problem is by:
Having an account that has the right to impersonate all users.
I create a service for that account with giving a username and
password.
I impersonate a user and add the subscription of the user to the
connection
I create another service which is a close for the main service with
the same username and password, which will impersonate another user
and then add the subscription to the same connection before
Here are two parts of my code . Forget about the LogDevice it is just something internally.
The first part is the detailed impersonation and adding the service to the list of services
the list of services in my case is a dictionary with the userSMTP is the key , the UserSMTP key here is the impersonated account.
/// <summary>
/// Impersonate one user at a time and without using the autodiscovery method to find the proper url for the userSmtp,
/// and copy the provided url to the usersmtp.
/// </summary>
/// <param name="url"> </param>
/// <param name="userSmtp">user smtp</param>
/// <param name="enableTrace">to enable logging from the XML tracing </param>
/// <param name="exchangeVersion">Exchange server version used</param>
private Uri ImpersonateUser(Uri url, string userSmtp, bool enableTrace, ExchangeVersion exchangeVersion)
{
Uri result = url;
var log = "ImpersonateUser \n";
try
{
log += "0/8 Checking services redundancy\n";
if (Services.ContainsKey(userSmtp))
{
Services.Remove(userSmtp);
}
log += "1/8 Create a new service for " + userSmtp + "\n";
var service = new ExchangeService(exchangeVersion);
log += "2/8 Get credentials for the service\n";
var serviceCred = ((System.Net.NetworkCredential)(((WebCredentials)(Services.First().Value.Credentials)).Credentials));
log += "3/8 Assign credentials to the new service\n";
service.Credentials = new WebCredentials(serviceCred.UserName, serviceCred.Password);
log += "4/8 TraceEnabled is" + enableTrace.ToString() + "\n";
service.TraceEnabled = enableTrace;
log += "5/8 Get the Url for the service with AutodiscoverUrl \n";
service.Url = url;
log += "6/8 Assign a new ImpersonatedUserId to the new service for" + userSmtp + "\n";
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, userSmtp);
try
{
log += "7/8 Validating the impersonation\n";
RuleCollection rulecoll = service.GetInboxRules();
}
catch (Exception ex)
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUser: failed to validate the impersonation for {0}\n Exception: {1}\n", userSmtp, ex.Message);
int hr = System.Runtime.InteropServices.Marshal.GetHRForException(ex);
if (hr == -2146233088) // We do not have right to impersonate this user.
{
result = null;
return result;
}
else
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUser(2): trying to resolve {0} with Autodiscover instead...", userSmtp);
result = ImpersonateUser(userSmtp, enableTrace, exchangeVersion);
}
}
log += "8/8 Adding the service \n";
if (!Services.ContainsKey(userSmtp))
{
Services.Add(userSmtp, service);
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUser(2): {0} has been impersonated\n", service.ImpersonatedUserId.Id);
}
}
catch (Exception ex)
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUser(2): exception {0}\n The exception occured after the following steps: \n{1}", ex.Message, log);
}
return result;
}
And here is the code that calls the previous function (i.e. for all users) put in mind that you should somehow storing the email address for every account you want to impersonate.
/// <summary>
/// To Impersonate users in order to get the info from them.
/// </summary>
/// <param name="userSmtps">List of users to be impersonated</param>
/// <param name="enableTrace"> To enable logging from the XML tracing</param>
/// <param name="exchangeVersion">Exchange server version used </param>
public void ImpersonateUsers(ICollection<string> userSmtps)
{
var log = "ImpersonateUsers\n";
var firstUserSmtp = "";
if (userSmtps != null)
if (userSmtps.Count > 0)
{
//the url for the first smtp
try
{
log += "1/2 Impersonating the first userSmtp\n";
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUsers: Getting the Url from the autodiscovery for the first smtp {0} ", userSmtps.First());
bool enableTrace = Services.First().Value.TraceEnabled;
ExchangeVersion exchangeVersion = Services.First().Value.RequestedServerVersion;
firstUserSmtp = userSmtps.First();
var commonSmtpUrl = ImpersonateUser(userSmtps.First(), enableTrace, exchangeVersion);
if (commonSmtpUrl == null) userSmtps.Remove(firstUserSmtp);
// If the list contains other than the first one
log += "2/2 Impersonating " + (userSmtps.Count - 1) + " userSmtps\n";
if (userSmtps.Count >= 1)
{
foreach (var userSmtp in userSmtps)
{
try
{ //skip ther first one because it is already impersonated.
if (userSmtp == firstUserSmtp)
{
continue;
}
// Impersonate the users
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUsers: Impersonating {0} ...", userSmtp);
commonSmtpUrl = ImpersonateUser(userSmtp, enableTrace, exchangeVersion);
}
catch (Exception ex)
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUsers: Impersonating {1}\n exception {0}\n", ex.Message, userSmtp);
}
}
}
}
catch (Exception ex)
{
_logDevice.LogSrvMessage(1, "ExchangeLiteService: ImpersonateUsers: exception {0}\n The exception occured after the following steps: \n{1}", ex.Message, log);
}
}
}
I would have put the subscription part and adding to the connection , but it is a bit ugly and hard to get. but the idea is simply that you should have a connection, and then you go to each service you made and then `connection.AddSubscription(streamingSubscription);
Where streamingSubscription is extracted from the service.

Related

Is there a way for a UWP app to know if Recurring Billing is turned on for a subscription in the Microsoft store

I have a Xamarin Forms UWP app. I am trying to provide more specific information to users of my app regarding their subscription expiration / renewal. I would like to be able to tell if they have turned off recurring billing for the subscription. Below are the details I get back from the store regarding the current subscription. I assume the isActive will turn to false after they turn off recurring billing AND cancel the subscription after Expiration date. However, in my testing I have found that if they turn off recurring billing and are still within the the subscription period of their last recurring purchase (i.e. the expiration date is still in the future), everything seems to look exactly as if they have recurring billing turned on and the subscription will auto-renew. I am trying find a way to let them know that their current subscription will expire on a specific date versus will auto-renew on a specific date. Am I missing something?
Here is the code -- Everything works fine I just don't see anything in the object returned from the Microsoft Store API that indicates if recurring billing is on for the subscription.
using FCISharedAll;
using FCISuite.UWP;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Windows.Services.Store;
using static FCISharedAll.FCIEnums.FlexEnums;
[assembly: Xamarin.Forms.Dependency(typeof(UWPSubscriptionManager))]
namespace FCISuite.UWP
{
public class UWPSubscriptionManager : IFCISubcriptionManager, IDisposable
{
private StoreContext context = null;
private static string[] ia_productKinds = { "Durable", "Consumable", "UnmanagedConsumable" };
private List<String> iobj_filterList = null;
//We need a constructor to load the list
public UWPSubscriptionManager()
{
iobj_filterList = new List<string>(ia_productKinds);
}
/// <summary>
/// This will run at the start of the app to get the active subscription info for the user.
/// </summary>
/// <returns></returns>
public async Task<ActiveSubscriptionDetails> GetActiveSubscriptionInformation(ActiveSubscriptionDetails pobj_SubscriptionData)
{
string ls_Result = string.Empty;
try
{
if (context == null)
{
context = StoreContext.GetDefault();
// If your app is a desktop app that uses the Desktop Bridge, you
// may need additional code to configure the StoreContext object.
// For more info, see https://aka.ms/storecontext-for-desktop.
}
StoreAppLicense appLicense = await context.GetAppLicenseAsync();
// Check if the customer has the rights to the subscription.
foreach (var addOnLicense in appLicense.AddOnLicenses)
{
StoreLicense license = addOnLicense.Value;
if (license.IsActive)
{
pobj_SubscriptionData.ActiveSubscriptionID = license.SkuStoreId;
pobj_SubscriptionData.SubscriptionEndDate = license.ExpirationDate.DateTime;
}
}
}
catch (Exception ex)
{
SharedErrorHandler.ProcessException(ex);
}
//Debug.WriteLine("End of Subscription Info");
return pobj_SubscriptionData;
}
#region get available subscriptions
public async Task<List<SubscriptionDetail>> GetAvailableSubscriptions(string ps_ProductNotFoundMessage,
string ps_StoreAddOnsNotFound, string ps_EnvironmentPrefix)
{
string ls_Result = "";
List<SubscriptionDetail> lobj_AvailableSubscriptions = new List<SubscriptionDetail>();
try
{
if (context == null)
{
context = StoreContext.GetDefault();
// If your app is a desktop app that uses the Desktop Bridge, you
// may need additional code to configure the StoreContext object.
// For more info, see https://aka.ms/storecontext-for-desktop.
}
// Get app store product details. Because this might take several moments,
// display a ProgressRing during the operation.
StoreProductResult product_queryResult = await context.GetStoreProductForCurrentAppAsync();
if (product_queryResult.Product == null)
{
// Show additional error info if it is available.
if (product_queryResult.ExtendedError != null)
{
ls_Result += $"\nExtendedError: {product_queryResult.ExtendedError.Message}";
}
// The Store catalog returned an unexpected result.
throw new Exception(ps_ProductNotFoundMessage + " " + ls_Result);
}
else
{
StoreProductQueryResult addon_queryResult = await context.GetAssociatedStoreProductsAsync(iobj_filterList);
if (addon_queryResult.ExtendedError != null)
{
// The user may be offline or there might be some other server failure.
ls_Result = $"ExtendedError: {addon_queryResult.ExtendedError.Message}";
// The Store catalog returned an unexpected result for the Add-ons.
throw new Exception(ps_StoreAddOnsNotFound + " " + ls_Result);
}
List<KeyValuePair<string, StoreProduct>> lobj_ProductsforEnvironment = new List<KeyValuePair<string, StoreProduct>>();
//We are in test - do not show the production subscriptions
lobj_ProductsforEnvironment = (from tobj_Product in addon_queryResult.Products
where tobj_Product.Value.Title.ToUpper().StartsWith(ps_EnvironmentPrefix.ToUpper())
select tobj_Product).ToList();
//foreach (KeyValuePair<string, StoreProduct> item in addon_queryResult.Products)
foreach (KeyValuePair<string, StoreProduct> item in lobj_ProductsforEnvironment)
{
// Add the store product's skus to the list
foreach (StoreSku lobj_Sku in item.Value.Skus)
{
if (lobj_Sku.IsSubscription)
{
lobj_AvailableSubscriptions.Add(new SubscriptionDetail()
{
SubscriptionID = item.Value.StoreId,
SubscriptionName = lobj_Sku.Title,
SubscriptionFormattedPrice = lobj_Sku.Price.FormattedPrice,
// Use the sku.SubscriptionInfo property to get info about the subscription.
// For example, the following code gets the units and duration of the
// subscription billing period.
SubscriptionPeriodUnit = lobj_Sku.SubscriptionInfo.BillingPeriodUnit.ToString(),
SubscriptionPeriod = lobj_Sku.SubscriptionInfo.BillingPeriod,
});
}
}
}
}
}
catch (Exception ex)
{
SharedErrorHandler.ProcessException(ex);
}
return lobj_AvailableSubscriptions;
}
#endregion
#region Subscription Purchase area
/// <summary>
/// This will get the product for the ID from the store, then request to purchase it for the user.
/// </summary>
/// <param name="ps_SubscriptionStoreId">The ID of the store subscription to be purchased</param>
/// <returns>boolean that indicates if the purchase succeeded or failed.</returns>
public async Task<SubscriptionPurchaseDetail> PurchaseSubscription(string ps_SubscriptionStoreId)
{
SubscriptionPurchaseDetail lobj_SubscriptionPurchaseDetail = new SubscriptionPurchaseDetail();
try
{
lobj_SubscriptionPurchaseDetail.PurchaseStatus = FCIStorePurchaseStatus.Succeeded;
lobj_SubscriptionPurchaseDetail.ExtendedErrorMessage = string.Empty;
lobj_SubscriptionPurchaseDetail.ProductToPurchaseWasFound = true;
//This gets the product to purchase
var tobj_StoreProduct = await GetSubscriptionProductAsync(ps_SubscriptionStoreId);
if (tobj_StoreProduct == null)
{
lobj_SubscriptionPurchaseDetail.ExtendedErrorMessage = "The add-on to purchase was not found. Product ID: " + ps_SubscriptionStoreId;
lobj_SubscriptionPurchaseDetail.ProductToPurchaseWasFound = false;
}
else
{
//Get the store product to purchase
StorePurchaseResult result = await tobj_StoreProduct.RequestPurchaseAsync();
//Convert the store namespace string to our string.
lobj_SubscriptionPurchaseDetail.PurchaseStatus = (FCIStorePurchaseStatus)Enum.Parse(typeof(FCIStorePurchaseStatus), result.Status.ToString());
// Capture the error message for the operation, if any.
if (result.ExtendedError != null)
{
lobj_SubscriptionPurchaseDetail.ExtendedErrorMessage += " " + result.ExtendedError.Message;
}
}
}
catch (Exception ex)
{
SharedErrorHandler.ProcessException(ex);
}
return lobj_SubscriptionPurchaseDetail;
}
/// <summary>
/// This returns the product we will use to call the RequestPurchaseAsync function on
/// </summary>
/// <param name="ps_SubscriptionStoreId">The ID if the subscription to purchase</param>
/// <returns>StoreProduct which represents subscritpion to purchase.</returns>
private async Task<StoreProduct> GetSubscriptionProductAsync(string ps_SubscriptionStoreId)
{
try
{
if (context == null)
{
context = StoreContext.GetDefault();
// If your app is a desktop app that uses the Desktop Bridge, you
// may need additional code to configure the StoreContext object.
// For more info, see https://aka.ms/storecontext-for-desktop.
}
// Load the sellable add-ons for this app and check if the trial is still
// available for this customer. If they previously acquired a trial they won't
// be able to get a trial again, and the StoreProduct.Skus property will
// only contain one SKU.
StoreProductQueryResult result = await context.GetAssociatedStoreProductsAsync(iobj_filterList);
if (result.ExtendedError != null)
{
throw new Exception("Something went wrong while getting the add-ons from the store. ExtendedError:" + result.ExtendedError);
}
// Look for the product that represents the subscription and return it if found - this is what we
//will purchase
foreach (var item in result.Products)
{
StoreProduct product = item.Value;
if (product.StoreId == ps_SubscriptionStoreId)
{
return product;
}
}
//If we get here the subscription was not found. We will return null and the calling function will know the
//subscription was not found.
}
catch (Exception ex)
{
SharedErrorHandler.ProcessException(ex);
}
return null;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
context = null;
}
}
#endregion
}
}
Currently, there is no such API from StoreContext that could get the information about whether the user disables auto-renew for the subscription.
When users have purchased a subscription add-on, it will renew automatically by default. Detailed messages are listed here: Subscription renewals and grace periods. If users need to check subscription mode, he still need to check it from the account service site.

How to solve the issue like app service will not be call on every time?

I am working on Microsoft band technology, in one of my current scenario as I am sending Sms in background (means even app is not open in foreground also its working like sending Sms successfully).
For that I created the appservice by taking with Windows Runtime Component as a template.
In that app service I wrote the code for how to connect to the band and how to create the tile and how to register the Events.
And also wrote the code in Button_Pressed event for sending the Sms to others.
After that I configured the app service in package. Manifest file like this below figure.
enter image description here
My issue is appservice will not be call on every time.it will be call on two or three times per day.
Is the problem in my code or issue of Microsoft health app?
using Microsoft.Band;
using Microsoft.Band.Notifications;
using Microsoft.Band.Tiles;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
namespace AppServiceEvents
{
public sealed class MyTileEventHandler : IBackgroundTask
{
private BackgroundTaskDeferral backgroundTaskDeferral;
private AppServiceConnection appServiceConnection;
IBandClient bandClient;
//BandTile tile;
//IEnumerable<BandTile> tiles;
//Guid tileGuid;
public void Run(IBackgroundTaskInstance taskInstance)
{
//throw new NotImplementedException();
this.backgroundTaskDeferral = taskInstance.GetDeferral(); taskInstance.Canceled += OnTaskCanceled;
//await ConnectBand();
// Add handlers for tile events
BackgroundTileEventHandler.Instance.TileOpened += EventHandler_TileOpened;
BackgroundTileEventHandler.Instance.TileClosed += EventHandler_TileClosed;
BackgroundTileEventHandler.Instance.TileButtonPressed += EventHandler_TileButtonPressed;
// Set up handler for incoming app service request messages
var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
this.appServiceConnection = details.AppServiceConnection;
this.appServiceConnection.RequestReceived +=OnRequestReceived;
}
private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
//throw new NotImplementedException();
var messageDeferral = args.GetDeferral();
ValueSet response = new ValueSet();
ValueSet request = args.Request.Message;
// Decode the received message and call the appropriate handler
BackgroundTileEventHandler.Instance.HandleTileEvent(request);
// Send the response
await args.Request.SendResponseAsync(response);
messageDeferral.Complete();
}
private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
//throw new NotImplementedException();
DisconnectBand();
if (this.backgroundTaskDeferral != null)
{
this.backgroundTaskDeferral.Complete();
}
}
private void EventHandler_TileOpened(object sender, BandTileEventArgs<IBandTileOpenedEvent> e)
{
// TODO: Handle the tile opening
LogEvent(String.Format("EventHandler_TileOpened: TileId={0} Timestamp={1}", e.TileEvent.TileId, e.TileEvent.Timestamp));
// We create a Band connection when the tile is opened and keep it connected until the tile closes.
ConnectBand();
}
private void EventHandler_TileClosed(object sender, BandTileEventArgs<IBandTileClosedEvent> e)
{
// TODO: Handle the tile closing
LogEvent(String.Format("EventHandler_TileClosed: TileId={0} Timestamp={1}", e.TileEvent.TileId, e.TileEvent.Timestamp));
// Disconnect the Band now that the user has closed the tile.
DisconnectBand();
}
private void EventHandler_TileButtonPressed(object sender, BandTileEventArgs<IBandTileButtonPressedEvent> e)
{
// TODO: Handle the button push
LogEvent(String.Format("EventHandler_TileButtonPressed: TileId={0} PageId={1} ElementId={2}", e.TileEvent.TileId, e.TileEvent.PageId, e.TileEvent.ElementId));
// We should have a Band connection from the tile open event, but in case the OS unloaded our background code
// between that event and this button press event, we restore the connection here as needed.
ConnectBand();
var tileid = e.TileEvent.TileId;
SendMessage(tileid);
//await new MessageDialog("This is an background task"+tileid).ShowAsync();
}
private void ConnectBand()
{
if (this.bandClient == null)
{
// Note that we specify isBackground = true here to avoid conflicting with any foreground app connection to the Band
Task<IBandInfo[]> getBands = BandClientManager.Instance.GetBandsAsync(isBackground: true);
getBands.Wait();
IBandInfo[] pairedBands = getBands.Result;
if (pairedBands.Length == 0)
{
LogEvent("ERROR - No paired Band");
}
try
{
Task<IBandClient> connect = BandClientManager.Instance.ConnectAsync(pairedBands[0]);
connect.Wait();
this.bandClient = connect.Result;
}
catch
{
LogEvent("ERROR - Unable to connect to Band");
}
}
}
/// <summary>
/// If currently connected to the Band, then disconnect.
/// </summary>
private void DisconnectBand()
{
if (bandClient != null)
{
bandClient.Dispose();
bandClient = null;
}
}
const string LogFileName = "EventLog.txt";
/// <summary>
/// Log event strings to a text file
/// </summary>
/// <param name="eventString">String describing the event</param>
private void LogEvent(string eventString)
{
using (FileStream stream = new FileStream("EventLog.txt", FileMode.Append))
{
string outputString = String.Format("{0}: {1}\r\n", DateTime.Now, eventString);
byte[] outputASCII = Encoding.ASCII.GetBytes(outputString);
stream.Write(outputASCII, 0, outputASCII.Length);
}
}
private async void SendMessage(Guid tileGuid)
{
try
{ // Send a message to the Band for one of our tiles,
// and show it as a dialog.
await bandClient.NotificationManager.SendMessageAsync(tileGuid, "Task", "This is an AppService Task", DateTimeOffset.Now, MessageFlags.ShowDialog);
}
catch (BandException ex)
{
// handle a Band connection exception
}
}
}
}
Please tell me how to resolve the above issue and tell me how to debug the appservice in VS 2015 but not a background task.
Regards,
Pradeep

Push sharp not sending notification and No Call backs in Push service

I am using pushsharp to create remote notification to my device. I did all the certficate stuff. My problem is when I try to send notification using push sharp nothing happens here is my code I adopted from Here.
I would atleast expect some of the callbacks to be called like NotificationFailed, ChanelCreated...etc
Here is my code
public class PushNotificationService
{
private static PushNotificationApple _pushNotificationApple;
public static void Main()
{
PushNotificationService ser = new PushNotificationService();
string token = "0eaaf46d379bc5f5529f0b3357e0973ccd4655b163d789d875e3ad5fe64210d9";
ser.SendPushNotification(token, "Hello my push");
System.Console.WriteLine("Test");
}
public PushNotificationService()
{
if (_pushNotificationApple == null)
{
_pushNotificationApple = new PushNotificationApple();
}
}
/// <summary>
/// Send the push notification to the device
/// </summary>
/// <param name="deviceToken">The device token</param>
/// <param name="message">The message</param>
/// <returns>True if the notification is sent</returns>
public bool SendPushNotification(string deviceToken, string message)
{
if (_pushNotificationApple != null)
{
_pushNotificationApple.SendNotification(deviceToken, message);
}
return true;
}
}
Another class
/// <summary>
/// Class for
/// </summary>
public class PushNotificationApple
{
private static PushBroker pushBroker;
/// <summary>
/// The push notification apple
/// </summary>
public PushNotificationApple()
{
if (pushBroker == null)
{
//Create our push services broker
pushBroker = new PushBroker();
//Wire up the events for all the services that the broker registers
pushBroker.OnNotificationSent += NotificationSent;
pushBroker.OnChannelException += ChannelException;
pushBroker.OnServiceException += ServiceException;
pushBroker.OnNotificationFailed += NotificationFailed;
pushBroker.OnNotificationRequeue += NotificationRequeue;
pushBroker.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
pushBroker.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
pushBroker.OnChannelCreated += ChannelCreated;
pushBroker.OnChannelDestroyed += ChannelDestroyed;
//-------------------------
// APPLE NOTIFICATIONS
//-------------------------
//Configure and start Apple APNS
// IMPORTANT: Make sure you use the right Push certificate. Apple allows you to generate one for connecting to Sandbox,
// and one for connecting to Production. You must use the right one, to match the provisioning profile you build your
// app with!
// Make sure you provide the correct path to the certificate, in my case this is how I did it in a WCF service under Azure,
// but in your case this might be different. Putting the .p12 certificate in the main directory of your service
// (in case you have a webservice) is never a good idea, people can download it from there..
//System.Web.Hosting.HostingEnvironment.MapPath("~/folder/file");
var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/PushNotification.p12"));
//var appleCert = File.ReadAllBytes("/Resources/PushNotification.p12");
// TODD revise
// var appleCert = File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/PushSharp.PushCert.Development.p12"));
//IMPORTANT: If you are using a Development provisioning Profile, you must use the Sandbox push notification server
// (so you would leave the first arg in the ctor of ApplePushChannelSettings as 'false')
// If you are using an AdHoc or AppStore provisioning profile, you must use the Production push notification server
// (so you would change the first arg in the ctor of ApplePushChannelSettings to 'true')
pushBroker.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, "")); //Extension method
}
}
private void NotificationRequeue(object sender, PushSharp.Core.NotificationRequeueEventArgs e)
{
Console.WriteLine("Chanel Notification Requeue");
}
public void SendNotification(string deviceToken, string message)
{
//Fluent construction of an iOS notification
//IMPORTANT: For iOS you MUST MUST MUST use your own DeviceToken here that gets generated within your iOS app itself when the Application Delegate
// for registered for remote notifications is called, and the device token is passed back to you
if (pushBroker != null)
{
pushBroker.QueueNotification(new AppleNotification()
.ForDeviceToken(deviceToken)
.WithAlert(message)
.WithBadge(10)
.WithSound("sound.caf"));
}
}
private void ChannelDestroyed(object sender)
{
Console.WriteLine("Chanel Destroyed");
}
private void ChannelCreated(object sender, PushSharp.Core.IPushChannel pushChannel)
{
Console.WriteLine("Chanel created");
}
private void DeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, PushSharp.Core.INotification notification)
{
Console.WriteLine("Device Subscription Changed");
}
private void DeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, PushSharp.Core.INotification notification)
{
Console.WriteLine("Device Subscription Expired");
}
private void NotificationFailed(object sender, PushSharp.Core.INotification notification, Exception error)
{
Console.WriteLine("Notification Failed");
}
private void ServiceException(object sender, Exception error)
{
Console.WriteLine("Service Exception");
}
private void ChannelException(object sender, PushSharp.Core.IPushChannel pushChannel, Exception error)
{
Console.WriteLine("Channel Exception");
}
private void NotificationSent(object sender, PushSharp.Core.INotification notification)
{
Console.WriteLine("Notification Sent");
}
}
This solved my problem:
While creating Production SSL certificate, do not change the name "aps_production.cer".
And before creating Development related certificates, first create certificates(SSL,provisioning,p12) for only production and try.
It really worked for me after trying different approaches. Give it a try.
Adding this line at the end of the method SendNotification() solved my problem.
pushBroker.StopAllServices();

Push Sharp Within Asp.Net Web Service

This is more of a general Asp.Net / .Net lifecycle question.
I'm looking at using PushSharp within a Asp.Net Web Service to send notifications using APNS.
Given the nature of PushSharp using a queue to async send messages and then event callbacks to notify of 'OnNotificationSent' / 'OnServiceException' etc.. how would this work within Asp.net?
The Web Service exposes a method that instantiates PushSharp, registers for the various callback events and queues Notification Messages.
The consumer calls the web service
Once The Web service method returns, does that method continue to receive the event callbacks or is it disposed and the events will not be called?
Thanks
for your help.
Not highly recommended in Asp.net, due to application pool interfering in the process (PushSharp author says notifications in the queue but not get sent). I have implemented this though in an Asp.net website and it works.
I have moved this to a Windows service since.
Global.asax.cs file:
using PushSharp;
using PushSharp.Core;
public class Global : System.Web.HttpApplication
{
private static PushBroker myPushBroker;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
myPushBroker = new PushBroker();
myPushBroker.OnNotificationSent += NotificationSent;
myPushBroker.OnChannelException += ChannelException;
myPushBroker.OnServiceException += ServiceException;
myPushBroker.OnNotificationFailed += NotificationFailed;
myPushBroker.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
myPushBroker.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
myPushBroker.OnChannelCreated += ChannelCreated;
myPushBroker.OnChannelDestroyed += ChannelDestroyed;
HttpContext.Current.Application["MyPushBroker"] = myPushBroker;
}
//IMPLEMENT PUSHBROKER DELEGATES HERE
}
aspx.cs file (example Notifications.aspx.cs):
using PushSharp;
using PushSharp.Apple;
using PushSharp.Core;
public partial class Notifications : System.Web.UI.Page {
private PushBroker myPushBroker = HttpContext.Current.Application["MyPushBroker"] as PushBroker;
//SO I CAN SWITCH FROM DEVELOPMENT TO PRODUCTION EASILY I SET THIS IN THE DATABASE
private string pushCertificate = "";
private string certPass = "";
private bool isProduction = false;
protected void btnSendNotification_Click(object sender, EventArgs e)
{
bool hasError = false;
lblError.Text = "";
if (!string.IsNullOrEmpty(txtMessage.Text))
{
try
{
GetCertificate();
//GET DEVICE TOKENS TO SEND MESSAGES TO
//NOT THE BEST WAY TO SEND MESSAGES IF YOU HAVE HUNDREDS IF NOT THOUSANDS OF TOKENS. THAT'S WHY A WINDOWS SERVICE IS RECOMMENDED.
string storedProcUser = "sp_Token_GetAll";
string userTableName = "User_Table";
DataSet dsUser = new DataSet();
UserID = new Guid(ID.Text);
dsUser = srvData.GetDeviceToken(UserID, storedProcUser, userTableName, dataConn);
DataTable userTable = new DataTable();
userTable = dsUser.Tables[0];
if (userTable.Rows.Count != 0)
{
string p12FileName = Server.MapPath(pushCertificate); //SET IN THE GET CERTIFICATE
var appleCert = File.ReadAllBytes(p12FileName);
string p12Password = certPass;
//REGISTER SERVICE
myPushBroker.RegisterAppleService(new ApplePushChannelSettings(isProduction, appleCert, p12Password));
DataRow[] drDataRow;
drDataRow = userTable.Select();
string savedDeviceToken = "";
for (int i = 0; i < userTable.Rows.Count; i++)
{
if (drDataRow[i]["DeviceToken"] is DBNull == false)
{
savedDeviceToken = drDataRow[i]["DeviceToken"].ToString();
myPushBroker.QueueNotification(new AppleNotification()
.ForDeviceToken(savedDeviceToken)
.WithAlert(txtMessage.Text)
.WithBadge(1)
.WithSound("sound.caf"));
//NOTHING TO DO ANYMORE. CAPTURE IN THE PUSH NOTIFICATION DELEGATE OF GLOBAL ASCX FILE WHAT HAPPENED TO THE SENT MESSAGE.
}
}
}
}
catch(Exception ex)
{
}
finally
{
}
}
}
}
Check out EasyServices it allows you to easily push notifications to various push servers using PushSharp without having to take care of un-received notifications even when using ASP.NET
var _pushNotificationService = EngineContext.Current.Resolve<IPushNotificationService>();
_pushNotificationService.InsertNotification(NotificationType type, string title, string message, int subscriberId, PushPriority Priority = PushPriority.Normal);
https://easyservices.codeplex.com

Is there a way to catch exceptions using System.Net.Mail.SendAsync()

I have a couple of methods already to send an email message syncronously.
If an email fails, I use this fairly standard code:
static void CheckExceptionAndResend(SmtpFailedRecipientsException ex, SmtpClient client, MailMessage message)
{
for (int i = 0; i < ex.InnerExceptions.Length -1; i++)
{
var status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable ||
status == SmtpStatusCode.TransactionFailed)
{
System.Threading.Thread.Sleep(3000);
client.Send(message);
}
}
}
However, I'm trying to achieve the same using SendAsync(). This is the code I have so far:
public static void SendAsync(this MailMessage message)
{
message.ThrowNull("message");
var client = new SmtpClient();
// Set the methods that is called once the event ends
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// Unique identifier for this send operation
string userState = Guid.NewGuid().ToString();
client.SendAsync(message, userState);
// Clean up
message.Dispose();
}
static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this operation.
String token = (string)e.UserState;
if (e.Error.IsNotNull())
{
// Do somtheing
}
}
The question is using token and/or e.Error how do I get the exception so that I can make the necessary checks for StatusCode and then resend?
I've been Googling all afternoon but havn't found anything positive.
Any advice appreciated.
e.Error already has the Exception that occurred while sending the email async. You can check the Exception.Message, Exception.InnerException, Exception.StackTrace, etc. to get further details.
Update:
Check if the Exception is of type SmtpException and if it is, you can query the StatusCode. Something like
if(e.Exception is SmtpException)
{
SmtpStatusCode code = ((SmtpException)(e.Exception)).StatusCode;
//and go from here...
}
And check here for further details.

Resources