Serializable attribute in silverlight 4 - silverlight

So do we or do we not have a Serializable attribute in silverlight 4? I have some confusing responses on the internet. When I try to use it in my code, i get a namespace error. These are my includes
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.Serialization;
I have the assemblies System ,System.Runtime.Serialization added to my project.
A follow up question is, if it is not available in Silverlight how do I correctly serialize a singleton? Since I was planning to use the example given here http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx
Thanks

That's a .NET attribute which you can't use in Silverlight, but you can use DataContract to serialize.
For stand-alone (non-WCF) serialization/deserialization, there are three components which can be used:
System.Runtime.Serialization.DataContractSerializer (from System.Runtime.Serialization.dll)
System.Runtime.Serialization.Json.DataContractJsonSerializer (from System.ServiceModel.Web.dll)
System.Xml.Serialization.XmlSerializer (from System.Xml.Serialization.dll)
A simple example using the DataContractSerializer:
string SerializeWithDCS(object obj)
{
if (obj == null) throw new ArgumentNullException("obj");
DataContractSerializer dcs = new DataContractSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
dcs.WriteObject(ms, obj);
return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);
}
Example from this thread: http://forums.silverlight.net/forums/p/23161/82135.aspx

Related

LocalReport in full .net class library referenced in .net core project

I'm trying to build a service to export a rdlc Localreport from one of my actions in my webapi. Webapi is built on .net core 3.1
I know that reportviewer is not compatible with .net core, so to try and mitigate that I've got a class library project added to my project based on .net framework 4.7.2. Added the reference to the class library to my .net core webapi, so far so good. I'm able to call methods from my class library, no problem.
Now try adding LocalReport to a class in my class library....
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Reports
{
public class RenderAction
{
public void GetOrderReport()
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
string p = Path.GetDirectoryName(path);
string reportPath = Path.Combine(p, "Reports","Order.rdlc");
if (!File.Exists(reportPath)) { return; }
var report = new LocalReport();
}
}
}
intellisense prompted to install Microsoft.Reporting.Viewer so I did so... When calling my function in runtime, I get the following error when creating the new instance of localreport:
System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'Void System.AppDomainSetup.set_ActivationArguments(System.Runtime.Hosting.ActivationArguments)'.
Source=Microsoft.ReportViewer.Common
StackTrace:
at Microsoft.Reporting.ReportRuntimeSetupHandler.InitAppDomainPool(Evidence sandboxEvidence, PolicyManager policyManager)
at Microsoft.Reporting.LocalService..ctor(ILocalCatalog catalog, Evidence sandboxEvidence, PolicyManager policyManager)
at Microsoft.Reporting.ControlService..ctor(ILocalCatalog catalog)
at Microsoft.Reporting.WebForms.LocalReport..ctor()
at Myproject.Reports.RenderAction.GetOrderReport() in C:\Users\RudiGroenewald\source\repos\Myproject-Api-Common\Myproject_Api_Common\Myproject_Reports\RenderAction.cs:line 24
at Myproject.Api.Common.Controllers.ReportsController.Get() in C:\Users\RudiGroenewald\Source\Repos\Myproject-Api-Common\Myproject_Api_Common\Myproject_Api_Common\Controllers\ReportsController.cs:line 22
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<<InvokeActionMethodAsync>g__Logged|12_1>d.MoveNext()
It seems like some sort of dll version mismatch or something... a bit stumped really.
Is it just not possible to get this working? My alternative is to have a full .net webapi, just for reportwriting, which I prefer not to do. Any thoughts on what I'm doing wrong?

Rx Example not working

I'm trying to follow along with Jonathan Worthington's airport announcement example in An Event-driven and Reactive Future
It compiles.
The problem: SayGateChange is never called. I'm new to Rx. I must be leaving something out. What I have here is his code as exactly as I could transcribe it. Sadly, there is no source available online.
AddGateChange is supposed to push a new item onto EventStreams.GateChanged, which in turn is supposed to be watched by Announcer.Announcements, which is supposed to be watched by SayGateChange.
I'm in Windows forms, not WPF, if that makes a difference.
I will gladly put it into a console app or LinqPad if that will make it work.
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
public class frmAnnouncements
{
Announcer _Announcer = new Announcer();
ObservableCollection<string> Announcements = new ObservableCollection<string>();
private void frmRx_Load(System.Object sender, System.EventArgs e)
{
PopulateAnnouncements();
AddGateChange();
}
private void AddGateChange()
{
EventStreams.GateChanged.OnNext(new GateChanged {
Destination = "DAL",
FlightCode = 1503
});
}
private void PopulateAnnouncements()
{
_Announcer.Announcements.ObserveOnDispatcher().Subscribe(SayGateChange);
}
private void SayGateChange(string Message)
{
Interaction.MsgBox(Message);
}
public class GateChanged
{
public string FlightCode;
public string Destination;
}
public class EventStreams
{
public static Subject<GateChanged> GateChanged = new Subject<GateChanged>();
}
public class Announcer
{
public Announcer()
{
this.Announcements = EventStreams.GateChanged.Select(e => string.Format("gate change {0} to {1} ", e.FlightCode, e.Destination));
}
public IObservable<string> Announcements;
}
public frmAnnouncements()
{
Load += frmRx_Load;
}
}
As #Enigmativity stated, using ObserveOnDispatcher() is a problem - although without looking at Interaction.MsgBox its hard to be 100% certain it's the whole story - I guess it may be in the video, but it's rather long and I didn't watch it all.
The use of ObservableOnDispatcher() suggests you have pulled in the wrong nuget package for Rx:
For WPF applications, use rx-xaml (deprecated synonym rx-wpf), which provides the extension method ObserveOnDispatcher()
For Winforms applications, use rx-winforms, which provides the extension method overload ObserveOn(Control)
Both Winforms and WPF have a similar design where the user interface runs on a dedicated thread. In Winforms this is known as the "UI Thread" and in WPF as the "Dispatcher". Although the concept is very similar, the implementation is quite different.
ObserveOnDispatcher in WPF will cause the observer notifications OnXXX to be invoked on the dispatcher thread.
In WinForms, where you use ObserveOn(this), the this will generally be the form itself. For any WinForms control, this will locate the control's SynchronizationContext and Post OnXXX notifications to that.
Both overloads are smart in that invocations are direct if you happen to be on the correct Dispatcher thread or UI thread already.
I do seem to remember that WinForms is a lot more tolerant of updating UI off the UI thread - although this problem occurs in WPF too. This isn't a good thing, since it can lead to unpredictable results that are hard to debug. I note that the WinForms MessageBox.Show method, for example, doesn't care which thread it is invoked on since it creates it's own window. In general, use of some form of ObserveOn/ObserveOnDispatcher is always recommended in UI scenarios.
For this reason, it's a good idea to understand how these work in detail. For this, and to learn about the related SubscribeOn, have a look at this question.
I am surprised that you didn't get an informative InvalidOperationException stating that "The current thread has no Dispatcher associated with it." I can only think some other part of your code is swallowing exceptions, or you are using WPF code in your app as well and a Dispatcher had been created associated with the Winforms UI thread. That code behind Interaction.MsgBox is probably to blame for swallowing an error. Either way, I suggest removing rx-xaml to avoid confusion.

Combining multiple assemblies into a single EXE for a WPF application

I followed this tutorial to combine a few DLL's into my EXE.
http://www.digitallycreated.net/Blog/61/combining-multiple-assemblies-into-a-single-exe-for-a-wpf-application
The way I understand this works is:
- it starts by telling the compiler to embed (as embedded resources) each and every DLL that have their Local Copy set to True.
That part is working fine. It apparently doesn't "add" them as resources to my project (figure 1 in the tutorial kind of says otherwise), but I can tell that the size of my EXE is correct.
FYI, my program uses WPFtoolkit, in my case, that's 2 DLL's:
system.windows.controls.datavisualization.toolkit.dll
WPFToolkit.dll
Then, I set the Build Action of my App.xaml to Page, and made a program.cs file which I added to my project.
this is my project.cs:
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using System.IO;
using System.Reflection;
using System.Globalization;
namespace Swf_perium {
public class Program {
//[System.Diagnostics.DebuggerNonUserCodeAttribute()]
//[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
[STAThreadAttribute]
public static void Main() {
Swft_perium.App app = new Swf_perium.App();
AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
app.InitializeComponent();
app.Run();
}
private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
AssemblyName assemblyName = new AssemblyName(args.Name);
string path = assemblyName.Name + ".dll";
Console.WriteLine(path);
if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false)
{
path = String.Format(#"{0}\{1}", assemblyName.CultureInfo, path);
}
using (Stream stream = executingAssembly.GetManifestResourceStream(path))
{
if (stream == null)
return null;
byte[] assemblyRawBytes = new byte[stream.Length];
stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length);
return Assembly.Load(assemblyRawBytes);
}
}
}
}
After I build the project, I run it off VS2013, no problem, since both DLL's have their local copy set to true. If I go in my debug folder, take both DLL's out and run the EXE off windows explorer, then the program instantly crashes because it can find the DLL's.
What this tutorial should allow me to do is being able to run that EXE by itself without the DLL's, so yeah, it doesn't work.
I added a console writeline of the path that are being read by the OnResolveAssembly method of my program.cs. And here's what I get:
4 times the same path:
"Swf_perium.resources.dll"
Obviously, when it gets to the Stream, it's null and the method then returns null.
I am trying to understand where these paths are coming from? I don't understand why 4? And why this path?
Has anyone ever tried this technique? Comments on the blog show pretty good success rate.
Does anyone have an idea?
I made several mistakes to get to this stage, but at this point I don't see what I am doing wrong.
Thanks
Steve
EDIT: following HB's guidance, here's what I did:
I took the MSBuild target "mod" out.
Set both references' copy local to FALSE.
Added both DLL as embedded resources manually. They're both into the "Resources" directory at the root of the project.
I set App.xaml build action back to "ApplicationDefinition".
And I excluded my program.cs out of the project.
and added this code to App.xaml.cs:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Reflection;
namespace Swf_perium
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var execAssembly = Assembly.GetExecutingAssembly();
string resourceName = execAssembly.FullName.Split(',').First() + ".Resources." + new AssemblyName(args.Name).Name + ".dll";
Console.WriteLine(resourceName);
using (var stream = execAssembly.GetManifestResourceStream(resourceName))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}
}
}
now, the console prints out of the 2 DLL's filename, but not the other.. I am guessing that's why it's still not working..
that's where I'm at.
edit:
The DLL that doesn't show is not called by my code directly. it's a dependence from the first DLL. I took that second DLL out of references and resources.. If I set copy local to true for the first DLL (which my program actually uses), building the project generates both DLL at the root - in this case with both dlls generated the program works, funny thing is if I delete that second DLL, the program still works. So the problem isn't that second DLL but the first one.
the error I have (which I've had all along no matter what technique I use) is that my XAML is calling that namespace and can't find it!
edit:
Ok, well it still doesn't work. I've brought my program.cs back into the solution, set it as the entry point. And added the code suggested by HB into it.
I made sure that the assemblyresolve is done on the first line of the main so that's it's done before any wpf is done. I even added a 5s sleep just to make sure that the dll was loaded before any wpf happens. Still no go.
Either the dependence to the second DLL is what's causing a problem (?) or maybe the way I import the namespace in my XAML is incorrect. Do I need to specify that this namespace is embedded? and where it's located - i.e. its path?.
thanks
Perhaps look at Costura where it will do all the hard work of embedding assemblies for you.
Don't know your project structure but i usually add a directory for the assemblies to the root of the project and then add the dlls to that directory as embedded resource. I also then turn off the local copy of the references to make sure that it works.
Here is the code i use in my App.xaml.cs:
static App()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var execAssembly = Assembly.GetExecutingAssembly();
string resourceName = execAssembly.FullName.Split(',').First() + ".ReferencedAssemblies." +
new AssemblyName(args.Name).Name + ".dll";
using (var stream = execAssembly.GetManifestResourceStream(resourceName))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}
Simply replace the ".ReferencedAssemblies." string according to the directory you placed the dlls in.
(Using the static constructor of the class makes sure that the event is hooked up before any code that potentially accesses referenced assemblies is executed, in your code i would move the hook to the first line of Main, that may already solve your problem.)

Serializing anonymous types in Silverlight with json.net

I'm having some problems serializing an anonymous type only on the Silverlight platform. I have code on .net 4.0 and .netcf that works fine.
This line right here
Newtonsoft.Json.JsonConvert.SerializeObject(new { Something = "yup" });
throws an aptly named guy, JsonSerializationException:
Error getting value from 'Something' on '<>f__AnonymousType0`1[System.String]'.
I tried 4.0r1 and 4.0r2 - Am I doing something wrong or am I taking crazy pills?
The problem is that anonymous types are defined as internal classes by the compiler. JSON.NET relies on reflection to work, and in Silverlight reflection across assembly borders work only for public types (when used by partially trusted assemblies such as this one).
I think DataContractJsonSerializer as mentioned in the previous answer is the way to go in this case, since it's part of the framework and should have extra privileges.
Another thing to try is use dictionaries or ExpandoObject's instead of anonymous types, but YMMV.
Answer is simple;) Add [assembly: InternalsVisibleTo("Newtonsoft.Json")] to AssemblyInfo.cs and voila... I have exactly the same problem and this attribute solved my serialization/deserialization problem.
AssemblyInfo.cs
[assembly: InternalsVisibleTo("Newtonsoft.Json")]
Is there a specific reason why you want to use Json.NET? If not, you might want to try the built-in serializer (in the System.Runtime.Serialization namespace). I have to admit, I have never tried it with anonymous types, so I am not sure if this will be useful to you. Anyway, here is the class I use for serialization/deserialization:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
namespace GLS.Gui.Helper
{
public static class SerializationHelper
{
public static string SerializeToJsonString(object objectToSerialize)
{
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
serializer.WriteObject(ms, objectToSerialize);
ms.Position = 0;
using (StreamReader reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}
}
}
public static T Deserialize<T>(string jsonString)
{
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(ms);
}
}
}
}
Maybe have a look at this http://whydoidoit.com/silverlight-serializer/ as I have used this to serialize many objects in Silverlight, although I cant remember if i did anonymous types with it.
To supplement the other answers with another workaround, note that the reflection (and so the serialization of anonymous types) will succeed when running with elevated trust.

Silverlight Async Timeout Error

Calling through to my Silverlight Enabled WCF-Service in my silverlight application, occasionally users get timeouts. Whats the easiest way to boost the time allowed by the service client for a response?
The exact exception thrown is: System.TimeoutException: [HttpRequestTimedOutWithoutDetail]
Thanks
Ammount of time connection can be open: BasicHttpBinding.OpenTimeout property
Time that a connection can remain inactive, during which no application messages are received, before it is dropped: BasicHttpBinding.ReceiveTimout property
This can be set in the node of the ServiceReference.ClientConfig file in the silverlight app.
The accepted answer here did not work for me. Instead I had to cast the base channel into an IContextChannel, and set the OperationTimeout on that.
To do that, I had to create a new file with a partial class, that matched the name of the ServiceReference. In my case the I had a PrintReportsService. Code is below.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace RecipeManager.PrintReportsService
{
public partial class PrintReportsClient : System.ServiceModel.ClientBase<RecipeManager.PrintReportsService.PrintReports>, RecipeManager.PrintReportsService.PrintReports
{
public void SetOperationTimeout(TimeSpan timeout)
{
((System.ServiceModel.IContextChannel)base.Channel).OperationTimeout = timeout;
}
}
}
Then when I create the client, I do the following:
PrintReportsService.PrintReportsClient client = new RecipeManager.PrintReportsService.PrintReportsClient();
client.SetOperationTimeout(new TimeSpan(0, 4, 0));
That did it for me! More info is available here, but the code snippet in this post doesn't compile.
This can be set, but there is an upper limit that is defined by the underlying HTTP library (I think its five minutes). Note that using a higher value will fall back to what the upper limit is. Also, be aware that you may need to configure the timeout on the server as well.
I fixed this by enabling compatibility mode in IE
Rather than changing the timeout, I think you should implement a timer-based callback to the server from the Silverlight application so that the session doesn't expire. This would eliminate the overhead of longer timeouts for all of your application.

Resources