Inserting Image into Azure blob -{"Error: Internal Server Error"} MobileInvalidOperationException - azure

i am trying to insert an image from an Win 8 App into Azure blob .I am running into an 500 exception when i try to do this . This is the class i am using -
private MobileServiceCollection<TodoItem, TodoItem> items;
private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>();
[DataContract]
public class TodoItem
{
[DataMember(Name = "id")]
public int ID { get; set; }
[DataMember(Name = "text")]
public string Text { get; set; }
[DataMember(Name = "containerName")]
public string ContainerName { get; set; }
[DataMember(Name = "resourceName")]
public string ResourceName { get; set; }
[DataMember(Name = "sasQueryString")]
public string SasQueryString { get; set; }
[DataMember(Name = "imageUri")]
public string ImageUri { get; set; }
}
The Exception is thrown at line -await todoTable.InsertAsync(todoItem);
At this time the exception is thrown the value of SASQueryString and ImageUri is NULL.
private async void OnTakePhotoClick(object sender, RoutedEventArgs e)
{
// Capture a new photo or video from the device.
CameraCaptureUI cameraCapture = new CameraCaptureUI();
media = await cameraCapture
.CaptureFileAsync(CameraCaptureUIMode.PhotoOrVideo);
TodoItem todoitem = new TodoItem { Text="NA",ContainerName="todoitemimages"};
InsertTodoItem(todoitem);
}
private async void InsertTodoItem(TodoItem todoItem)
{
string errorString = string.Empty;
if (media != null)
{
// Set blob properties of TodoItem.
todoItem.ResourceName = media.Name;
}
// Send the item to be inserted. When blob properties are set this
// generates an SAS in the response.
await todoTable.InsertAsync(todoItem);
// If we have a returned SAS, then upload the blob.
if (!string.IsNullOrEmpty(todoItem.SasQueryString))
{
// Get the new image as a stream.
using (var fileStream = await media.OpenStreamForReadAsync())
{
// Get the URI generated that contains the SAS
// and extract the storage credentials.
StorageCredentials cred = new StorageCredentials(todoItem.SasQueryString);
var imageUri = new Uri(todoItem.ImageUri);
// Instantiate a Blob store container based on the info in the returned item.
CloudBlobContainer container = new CloudBlobContainer(
new Uri(string.Format("https://{0}/{1}",
imageUri.Host, todoItem.ContainerName)), cred);
// Upload the new image as a BLOB from the stream.
CloudBlockBlob blobFromSASCredential =
container.GetBlockBlobReference(todoItem.ResourceName);
await blobFromSASCredential.UploadFromStreamAsync(fileStream.AsInputStream());
}
}
// Add the new item to the collection.
items.Add(todoItem);
}
IS there anyway i can resolve this exception .Thanks.
These are the exception details -
Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException
was unhandled HResult=-2146233079 Message=Error: Internal Server
Error Source=Microsoft.Threading.Tasks StackTrace:
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task
task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.d_0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at DeltaVMobile.CrudeStorageScenario.d_22.MoveNext() in
c:\Users\~
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__0(Object
state)
at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch() InnerException:

By default, Azure Mobile Services have "dynamic schema" enabled, which means that you don't really need to define the columns in your tables - as long as you insert data, it will "figure out" the appropriate type and create the column for you. But it needs to figure out which type to use. In your case, this is the request which will be sent to the service (pretty-printing of JSON added for clarity, in reality it's sent without unnecessary whitespaces):
POST .../tables/TodoItem
Content-Type: application/json
{
"text":null,
"containerName":"todoitemimages",
"resourceName":null,
"sasQueryString":null,
"imageUri":null
}
When this gets to the service for the first time, it knows that you're trying to insert columns such as "text", "resourceName", but since there is no value associated to it (null can be of any type), it will fail to insert that data.
If this is indeed your problem, you have basically two choices: a simple one is to do a dummy insert once during development with all members of the type with some value - and right away issue a delete for the item which was inserted. This way the columns will be created and after that the runtime doesn't have to "guess" their types. The other choice is to set the EmitDefaultValue property in the [DataMember] attribute to false, which will make the serializer not emit the fields in the request with a null value.

Related

Azure Function AddDeviceAsync

I am trying to add a device with "Microsoft.Azure.Devices.Device::AddDeviceAsync(Device)" inside an Azure function with the following code :
public static async Task Run(string myQueueItem, TraceWriter log)
{
log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
var manager = new Manager();
RegisterMessage message = manager.ConvertMessage(myQueueItem);
await manager.AddToHub(log, message.MacAddress);
log.Info("fin du run");
}
public class Manager {
string EndpointUrl = "endpoint";
string PrimaryKey = "key";
string connectionString = "connectionString";
RegistryManager registryManager;
DocumentClient client;
public Manager() {
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
}
public async Task AddToHub(TraceWriter log, string macAddress) {
string deviceId = this.GenerateUniqueDevice(macAddress);
var device = new Device(deviceId);
try {
await registryManager.AddDeviceAsync(device);
}
catch(Exception e) {
log.Info($"{e.Message}");
}
}
public RegisterMessage ConvertMessage(string queueItem) {
return JsonConvert.DeserializeObject<RegisterMessage>(queueItem);
}
private string GenerateUniqueDevice(string macAddress)
{
...
}
}
this code is working perfectly fine on Visual Studio but inside Azure Function portal i got the following error
2018-02-28T14:30:28.454 [Info] Function started
(Id=3013dd9c-7248-4a01-8a4a-c087595eef97) 2018-02-28T14:30:28.720
at Microsoft.Azure.Devices.HttpClientHelper.d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at >System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotifica>tion(Task task)
at Microsoft.Azure.Devices.HttpClientHelper.d__12`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at >System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotifica>tion(Task task)
at Submission#0.Manager.d__6.MoveNext() in >D:\home\site\wwwroot\PaymentQueueTrigger\run.csx:line 41
ID:041b47841b234f9287236022c701f479-G:7-TimeStamp:02/28/2018
14:30:28"} 2018-02-28T14:30:28.798 [Info] Function completed (Success,
Id=3013dd9c-7248-4a01-8a4a-c087595eef97, Duration=343ms)
But the device parameter isn't null when I log it.
Any idea ?

Azure Notification Hub Device Registration Fails if App Service Uses Authentication

I have a working Azure App Service connected to a Notification Hub configured with GCM and APNS. It has been working perfectly for months with on both platforms.
I have now turned on authentication in the app service and configured google and facebook. These also work perfectly, and the correct access is honoured for the easy tables.
However, device registration from the mobile app now fails since the authentication has been turned on.
The following is the error I am getting on the app service (NodeJS):
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Azure.AppService.Push.PushRequestHandler.<HandleCreateOrUpdateInstallationAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.AppService.Push.PushRequestHandler.<HandlePushRequestAsync>d__f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.AppService.Push.PushModule.<OnPostAuthenticateRequestAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.AppService.Authentication.HttpModuleDispatcher.<DispatchAsync>d__13.MoveNext()
2016-11-14T09:43:00 PID[7348] Information Sending response: 500.79 Internal Server Error
2016-11-14T09:43:00 PID[7348] Critical System.ComponentModel.Win32Exception (0x80004005): An operation was attempted on a nonexistent network connection
at CHttpRequest.ReadEntityBody(Byte[] buffer, Int32 maxLength, Boolean allowAsync, Int32& bytesReceived, Int32& bytesRemaining, Boolean& completionPending)
at Microsoft.Azure.AppService.Authentication.HttpRequestBase.AsyncReadHelper.Read()
at Microsoft.Azure.AppService.Authentication.HttpRequestBase.AsyncReadHelper..ctor(HttpRequestBase request, Int32 maxLength)
at Microsoft.Azure.AppService.Authentication.HttpRequestBase.<ReadRequestContentAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.AppService.Push.PushRequestHandler.<HandleCreateOrUpdateInstallationAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.AppService.Push.PushRequestHandler.<HandlePushRequestAsync>d__f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.AppService.Push.PushModule.<OnPostAuthenticateRequestAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.AppService.Authentication.HttpModuleDispatcher.<DispatchAsync>d__13.MoveNext()
2016-11-14T09:43:00 PID[7348] Information Sending response: 500.79 Internal Server Error
I am able to send notifications (using test send and via my server push code) to existing registrations, I am just not able to create new registrations.
When I turn the authentication off, it all works normally again.
Do I need to manually create the registrations in the notification hub in this authentication scenario?
I am at a loss as to where I am going wrong, so any help would be greatly appreciated.
As an alternative to the RegisterAsync that is causing the problem; you can use a HttpClient call to PUT a Notification Hub installation. The following code will set up the installation on Android, for example:
public async Task RegisterForPushNotifications(MobileServiceClient client)
{
if (GcmClient.IsRegistered(RootView))
{
try
{
var registrationId = GcmClient.GetRegistrationId(RootView);
//var push = client.GetPush();
//await push.RegisterAsync(registrationId);
var installation = new DeviceInstallation
{
InstallationId = client.InstallationId,
Platform = "gcm",
PushChannel = registrationId
};
// Set up tags to request
installation.Tags.Add("topic:Sports");
// Set up templates to request
PushTemplate genericTemplate = new PushTemplate
{
Body = "{\"data\":{\"message\":\"$(messageParam)\"}}"
};
// Register with NH
var response = await client.InvokeApiAsync<DeviceInstallation, DeviceInstallation>(
$"/push/installations/{client.InstallationId}",
installation,
HttpMethod.Put,
new Dictionary<string, string>());
}
catch (Exception ex)
{
Log.Error("DroidPlatformProvider", $"Could not register with NH: {ex.Message}");
}
}
else
{
Log.Error("DroidPlatformProvider", $"Not registered with GCM");
}
}
The DeviceInstallation class looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace TaskList.Abstractions
{
public class DeviceInstallation
{
public DeviceInstallation()
{
Tags = new List<string>();
Templates = new Dictionary<string, PushTemplate>();
}
[JsonProperty(PropertyName = "installationId")]
public string InstallationId { get; set; }
[JsonProperty(PropertyName = "platform")]
public string Platform { get; set; }
[JsonProperty(PropertyName = "pushChannel")]
public string PushChannel { get; set; }
[JsonProperty(PropertyName = "tags")]
public List<string> Tags { get; set; }
[JsonProperty(PropertyName = "templates")]
public Dictionary<string, PushTemplate> Templates { get; set; }
}
public class PushTemplate
{
public PushTemplate()
{
Tags = new List<string>();
Headers = new Dictionary<string, string>();
}
[JsonProperty(PropertyName = "body")]
public string Body { get; set; }
[JsonProperty(PropertyName = "tags")]
public List<string> Tags { get; set; }
[JsonProperty(PropertyName = "headers")]
public Dictionary<string, string> Headers { get; set; }
}
}
Place the RegisterForPushNotifications() method in your platform-specific code. The DeviceInstallation can be in a PCL.
This is an issue with the push blade, and not something you can solve by yourself. I've reached out via email so that we can support you directly.
Not sure if you are using Xamarin.Forms but I am and been struggling with this for a week and a half.
Found that:
Firebase Cloud Messaging (FCM) is the new version of GCM
Firebase library cannot be installed in Xamarin forms due to version conflict of Android Support lib
Workaround provided by Habib Ali (not tested by me yet)

trouble shooting System.AggregateException

I need another pair of eyes to take a look at this. It's driving me nuts.
I am intermittently getting a 'System.AggregateException' when running a console app that connects to a web api.
I am doing this in a local testing environment through visual studio(IIS Express).
As stated, I have two different apps running locally on IIS Express(2 different ports). One is a console app and the other is a web api. The console app connects to the web api.
It's about 50/50 if it works or not. 50% of the time it works fine and spits out the expected results. But the other 50% of the time, it fails with the errors below. When it does fail, it's always immediate, like 2 or 3 seconds after starting the console app.
After some Googling and fiddling around with various settings, I know it's not either of these:
not a timeout issue
not a firewall issue
I've tried setting breakpoints at various points, but it never really reveals anything significant.
The exception I get when it fails is:
An exception of type 'System.AggregateException' occurred in mscorlib.dll but was not handled in user code
Here is the inner exception:
No connection could be made because the target machine actively refused it http://localhost:45321
The stack trace indicates:
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task1.get_Result()
at BeatGenerator.BeatGeneratorMain.<>c.b__2_0(Task1 postTask) in C:\Users\xxx\Documents\VS2012\DrumBeats\BeatGenerator\BeatGeneratorMain.cs:line 72
at System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
Here is the error line:
var response = await http.PostAsJsonAsync("http://localhost:45321/api/drumcorp/beats/generate", drumbeat)
.ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
This is the console app that connects to API controller:
public class DrumBeats
{
public int StartBeat { get; set; }
public int EndBeat { get; set; }
public int ChordId { get; set; }
}
public class BeatGeneratorMain
{
static void Main(string[] args)
{
Generate().Wait();
}
private static async Task Generate()
{
var drumbeat = new DrumBeats();
drumbeat.ChordId = 122;
drumbeat.StartBeat = 2;
drumbeat.EndBeat = 4;
var creds = new NetworkCredential("testUser", "xxxx", "xxx"); //username, pw, domain
var handler = new HttpClientHandler { Credentials = creds };
using (var http = new HttpClient(handler))
{
http.Timeout = TimeSpan.FromMinutes(10);
var response = await http.PostAsJsonAsync("http://localhost:45321/api/drumcorp/beats/generate", drumbeat)
.ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine(result);
}
}
}
This is the relevant section of the web api controller app:
public class DrumBeats //same as in console app
{
public int StartBeat { get; set; }
public int EndBeat { get; set; }
public int ChordId { get; set; }
}
[HttpPost("api/drumcorp/beats/generate")]
public string PostMethodBeats([FromBody] DrumBeats drumbeat)
{
string beatsChart = DrumBeatMaster.ReturnBeatsChart(DrumBeats.ChordId, DrumBeats.StartBeat, DrumBeats.EndBeat);
var mesg = "<b>Beats Created</b><br /><br /> ";
return mesg + beatsChart;
}
DrumBeatMaster.ReturnBeatsChart is just a simple helper method that processes the beats and spits out a string.
To understand what is the exception you will have to catch the aggregate exception and throw them flattened like
try
{
// Your code
}
catch (AggregateException agg)
{
throw agg.Flatten();
}

Exception thrown when using Glimpse and Postal

I'm just starting to use Glimpse with my MVC5 project and have run into an issue when I use Postal to send an email without disabling Glimpse. I've been able to narrow it down to an issue with both packages - it doesn't occur if the Glimpse cookie has not been turned on.
In Fiddler, I checked the difference between the two. When it threw the exception, the cookie was
glimpsePolicy=On
when it worked (Glimpse was off) there were two cookies
glimpseId=FBar; glimpsePolicy=
The exception I get is
System.ArgumentNullException: Value cannot be null.
Parameter name: controllerContext
at System.Web.Mvc.ChildActionValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
at Castle.Proxies.Invocations.ValueProviderFactory_GetValueProvider.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Glimpse.Core.Extensibility.ExecutionTimer.Time(Action action)
at Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ValueProviderFactoryProxy.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ControllerBase.get_ValueProvider()
at Glimpse.Mvc.Message.ActionMessageExtension.AsActionMessage[T](T message, ControllerBase controller)
at Glimpse.Mvc.AlternateType.ViewEngine.FindViews.PostImplementation(IAlternateMethodContext context, TimerResult timerResult)
at Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IViewEngineProxy.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)
at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClass6.<FindView>b__4(IViewEngine e)
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths)
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
at Postal.EmailViewRenderer.Render(Email email, String viewName)
at Postal.EmailService.Send(Email email)
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1[T0](CallSite site, T0 arg0)
at System.Web.Mvc.ActionMethodDispatcher.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at Castle.Proxies.Invocations.AsyncControllerActionInvoker_EndInvokeActionMethod.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Glimpse.Mvc.AlternateType.AsyncActionInvoker.EndInvokeActionMethod.NewImplementation(IAlternateMethodContext context)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.AsyncControllerActionInvokerProxy.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass30.<BeginInvokeActionMethodWithFilters>b__2f(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass1e.<>c__DisplayClass28.<BeginInvokeAction>b__19()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult)
I created a quick action to test it. The controller code is:
public void TestEmailExt()
{
var confirmationToken = "ConfirmationToken";
var Phone1 = "**********";
dynamic email = new Email("RegEmail");
email.To = "**#gmail.com";
email.UserName = "UserName";
email.ConfirmationToken = confirmationToken;
email.Phone = Extensions.Right(Phone1, 4);
if (email.To.Contains("#mydomain"))
email.From = INTERNAL_EMAIL_FROM;
else
email.From = EXTERNAL_EMAIL_FROM;
email.Send();
}
The reason this fails is because the Postal library creates its own HttpContext instance while rendering the email view as the decompiled CreateControllerContext method inside Postal's EmailViewRenderer class shows:
private ControllerContext CreateControllerContext()
{
HttpContextWrapper httpContextWrapper = new HttpContextWrapper(new HttpContext(new HttpRequest("", this.UrlRoot(), ""), new HttpResponse(TextWriter.Null)));
RouteData routeData = new RouteData();
routeData.Values["controller"] = (object) this.EmailViewDirectoryName;
return new ControllerContext(new RequestContext((HttpContextBase) httpContextWrapper, routeData), (ControllerBase) new EmailViewRenderer.StubController());
}
This means that the setup that Glimpse does at BeginRequest is completely removed, while the hooks are still in place to intercept MVC related calls.
We've had a similar issue where I gave a similar response to why this is not working.
UPDATE :
I mentioned above that a similar issue had been reported previously, but while I was trying to find a more appropriate solution, it seemed that this case is slightly different in that respect that the other similar issue actually executes a controller with the freshly created context resulting in a NullReferenceException in Glimpse specific code, while here we get a NullReferenceException inside MVC specific code, albeit triggered by Glimpse.
System.ArgumentNullException: Value cannot be null.
Parameter name: controllerContext
at System.Web.Mvc.ChildActionValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
And the exception we get here is because the ControllerContext property on the StubController instance (created inline) is null, which would normally be set when executing the controller (which is not the case here).
So the workaround that I proposed below still applies, but can be avoided if the code of the CreateControllerContext() above is slightly modified:
private ControllerContext CreateControllerContext()
{
HttpContextWrapper httpContextWrapper = new HttpContextWrapper(new HttpContext(new HttpRequest("", this.UrlRoot(), ""), new HttpResponse(TextWriter.Null)));
RouteData routeData = new RouteData();
routeData.Values["controller"] = (object) this.EmailViewDirectoryName;
// MODIFIED
var stubController = new EmailViewRenderer.StubController();
var controllerContext = new ControllerContext(new RequestContext(httpContextWrapper, routeData), stubController);
stubController.ControllerContext = controllerContext;
return controllerContext;
}
I've created an issue for this on the Postal issue tracker
END OF UPDATE
I think the best solution, for now, is to disable Glimpse while calling into Postal and restore normal Glimpse behavior back again afterwards. We might include this one way or the other into the Glimpse Core library in one of the upcoming releases as it seems that disabling Glimpse during a specific part of the request processing logic doesn't seem to be that uncommon, but for now the following snippet might help you (beware it makes use of a Glimpse internal key which is not guaranteed to be there in an upcoming release)
public class GlimpseSuppressionScope : IDisposable
{
private const string GlimpseRequestRuntimePermissionsKey = "__GlimpseRequestRuntimePermissions";
private readonly HttpContext currentHttpContext;
private readonly RuntimePolicy? currentRuntimePolicy;
private bool disposed;
public GlimpseSuppressionScope(HttpContext currentHttpContext)
{
if (currentHttpContext == null)
{
throw new ArgumentNullException("currentHttpContext");
}
this.currentHttpContext = currentHttpContext;
this.currentRuntimePolicy = this.currentHttpContext.Items[GlimpseRequestRuntimePermissionsKey] as RuntimePolicy?;
this.currentHttpContext.Items[GlimpseRequestRuntimePermissionsKey] = RuntimePolicy.Off;
}
~GlimpseSuppressionScope()
{
this.Dispose(false);
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (this.currentHttpContext != null)
{
this.currentHttpContext.Items.Remove(GlimpseRequestRuntimePermissionsKey);
if (this.currentRuntimePolicy.HasValue)
{
this.currentHttpContext.Items[GlimpseRequestRuntimePermissionsKey] = this.currentRuntimePolicy.Value;
}
}
}
this.disposed = true;
}
}
}
which you can then use in your controller action method as shown below:
public void TestEmailExt()
{
using (new GlimpseSuppressionScope(System.Web.HttpContext.Current))
{
var confirmationToken = "ConfirmationToken";
var Phone1 = "**********";
dynamic email = new Email("RegEmail");
email.To = "**#gmail.com";
email.UserName = "UserName";
email.ConfirmationToken = confirmationToken;
email.Phone = Extensions.Right(Phone1, 4);
if (email.To.Contains("#mydomain"))
email.From = INTERNAL_EMAIL_FROM;
else
email.From = EXTERNAL_EMAIL_FROM;
email.Send();
}
}

DatePicker.Value.Set Error binding to datasource

I have a bindingsource control called binding, on a form in VS2012, and a DateTimePicker control bound to it.
for the binding properties I have MinDate = 1/01/1753 and MaxDate = 31/12/9998
Value has been set by picking Today from the calender 5/04/2013 11:27 AM
I set a bindingsource up using
var dset = base.Context.ContactEvents;
var qry = dset.Where(p => p.Id > 0).OrderBy(x => x.Id);
qry.Load();
this.bindingSource.DataSource = dset.Local.ToBindingList();
The bindingsource is used in the following manner;
public void RefreshBindingDataSourceAndPosition(BindingSource binding)
{
binding.DataSource = this.bindingSource.DataSource; // error raised here
binding.Position = this.bindingSource.Position;
}
The error information is
System.ArgumentOutOfRangeException crossed a native/managed boundary
HResult=-2146233086
Message=Value of '1/01/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'.
Parameter name: Value
Source=System.Windows.Forms
ParamName=Value
StackTrace:
at System.Windows.Forms.DateTimePicker.set_Value(DateTime value)
InnerException:
I can work around the problem by not binding the Data Picker , and setting it in the EventsBindingSource_CurrentChanged event
However it seems odd to have to do this. How can I get the databinding working?
[Update]
This problem is similar to the one described here
I have tried to reproduce the problem in a simpler project so as to try and isolate the cause, however it works in the simpler project. Also the project works on another computer.
The problem occurs on my computer with both SQL Server 2012 and 2008R2. I have tried altering the date format and country in control panel. Also I have tried different settings for the format property. I have also tried setting the date field to support null.
When I copy the error to the clipboard it shows the following ;
System.Reflection.TargetInvocationException occurred
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
InnerException: System.ArgumentOutOfRangeException
HResult=-2146233086
Message=Value of '1/01/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'.
Parameter name: Value
Source=System.Windows.Forms
ParamName=Value
StackTrace:
at System.Windows.Forms.DateTimePicker.set_Value(DateTime value)
InnerException:
My EF class is as follows
public class ContactEvent : LoggedEntity
{
public virtual SortableBindingList<ContactEventAttendee> Attendees { get; private set; }
public virtual ContactEventType ContactEventType { get; set; }
public string Details { get; set; }
public DateTime? EventTime { get; set; }
public virtual SortableBindingList<ContactEventItem> Items { get; private set; }
public int State { get; set; }
public string Title { get; set; }
public override string ToString()
{
return "Contact Events";
}
}
it inherits from
public abstract class LoggedEntity
{
public LoggedEntity()
{
this.RowId = Guid.NewGuid();
this.RowVersionId = 0;
AppDomain dm = AppDomain.CurrentDomain; // Gets the current application domain for the current Thread.
object s = AppDomain.CurrentDomain.GetData("SiteNumber");
this.SourceSiteNumber = Convert.ToInt32(s);
}
public LoggedEntity(int SiteNumber)
{
// the following 3 are used to identify the version
this.RowId = Guid.NewGuid();
this.RowVersionId = 0;
this.SourceSiteNumber = SiteNumber;
}
public int Id { get; set; }
public Guid RowId { get; set; }
[ConcurrencyCheck]
public int RowVersionId { get; set; }
public int SourceSiteNumber { get; set; }
}
[update]
A similar problem is here
[update]
Another here makes me think I need to look at how keys are being processed.
[update]
I noticed the following in the output window
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
[update]
This led me to
here
and after turning on the debug options I found an error
Invalid object name 'dbo.__MigrationHistory'.
however that is a known bug in EF5
[Update]: I found another person with similar unsolved issues here
Discovered I dont have problems when running the .EXE
[update] I can skip over the error by disabling "Break when exceptions cross App Domain or managed/native boundary
in Tools->Options->Debugging->General
[update] I adding the following, so I could inspect the control properties.
private void EventsBindingSource_BindingComplete(object sender, BindingCompleteEventArgs e)
{
// If the BindingComplete state is anything other than success,
// set the ErrorProvider to the error message.
if (e.BindingCompleteState != BindingCompleteState.Success)
{
errorProvider1.SetError((Control)e.Binding.BindableComponent, e.ErrorText);
var errdesc = e.ErrorText;
var ctrl = (Control)e.Binding.BindableComponent;
var info = string.Format(
"{0} {1}",errdesc,
ctrl.ToString());
Debug.Print(info);
// "Value of '1/1/0001 12:00:00 AM' is not valid for 'Value'.
'Value' should be between 'MinDate' and 'MaxDate'.\r\nParameter name:
Value System.Windows.Forms.DateTimePicker, Value: 1/1/1900 12:00:00 AM"
}
else
{
errorProvider1.SetError((Control)e.Binding.BindableComponent, "");
}
}
The cause of the exception may be that the DatePicker's DataBinding "Value" property has been set to a BindingSource field.
Only the DatePicker's DataBinding "Text" property needs to be set for the data binding to work correctly.
Check if there is a value in the DatePicker's DataBinding "Value" property field, once removed the issue should go away.
It seems that the key problem is the property is a Nullable DateTime. Probably the null value used to transcribe the DateTimePicker component is '1 / 01/0001 12:00:00 AM 'which creates a problem related to the configuration of MinValue and MaxValue. Using the Advanced tab of the DataBinding has an option to set the value to be used to null One way to solve this would be setting the value to null as MinDate, or we can set the MinDate with the value '01 / 01/0001 12:00 : 00 AM '. Despite my limited experience, I believe that this may be the source of your problems. the link http://msdn.microsoft.com/en-us/library/aa480734.aspx can see something else on the Advanced tab of the property DataBinding.

Resources