Microsoft Azure dll throws an exception in .NET 4.0 - json

I have developed on EXE project(use for startup task) and use following dlls of Microsoft Azure ,
It's work very well in .Net framework 3.5 but in my case i need to use system.runtime.serialization to serialize class as json string as per following way
public static string Serialize<T>(T obj)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new
System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
return retVal;
}
For this i need to change framework to 4.0 but at that time i got exception from Azure dlls
like
The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception
I think all Microsoft's dlls are with backward compatibly so what's going wrong in this matter?
I should find another way to serialize to json string?
OR
I should to change Azure's dlls to latest version?
Thanks in Advance.

If you write a console app in .NET4 and want to use the RoleEnvironment then you’ll get an error:
The type initializer for ‘Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment’ threw an exception.
To get around this, just add a “useLegacyV2RuntimeActivationPolicy” to the startup tag generated in the default app.config:
<startup useLegacyV2RuntimeActivationPolicy="true">
This is because Microsoft.WindowsAzure.ServiceRuntime.dll is a mixed mode assembly. The useLegacyV2RuntimeActivationPolicy attribute is required for referencing any mixed mode assembly, not only the Windows Azure ones.

One thing you might want to check is target framework for your .Net project in Visual Studio. By default when you create a project in VS using .Net framework, it uses ".Net Framework 4 Client Profile". Try changing it to ".Net Framework 4" and see if that helps.

Related

Quartz 2.6.2 and .NET Core? - Error "Could Not Initialize DataSource"

I'm using an older version of Quartz.NET (v2.6.2) with .NET Core (or possibly .NET5). I'm getting an error when attempting to use the StdSchedulerFactory.GetScheduler. All my configuration settings are within my appsettings.json where I populate a NameValueCollection with these values and inject them into my classes with DI.
["quartz.scheduler.instanceId"] = "instance_one",
["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz",
["quartz.threadPool.threadCount"] = "5",
["quartz.jobStore.misfireThreshold"] = "60000",
["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
["quartz.jobStore.useProperties"] = "false",
["quartz.jobStore.dataSource"] = "default",
["quartz.jobStore.tablePrefix"] = "QRTZ_",
["quartz.dataSource.default.provider"] = "SqlServer-20",
["quartz.dataSource.default.connectionString"] = quartzConn
I am using the StdSchedulerFactory like this, where Settings.Properties is that NameValueCollection which contains all the config settings:
var factory = new StdSchedulerFactory(Settings.Properties);
var scheduler = factory.GetScheduler();
On the GetScheduler method, the error, "Could Not Initialize Datasource: default" is thrown.
The crazy thing is this code works fine in a Framework 4.x project that uses a regular web.config to supply the configuration settings. Also, when I change to use Quartz 3.X with my code above, with configurations in the appsettings.json works fine. Seems that me mixing and matching both versions is causing an issue where Quartz doesn't know how to retrieve some value?
Is there a way to manually build my scheduler and not use the factory?
Thanks!
I've had to go back to Framework 4 and Quartz 2.6 to get them to play nicely together. I can only get Quartz 3.x to work with .NET Core/5. Stepping through the source code with dotPeek, Quartz 2.6 is using ConfigurationManager to pull web.config details that don't exist in Core/5. At this point I don't remember if I tried to add my own web.config file to this project or not, but I've since moved on.

CodeEffects exception with .net core 3.1 and running in docker container

After the upgrade from .net core 2.2 to 3.1 (without changing codeffects related code or library versions) we have problems when loading the xml into the rule editor (in the backend), but only when running the application in a docker container (which is based on mcr.microsoft.com/dotnet/aspnet:3.1.11 image). When running exactly the same code from Visual Studio everything is fine. Like it is also if we revert to the .net core 2.2 version of the app.
The problem is on loading the existing rule xml into the rule editor:
editor.LoadRuleXml(ruleXml);
or this way
editor.Rule = CodeEffects.Rule.Models.RuleModel.Create(ruleXml, type);
The exception is:
CodeEffects.Rule.Common.InvalidRuleException: Invalid rule XML. Input string was not in a correct format. (#125) at CodeEffects.Rule.Formats.Ce.FillRule(List`1 list, XmlNode ruleXml, XmlDocument sourceXml, GetRuleDelegate ruleDelegate, Type sourceObject) at CodeEffects.Rule.Formats.Ce.LoadXml(XmlDocument rule, XmlDocument source, GetRuleDelegate ruleDelegate, Type sourceObject) at CodeEffects.Rule.Core.RuleLoader.LoadXml(XmlDocument rule, XmlDocument source, RuleFormatType format, GetRuleDelegate getRuleDelegate, Type sourceObject) at CodeEffects.Rule.Core.RuleLoader.LoadXml(String xmlRule, XmlDocument sourceXml, GetRuleDelegate ruleDelegate, Type sourceObject) at CodeEffects.Rule.Web.RuleEditor.LoadRuleXml(String ruleXml, GetRuleDelegate ruleDelegate) at CodeEffects.Rule.Web.RuleEditor.LoadRuleXml(String ruleXml)
Logging the xml that we pass to the load method shows that the xml is as it should be, so like it was stored in the db, and this same xml otherwise works fine, just not when the app is running in a docker based on .net core 3.1.
Unfortunatelly there is no inner exception that would point us closer to the source of the problem. And since it can be only reproduced when running in docker container, we are running out of ideas.
Our version of CodeEffects.Rule.Editor.Web.Core library is quite old, 5.0.7.6, but the same problem persists even if we upgrade to the latest 5.0.38.4.
Any idea what could be the problem?

using Hibernate ORM in an EJB Project in Myeclipse with glassfish and mysql data base

I am new to J2EE development and its frameworks, so I'm leads to create a J2EE application usign Myeclipse,glassfish ans mysql as SGBD ... I need to create a project EJB3 session I have to use Hibernate3 ORM .. My concern is that I've worked with hibernate but in a web project type and not EJB and I really do not know how my project should be like .. I just need to understand the structure of my EJB project because normally we have 2 basic classes: EJBService and EJBserviceRemote .. EJBService, containing all my methods that I would need to call from my client (a web project for exemple) and EJBServiceRemote which contains the signature of each method .. so where do I rank the DAO classes generated by Hibernate ORM and how to call them?? shoukd I copy their code in EJBService and then declare in EJBServiceRemote to be able to call them by my client??
SOS I'm really disturbed
add all the jars that you're using in your ejb project to the following glassfish directory :
C:/..../glassfish/lib
C:/..../glassfish/domains/"your domain name"/lib
Caused by: java.lang.NoClassDefFoundError: org/hibernate/criterion/Criterion
may be you're missing one of the hibernate jars (hibernate-core.jar) or maybe you have an older version of hibernate + a recent one at the same time in your classpath.
ok everything is working now here is my methode to show data:
#SuppressWarnings("unchecked")
public int[][] afficheProduitsStockList(){
int j,a;
ProduitsStockDAO stockdao = new ProduitsStockDAO();
List<ProduitsStock> LPdt = stockdao.findAll();
a=LPdt.size();
int t[][]=new int[a][3];
Iterator it = LPdt.iterator();
while(it.hasNext()){
for(j=0;j<t.length;j++){
ProduitsStock pdt = (ProduitsStock)it.next();
t[j][0]=pdt.getCodeStock();
t[j][1]=pdt.getCodePdt();
t[j][2]=pdt.getQtePdt();
} }
return t;
}
and everything works !
Thank you all :)

JObject jo = new JObject() (Windows Phone 8)

I'm looking for some information about how to work with Azure Mobile Services, in a Windows Phone Client. As we can check at this link, there's some examples about how to work with mobile services.
In my case, I want to delete an ID from my SQL database. For this, we can use the example code from MSDN Documentation:
JObject jo = new JObject();
jo.Add("Id", "37BBF396-11F0-4B39-85C8-B319C729AF6D");
await table.DeleteAsync(jo);
When i paste this code into my function, I cant compile it because JObject.
This is the error I got at JObject jo = new JObject(); line:
The type or namespace name 'JObject' could not be found (are you missing a using directive or an assembly reference?)
I just can't instance a new JObject object, but the Newtonsoft.JSON are referred in my project. So I think it should work fine, but I cant instance a JObject.
This JObject class belongs to JSON.NET library (Newtonsoft).
Here's the full namespace:
Newtonsoft.Json.Linq.JObject
If you can't compile, probably you don't have the JSON.NET library in your project. Add Newtonsoft library to your project using Nuget Package.
In your VS, open your solution, open the menu Tools -> Library package manager -> package manager console
PM> Install-Package Newtonsoft.Json
When working with Windows Azure Mobile Services and Windows Phone 8. I recommend on starting from downloading the template project right from Mobile Service dashboard.
If you want to use existing project then follow the guide from dashboard how to get started.

Json does not exist in the namespace System

In this tutorial: http://www.asp.net/web-api/videos/getting-started/custom-validation Jon uses
dynamic error = new JsonObject();
with
using System.Json;
I guess it's the JsonObject here: http://msdn.microsoft.com/en-us/library/system.json.jsonobject(v=vs.110).aspx located in:
Namespace: System.Json
Assembly: System.Runtime.Serialization (in System.Runtime.Serialization.dll)
I've added System.Runtime.Serialization a reference but still cannot find System.Json.
Am I reading the Microsoft docs wrong? (I'm using .NET 4.5)
Try this:
PM> Install-Package System.Json -Version 4.0.20126.16343
Per: http://nuget.org/packages/System.Json
It worked!
If you have questions on how to add enter nuget code, please follow the link below:
http://docs.nuget.org/docs/start-here/using-the-package-manager-console
http://www.webcosmoforums.com/asp/32551-type-namespace-name-json-does-not-exist-namespace-system-runtime-serialization.html
Most likely you are missing a reference to System.ServiceModel.Web
Make sure your application is targeting the .Net 4.5 framework in the project properties.
The System.Json objects are only available in 4.5
Edit:
Use Nuget to install system.json : 'Install-Package System.Json'
How to parse JSON without JSON.NET library?
The Json object works only from Controller class and not outside. Even if we refer System.Web.MVC outside controller, we have access only to JsonResult and not to Json as Json object is protected object of JsonResult. Please refer the below documentation which explains that,
http://msdn.microsoft.com/en-us/library/dd504936(v=vs.118).aspx
If you want to use other .Net json serializer, you can use the following:
go to manage nuget package.
search json.net.
click install.
for more details, follow - http://netfx.codeplex.com/
Using System.json will not work for .Net 4 framework. The library is deprecated.
To check it try:
Go Add reference... to project.
On .Net Tab, Search System.json, you will not find any.
That means, it is deprecated.