How to allow the user only if he/she type the “ivr” or “IVR” using Form Flow concept - botframework

I am working on bot framework technology, in one of my current project I want to allow the user only if he/she type the ‘’ivr” or “IVR” otherwise it shows some feedback to the user.
For that I have wrote below lines of code, but this code shows some wrong output to the user. Even if the user enter ivr or IVR it shows feedback to the user for the first time, but from second time onwards its working correctly.
[Serializable]
class Customer
{
//Create Account Template
[Prompt("Please send any of these commands like **IVR** (or) **ivr**.")]
public string StartingWord;
public static IForm<Customer> BuildForm()
{
OnCompletionAsyncDelegate<Customer> accountStatus = async (context, state) =>
{
await Task.Delay(TimeSpan.FromSeconds(5));
await context.PostAsync("We are currently processing your account details. We will message you the status.");
};
var builder = new FormBuilder<Customer>();
return builder
//.Message("Welcome to the BankIVR bot! To start an conversation with this bot send **ivr** or **IVR** command.\r \n if you need help, send the **Help** command")
.Field(nameof(Customer.StartingWord), validate: async (state, response) =>
{
var result = new ValidateResult { IsValid = true, Value = response };
string str = (response as string);
if (str.ToLower() != "ivr")
{
result.Feedback = "I'm sorry. I didn't understand you.";
result.IsValid = false;
return result;
}
else if (str.ToLower() == "ivr")
{
result.IsValid = true;
return result;
}
else
{
return result;
}
})
.OnCompletion(accountStatus)
.Build();
}
};
Please tell me how to resolve this issue using Form Flow concept.
-Pradeep

Your code looks correct to me - I can only suggest you debug your code with a step-through debugger and see where the logic tests are failing.
That said, if it's not working for people in Turkey, it's because you shouldn't use .ToLower() for normalizing text, for example the .ToLower() method does not work for text that contains the Turkish dotless 'I' character: http://archives.miloush.net/michkap/archive/2004/12/02/273619.html
Also, your else case will never be hit because your two prior checks (!= and ==) cover every possible case (the C# compiler is currently not sophisticated enough to flag the else case as unreachable code).
The correct way to do a case-insensitive comparison is with String.Equals:
if( "ivr".Equals( str, StringComparison.InvariantCultureIgnoreCase ) ) {
result.IsValid = true;
return result;
}
else {
result.Feedback = "I'm sorry. I didn't understand you.";
result.IsValid = false;
}

Finally, I got the result with out any issue.
here is my updated code for to allow only the user enter "ivr or IVR" word, to start a form flow conversation with bot.
.Field(nameof(Customer.StartingWord), validate: async (state, response) =>
{
var result = new ValidateResult { IsValid = true, Value = response };
string str = (response as string);
if ("ivr".Equals(str, StringComparison.InvariantCultureIgnoreCase))
{
//result.IsValid = true;
//return result;
}
else
{
result.Feedback = "I'm sorry. I didn't understand you.";
result.IsValid = false;
//return result;
}
return result;
})
-Pradeep

Related

Null response and cross session issue in chat bot

I am using the bot framework of V4. using an API call, I'm trying to get data and display it to the user and I defined a custom method to capture data from API and preprocess it, before sending it to the user through waterfall dialog and I made this method async and also using await where it is being called.
There are 2 scenarios where I'm facing the issue -
When two users send questions at an instance one of the responses captured as null instead of value acquired from API.
We are using the suggestive card to display result and when the user clicks on button cross-session was observed sporadically.
Help in this area is much appreciated.
The custom method defined to make API call and get data:
public static async Task<string>
GetEPcallsDoneAsync(ConversationData conversationData)
{
LogWriter.LogWrite("Info: Acessing end-points");
string responseMessage = null;
try
{
conversationData.fulFillmentMap = await
AnchorUtil.GetFulfillmentAsync(xxxxx);//response from API call to get data
if (conversationData.fulFillmentMap == null || (conversationData.fulFillmentMap.ContainsKey("status") && conversationData.fulFillmentMap["status"].ToString() != "200"))
{
responseMessage = "Sorry, something went wrong. Please try again later!";
}
else
{
conversationData.NLGresultMap = await
AnchorUtil.GetNLGAsync(conversationData.fulFillmentMap ,xxxx);//API call to get response to be displayed
if (conversationData.errorCaptureDict.ContainsKey("fulfillmentError") || conversationData.NLGresultMap.ContainsKey("NLGError"))
{
responseMessage = "Sorry, something went wrong:( Please try again later!!!";
}
else
{
responseMessage = FormatDataResponse(conversationData.NLGresultMap["REPLY"].ToString()); //response message
}
}
return responseMessage;
}
catch (HttpRequestException e)
{
LogWriter.LogWrite("Error: " + e.Message);
System.Console.WriteLine("Error: " + e.Message);
return null;
}
}
And the waterfall step of dialog class where the above function is being called:
private async Task<DialogTurnResult> DoProcessInvocationStep(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
conversationData.index = 0; //var for some other purpose
conversationData.result = await
AnchorUtil.GetEPcallsDoneAsync(conversationData);
await _conversationStateAccessor.SetAsync(stepContext.Context, conversationData, cancellationToken);
return await stepContext.NextAsync(cancellationToken);
}
ConversationData contains variables that are required to process data through waterfall dialogs, values to the object has been set and accessed through accessor as below in each step:
In a dialog class,
public class TopLevelDialog : ComponentDialog
{
private readonly IStatePropertyAccessor<ConversationData> _conversationStateAccessor;
ConversationData conversationData;
public TopLevelDialog(ConversationState conversationState)
: base(nameof(TopLevelDialog))
{
_conversationStateAccessor = conversationState.CreateProperty<ConversationData>(nameof(ConversationData));
AddDialog(new TextPrompt(nameof(TextPrompt)));
AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
AddDialog(new ReviewSelectionDialog(conversationState));
AddDialog(new ESSelectionDialog());
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
StartSelectionStepAsync,
GetESResultStep,
DoProcessInvocationStep,
ResultStepAsync,
IterationStepAsync
}));
InitialDialogId = nameof(WaterfallDialog);
}
private async Task<DialogTurnResult> StartSelectionStepAsync (WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
conversationData = await _conversationStateAccessor.GetAsync(stepContext.Context, () => new ConversationData());
//code for functionality
await _conversationStateAccessor.SetAsync(stepContext.Context, conversationData, cancellationToken);
return await stepContext.NextAsync(null, cancellationToken);
}
//other dialog steps
}
Both of your issues likely stem from the same thing. You can't declare conversationData as a class-level property. You'll run into concurrency issues like this as every user will overwrite the conversationData for every other user. You must re-declare conversationData in each step function.
For example,
User A starts the waterfall dialog and gets half of the way through. conversationData is correct at this point and represents exactly what it should.
Now User B starts a dialog. At the StartSelectionStepAsync, they just reset conversationData for everybody because of conversationData = await _conversationStateAccessor.GetAsync(stepContext.Context, () => new ConversationData()); and all users share the same conversationData because of ConversationData conversationData;.
So now, when User A continues their conversation, conversationData will be null/empty.
How State Should Be Saved
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { Channels, MessageFactory } = require('botbuilder');
const {
AttachmentPrompt,
ChoiceFactory,
ChoicePrompt,
ComponentDialog,
ConfirmPrompt,
DialogSet,
DialogTurnStatus,
NumberPrompt,
TextPrompt,
WaterfallDialog
} = require('botbuilder-dialogs');
const { UserProfile } = require('../userProfile');
const ATTACHMENT_PROMPT = 'ATTACHMENT_PROMPT';
const CHOICE_PROMPT = 'CHOICE_PROMPT';
const CONFIRM_PROMPT = 'CONFIRM_PROMPT';
const NAME_PROMPT = 'NAME_PROMPT';
const NUMBER_PROMPT = 'NUMBER_PROMPT';
const USER_PROFILE = 'USER_PROFILE';
const WATERFALL_DIALOG = 'WATERFALL_DIALOG';
/**
* This is a "normal" dialog, where userState is stored properly using the accessor, this.userProfile.
* In this dialog example, we create the userProfile using the accessor in the first step, transportStep.
* We then pass prompt results through the remaining steps using step.values.
* In the final step, summaryStep, we save the userProfile using the accessor.
*/
class UserProfileDialogNormal extends ComponentDialog {
constructor(userState) {
super('userProfileDialogNormal');
this.userProfileAccessor = userState.createProperty(USER_PROFILE);
this.addDialog(new TextPrompt(NAME_PROMPT));
this.addDialog(new ChoicePrompt(CHOICE_PROMPT));
this.addDialog(new ConfirmPrompt(CONFIRM_PROMPT));
this.addDialog(new NumberPrompt(NUMBER_PROMPT, this.agePromptValidator));
this.addDialog(new AttachmentPrompt(ATTACHMENT_PROMPT, this.picturePromptValidator));
this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
this.transportStep.bind(this),
this.nameStep.bind(this),
this.nameConfirmStep.bind(this),
this.ageStep.bind(this),
this.pictureStep.bind(this),
this.confirmStep.bind(this),
this.saveStep.bind(this)
]));
this.initialDialogId = WATERFALL_DIALOG;
}
/**
* The run method handles the incoming activity (in the form of a TurnContext) and passes it through the dialog system.
* If no dialog is active, it will start the default dialog.
* #param {*} turnContext
* #param {*} accessor
*/
async run(turnContext, accessor) {
const dialogSet = new DialogSet(accessor);
dialogSet.add(this);
const dialogContext = await dialogSet.createContext(turnContext);
const results = await dialogContext.continueDialog();
if (results.status === DialogTurnStatus.empty) {
await dialogContext.beginDialog(this.id);
}
}
async transportStep(step) {
// Get the userProfile if it exists, or create a new one if it doesn't.
const userProfile = await this.userProfileAccessor.get(step.context, new UserProfile());
// Pass the userProfile through step.values.
// This makes it so we don't have to call this.userProfileAccessor.get() in every step.
step.values.userProfile = userProfile;
// Skip this step if we already have the user's transport.
if (userProfile.transport) {
// ChoicePrompt results will show in the next step with step.result.value.
// Since we don't need to prompt, we can pass the ChoicePrompt result manually.
return await step.next({ value: userProfile.transport });
}
// WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
// Running a prompt here means the next WaterfallStep will be run when the user's response is received.
return await step.prompt(CHOICE_PROMPT, {
prompt: 'Please enter your mode of transport.',
choices: ChoiceFactory.toChoices(['Car', 'Bus', 'Bicycle'])
});
}
async nameStep(step) {
// Retrieve the userProfile from step.values.
const userProfile = step.values.userProfile;
// Set the transport property of the userProfile.
userProfile.transport = step.result.value;
// Pass the userProfile through step.values.
// This makes it so we don't have to call this.userProfileAccessor.get() in every step.
step.values.userProfile = userProfile;
// Skip the prompt if we already have the user's name.
if (userProfile.name) {
// We pass in a skipped bool so we know whether or not to send messages in the next step.
return await step.next({ value: userProfile.name, skipped: true });
}
return await step.prompt(NAME_PROMPT, 'Please enter your name.');
}
async nameConfirmStep(step) {
// Retrieve the userProfile from step.values and set the name property
const userProfile = step.values.userProfile;
// If userState is working correctly, we'll have userProfile.transport from the previous step.
if (!userProfile || !userProfile.transport) {
throw new Error(`transport property does not exist in userProfile.\nuserProfile:\n ${ JSON.stringify(userProfile) }`);
}
// Text prompt results normally end up in step.result, but if we skipped the prompt, it will be in step.result.value.
userProfile.name = step.result.value || step.result;
// step.values.userProfile.name is already set by reference, so there's no need to set it again to pass it to the next step.
// We can send messages to the user at any point in the WaterfallStep. Only do this if we didn't skip the prompt.
if (!step.result.skipped) {
await step.context.sendActivity(`Thanks ${ step.result }.`);
}
// WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
// Skip the prompt if we already have the user's age.
if (userProfile.age) {
return await step.next('yes');
}
return await step.prompt(CONFIRM_PROMPT, 'Do you want to give your age?', ['yes', 'no']);
}
async ageStep(step) {
// Retrieve the userProfile from step.values
const userProfile = step.values.userProfile;
// If userState is working correctly, we'll have userProfile.name from the previous step.
if (!userProfile || !userProfile.name) {
throw new Error(`name property does not exist in userProfile.\nuserProfile:\n ${ JSON.stringify(userProfile) }`);
}
// Skip the prompt if we already have the user's age.
if (userProfile.age) {
// We pass in a skipped bool so we know whether or not to send messages in the next step.
return await step.next({ value: userProfile.age, skipped: true });
}
if (step.result) {
// User said "yes" so we will be prompting for the age.
// WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
const promptOptions = { prompt: 'Please enter your age.', retryPrompt: 'The value entered must be greater than 0 and less than 150.' };
return await step.prompt(NUMBER_PROMPT, promptOptions);
} else {
// User said "no" so we will skip the next step. Give -1 as the age.
return await step.next(-1);
}
}
async pictureStep(step) {
// Retrieve the userProfile from step.values and set the age property
const userProfile = step.values.userProfile;
// We didn't set any additional properties on userProfile in the previous step, so no need to check for them here.
// Confirm prompt results normally end up in step.result, but if we skipped the prompt, it will be in step.result.value.
userProfile.age = step.result.value || step.result;
// step.values.userProfile.age is already set by reference, so there's no need to set it again to pass it to the next step.
if (!step.result.skipped) {
const msg = userProfile.age === -1 ? 'No age given.' : `I have your age as ${ userProfile.age }.`;
// We can send messages to the user at any point in the WaterfallStep. Only send it if we didn't skip the prompt.
await step.context.sendActivity(msg);
}
// Skip the prompt if we already have the user's picture.
if (userProfile.picture) {
return await step.next(userProfile.picture);
}
if (step.context.activity.channelId === Channels.msteams) {
// This attachment prompt example is not designed to work for Teams attachments, so skip it in this case
await step.context.sendActivity('Skipping attachment prompt in Teams channel...');
return await step.next(undefined);
} else {
// WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
var promptOptions = {
prompt: 'Please attach a profile picture (or type any message to skip).',
retryPrompt: 'The attachment must be a jpeg/png image file.'
};
return await step.prompt(ATTACHMENT_PROMPT, promptOptions);
}
}
async confirmStep(step) {
// Retrieve the userProfile from step.values and set the picture property
const userProfile = step.values.userProfile;
// If userState is working correctly, we'll have userProfile.age from the previous step.
if (!userProfile || !userProfile.age) {
throw new Error(`age property does not exist in userProfile.\nuserProfile:\n ${ JSON.stringify(userProfile) }`);
}
userProfile.picture = (step.result && typeof step.result === 'object' && step.result[0]) || 'no picture provided';
// step.values.userProfile.picture is already set by reference, so there's no need to set it again to pass it to the next step.
let msg = `I have your mode of transport as ${ userProfile.transport } and your name as ${ userProfile.name }`;
if (userProfile.age !== -1) {
msg += ` and your age as ${ userProfile.age }`;
}
msg += '.';
await step.context.sendActivity(msg);
if (userProfile.picture && userProfile.picture !== 'no picture provided') {
try {
await step.context.sendActivity(MessageFactory.attachment(userProfile.picture, 'This is your profile picture.'));
} catch (err) {
await step.context.sendActivity('A profile picture was saved but could not be displayed here.');
}
}
// WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
return await step.prompt(CONFIRM_PROMPT, { prompt: 'Would you like me to save this information?' });
}
async saveStep(step) {
if (step.result) {
// Get the current profile object from user state.
const userProfile = step.values.userProfile;
// Save the userProfile to userState.
await this.userProfileAccessor.set(step.context, userProfile);
await step.context.sendActivity('User Profile Saved.');
} else {
// Ensure the userProfile is cleared
await this.userProfileAccessor.set(step.context, {});
await step.context.sendActivity('Thanks. Your profile will not be kept.');
}
// WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is the end.
return await step.endDialog();
}
async agePromptValidator(promptContext) {
// This condition is our validation rule. You can also change the value at this point.
return promptContext.recognized.succeeded && promptContext.recognized.value > 0 && promptContext.recognized.value < 150;
}
async picturePromptValidator(promptContext) {
if (promptContext.recognized.succeeded) {
var attachments = promptContext.recognized.value;
var validImages = [];
attachments.forEach(attachment => {
if (attachment.contentType === 'image/jpeg' || attachment.contentType === 'image/png') {
validImages.push(attachment);
}
});
promptContext.recognized.value = validImages;
// If none of the attachments are valid images, the retry prompt should be sent.
return !!validImages.length;
} else {
await promptContext.context.sendActivity('No attachments received. Proceeding without a profile picture...');
// We can return true from a validator function even if Recognized.Succeeded is false.
return true;
}
}
}
module.exports.UserProfileDialogNormal = UserProfileDialogNormal;

`Feedback` text not shown in bot conversation

My formflow dialog contains a field with custom validation applied...
var form = builder
.Field(new FieldReflector<CarValuationDialog>(nameof(UserName))
.SetActive(state => string.IsNullOrEmpty(state.UserName)))
.Field(new FieldReflector<CarValuationDialog>(nameof(ValuationOption))
.SetPrompt(new PromptAttribute($"Hello {{UserName}}.<br /><br />Are you looking to get an price estimate for a car you’re selling, or for a car you’re looking to buy? {{||}}")))
.Field(new FieldReflector<CarValuationDialog>(nameof(RegistrationNumber))
.SetDefine(RegistrationNumberDefinitionMethod))
.Field(new FieldReflector<CarValuationDialog>(nameof(Mileage))
.SetValidate(async (state, value) =>
{
var result = new ValidateResult { IsValid = true, Value = value };
if (int.TryParse(value.ToString(), out int mileage))
{
result.IsValid = true;
}
else
{
result.Feedback = "That isn't valid number. Can you enter it again please?";
result.IsValid = false;
}
return await Task.FromResult(result);
}))
.Field(nameof(PreviousOwnerOption),
active: carValuationDialog => carValuationDialog.ValuationOption == ValuationOptions.LookingToSell)
.Field(nameof(ServiceHistoryOption),
active: carValuationDialog => carValuationDialog.ValuationOption == ValuationOptions.LookingToSell)
.Confirm(Confirmation)
.OnCompletion(GetValuationAndDisplaySummaryToUser);
When SetValidate() executes, and the given mileage value is not an int, the result.Feedback = ... line is executed. However, instead of seeing the message "That isn't a valid mileage value. Can you enter it again please?" I see this...
Full discolsure, I do have the following attribute declared on the dialog class
[Template(TemplateUsage.NotUnderstood, "Sorry, I don't understand '{0}'.")]
but I thought the result.Feedback text would be shown to the user in this case? Not using the Template[] results in the following text displayed when the user enters an invalid mileage...
"d" is not a car mileage option.
Which looks rubbish, and is why I used the Template[] override.
EDIT
I was able to accomplish this by applying templates to the fields themselves for validation. This should be the exact behavior you are looking for. Note that I did not do extensive testing, I just wanted to get the general idea working. Heres the code I used:
[Serializable]
public class SandwichOrder
{
//public PreviousOwnerOptions Options;
[Template(TemplateUsage.NotUnderstood, "Sorry, I don't understand '{0}'.")]
[Prompt("What is the {&}?")]
public int Mileage;
[Template(TemplateUsage.NotUnderstood, "Sorry, This is not valid '{0}'.")]
[Prompt("How many {&}?")]
public int Days;
public static IForm<SandwichOrder> BuildForm()
{
var form = new FormBuilder<SandwichOrder>()
.Field(new FieldReflector<SandwichOrder>(nameof(Mileage))
.SetValidate(async (state, value) =>
{
var result = new ValidateResult {IsValid = true, Value = value};
if (int.TryParse(value.ToString(), out int mileage))
{
result.IsValid = true;
}
else
{
result.IsValid = false;
}
return await Task.FromResult(result);
}))
.Field(new FieldReflector<SandwichOrder>(nameof(Days))
.SetValidate(async (state, value) =>
{
var result = new ValidateResult { IsValid = true, Value = value };
if (int.TryParse(value.ToString(), out int days))
{
result.IsValid = true;
}
else
{
result.IsValid = false;
}
return await Task.FromResult(result);
}));
return form.Build();
}
}
This produced this result:

cancel taskcompletionsource which calls a void method from an API with timeout xamarin forms

I have this non-async Task> which just requests:
TaskCompletionSource<ObservableCollection<ItemDto>> tcs = new TaskCompletionSource<ObservableCollection<ItemDto>>();
ObservableCollection<ItemDto> results = new ObservableCollection<ItemDto>();
try
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.OpenTimeout = new TimeSpan(0, 0, 30);
binding.CloseTimeout = new TimeSpan(0, 0, 30);
binding.SendTimeout = new TimeSpan(0, 0, 30);
binding.ReceiveTimeout = new TimeSpan(0, 0, 30);
MobileClient clientMobile = new MobileClient(binding, new EndpointAddress(_endpointUrl));
clientMobile.FindItemsCompleted += (object sender, FindItemsCompletedEventArgs e) =>
{
if (e.Error != null)
{
_error = e.Error.Message;
tcs.TrySetException(e.Error);
}
else if (e.Cancelled)
{
_error = "Cancelled";
tcs.TrySetCanceled();
}
if (string.IsNullOrWhiteSpace(_error) && e.Result.Count() > 0)
{
results = SetItemList(e.Result);
tcs.TrySetResult(results);
}
clientMobile.CloseAsync();
};
clientMobile.FindItemsAsync(SetSearchParam(searchString, 100));
}
catch (Exception)
{
results = new ObservableCollection<ItemDto>();
tcs.TrySetResult(results);
}
return tcs.Task;
Yes, I know, nothing special, it's just that this
clientMobile.FindItemsAsync(SetSearchParam(searchString, 100))
is a call to a void method, which in turn calls another void method which sets a few params in order to then call an async method which itself calls an async method which performs an async operation to return the list of Items.
Problem is, I have no control whatsoever of anything beyond the scope of this Task above, because everything I just explained is part of an API, in which I'm not allowed to touch, and of which I can make no comment, regarding the way it works, as the policy is for me to adapt my work to it... -_-
So, in order to do that, I must kill this call to the FindItemsAsync, as soon as a total of 1 minute has passed... I tried setting the above timespans to a minute each (first worked, now some changes have been made and no go), I tried reducing to half the time, and no go...
Here's the code which is calling this Task:
public void LoadItemList(string searchString)
{
_itemList = new ObservableCollection<ItemDto>();
// Calls the Task LoadList.
var result = LoadList(searchString).Result;
if (result != null && result != new ObservableCollection<ItemDto>())
{
_itemList = result;
}
else
{
_isTaskCompleted = false;
}
_isListEmpty = (_itemList != new ObservableCollection<ItemDto>()) ? false : true;
}
and below is the code which calls the caller of this task... (what a mess -_-):
void Init(string searchString = "")
{
Device.BeginInvokeOnMainThread(async () =>
{
if (!LoadingStackLayout.IsVisible && !LoadingActivityIndicator.IsRunning)
{
ToggleDisplayLoadingListView(true);
}
await Task.Run(() => _listVM.LoadItemList(searchString));
ToggleDisplayLoadingListView();
if (!string.IsNullOrWhiteSpace(_listVM.Error))
{
await DisplayAlert("Error", _listVM.Error, "OK");
}
else if (_listVM.AdList != null && !_listVM.IsListEmpty)
{
ItemListView.IsVisible = true;
ItemListView.ItemsSource = _listVM.ItemList;
}
else if (!_listVM.IsTaskCompleted || _listVM.IsListEmpty)
{
await DisplayAlert("", "At the moment it is not possible to show results for your search.", "OK");
}
else if (_listVM.ItemList.Count == 0)
{
await DisplayAlert("", "At the moment there are no results for your search.", "OK");
}
});
}
At the moment I'm trying to implement the MVVM arch...
Really, thank you so much for your help on this matter, it's been great, and I really apologize for all this inconvenience...
EDIT
Sorry because I didn't explain my objective clearly; it is: I need to fetch a list of Items accessing an API that just communicates with me via a void method FindItemsAsync. I have 60 seconds to fetch all those items. If something goes wrong, or if timeout, I have to cancel the process and inform the user something went wrong.
That doesn't happen. It never cancels. Either gets me the Items, or stays loading forever, dispite my hardest tries... I'm new to tasks and most of this stuff, hence my constant issues...
You can call CloseAsync when your cancellation token expires.
//Creates an object which cancels itself after 5000 ms
var cancel = new CancellationTokenSource(5000);
//Give "cancel.Token" as a submethod parameter
public void SomeMethod(CancellationToken cancelToken)
{
...
//Then use the CancellationToken to force close the connection once you created it
cancelToken.Register(()=> clientMobile.CloseAsync());
}
It will cut down the connection.

how can i show custom exception next to textbox?

I have register form. I want to check new username that is in db or not and if there is in DB , exception show next to it's textbox "UserName already exist...", what should I do?
this my method with exception that I have used it in Register action.:
public void InsertNewUser(MemberRegisterModel mm)
{
EShopThemeDBEntities context = new EShopThemeDBEntities(idbconnection.ConnStr);
using (context)
{
var listUsers = (from o in context.Users
select o.Username).ToList();
var a = listUsers.Count();
foreach (var item in listUsers)
{
if (mm.Username == item.ToString())
{
throw new Exception("UserName already exist...");
}
User mmr = new User();
mmr.FName = mm.FName;
mmr.LName = mm.LName;
mmr.Username = mm.Username;
mmr.Password = mm.Password;
mmr.Email = mm.Email;
mmr.Phone = mm.Phone;
mmr.Mobile = mm.Mobile;
mmr.CreateDate = DateTime.Now;
mmr.RoleId = 2;
context.AddToUsers(mmr);
context.SaveChanges();
}
}
You can set the Model error and return the model object back to view.
if(mm.Username == item.ToString())
{
ModelState.AddModelError("UserName","Username already taken";)
return View(model);
}
Also You do not need to get a list of usrs from database and do a loop to check whether the user entered user name exist or not. You can use the FirstOrDefault method to atleast one is there.
using (context)
{
var user=(from o in context.Users
where o.UserName==mm.UserName).FirstOrDefault();
if(user!=null)
{
ModelState.AddModelError("UserName","Username already taken";)
return View(model);
}
else
{
//Save new user info
}
}
Make sure you have the validation fields in your view, adjacent to the text box
#Html.TextBoxFor(m => m.UserName)
#Html.ValidationMessageFor(m => m.UserName)
But, Ideally, I would also do it asynchronosly with ajax to provide a rich user experience to the user. For that what you have to do is to look for the blur event of the text box and get the value of the textbox, make an ajax call to an action method which checks the availability of user name and return appropriate result.
<script type="text/javascript">
$(function(){
$("#UserName").blur(){
var userName=$(this).val();
$.getJSON("#Url.Action("Check","User")/"+userName,function(response){
if(response.status=="Available")
{
//It is available to register. May be show a green signal in UI
}
else
{
//not available. Show the message to user
$("#someMsgDIv").html("User name not available");
}
});
});
});
</script>
Now we should have an action method called Check in UserController to handle the ajax request
public ActionResult Check(string id)
{
bool isAvailable=false;
string userName=id;
//Check the user name is availabe here
if(isAvailable)
return Json(new { status="Available"},
JsonRequestBehaviour.AllowGet);
else
return Json(new { status="Not Available"},
JsonRequestBehaviour.AllowGet);
}
Note: Never do the client side approach only. Always do the server side checking no matter whether you have client side checking or not.
Shyju's answer is a thorough answer. However, based on your comments about handling the exception, here's a sample:
public void InsertNewUser(MemberRegisterModel mm)
{
// Some code...
if (userExists)
{
throw new ArgumentException("User name not available");
}
}
in your action method:
public ActionResult AddUser(MemberRegisterModel newUser)
{
try
{
var userManager = new MembersSrv();
userManager.InsertNewUser(newUser);
}
catch (ArgumentException ex)
{
if (ex.Message == "User name not available")
{
ModelState.AddModelError("UserName","Username already taken";)
return View(model);
}
}
}
Please note that the better way is to define a class which derives from Exception class (e.g. DuplicateUserNameException) and throw/catch that exception in your code. This sample code has been simplified.

setting HttpContext.Current.User

I am developing an asp.net mvc 3.0 application which has a simple authentication process. User fills a form which is sent to server by ajax call and gets response, but the problem here is that using the following method :
FormsAuthentication.SetAuthCookie(person.LoginName,false);
is not enough to fill 'HttpContext.Current.User' and it needs the below method to be run :
FormsAuthentication.RedirectFromLoginPage("...");
Problem here is that as i mentioned, the loggin form uses an ajax form, and get responses with json, so redirecting is not possible.
How could I fill 'HttpContext.Current.User' ?
Thanks.
Update :
Here is register method :
[HttpPost]
public ActionResult Register(Person person)
{
var q = da.Persons.Where(x => x.LoginName == person.LoginName.ToLower()).FirstOrDefault();
if (q != null)
{
ModelState.AddModelError("", "Username is repettive, try other one");
return Json(new object[] { false, this.RenderPartialViewToString("RegisterControl", person) });
}
else
{
if (person.LoginName.ToLower() == "admin")
{
person.IsAdmin = true;
person.IsActive = true;
}
da.Persons.Add(person);
da.SaveChanges();
FormsAuthentication.SetAuthCookie(person.LoginName,false);
return Json(new object[] { true, "You have registered successfully!" });
}
}
FormsAuthentication doesn't support immediate setting of user's identity, but you should be able to fake it by something like this:
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
new System.Security.Principal.GenericIdentity(person.LoginName),
new string[] { /* fill roles if any */ } );
Here is the version I ended up using, which is based on the answer by #AdamTuliper-MSFT. It is only meant to be used right after logging in, but before redirect, to allow other code to access HttpContext.User.
Don't do anything if already authenticated
Doesn't modify the cookie, since this should only be used for the lifetime of this request
Shorten some things, and a little safer with userdata (should never be null, but...)
Call this after you call SetAuthCookie(), like below:
// in login function
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
AuthenticateThisRequest();
private void AuthenticateThisRequest()
{
//NOTE: if the user is already logged in (e.g. under a different user account)
// then this will NOT reset the identity information. Be aware of this if
// you allow already-logged in users to "re-login" as different accounts
// without first logging out.
if (HttpContext.User.Identity.IsAuthenticated) return;
var name = FormsAuthentication.FormsCookieName;
var cookie = Response.Cookies[name];
if (cookie != null)
{
var ticket = FormsAuthentication.Decrypt(cookie.Value);
if (ticket != null && !ticket.Expired)
{
string[] roles = (ticket.UserData as string ?? "").Split(',');
HttpContext.User = new GenericPrincipal(new FormsIdentity(ticket), roles);
}
}
}
Edit: Remove call to Request.Cookies, as #AdamTuplier-MSFT mentioned.
You need to manually set it. Rather than reinventing the wheel, note the section here on updating the current principal for the request - thats your option here.
How to set Request.IsAuthenticated to true when not using FormsAuthentication.RedirectFromLoginPage?
public void RenewCurrentUser()
{
System.Web.HttpCookie authCookie =
System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = null;
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null && !authTicket.Expired)
{
FormsAuthenticationTicket newAuthTicket = authTicket;
if (FormsAuthentication.SlidingExpiration)
{
newAuthTicket = FormsAuthentication.RenewTicketIfOld(authTicket);
}
string userData = newAuthTicket.UserData;
string[] roles = userData.Split(',');
System.Web.HttpContext.Current.User =
new System.Security.Principal.GenericPrincipal(new FormsIdentity(newAuthTicket), roles);
}
}
}

Resources