Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, June 17, 2013

Godaddy - Medium Trust - MySql - Devart - System.Security.SecurityException

I have recently started encouraging my clients to use Godaddy.com - for cost reasons... and had finished with the little tweaks needed to do so.

However, a couple months after the site was up we got a real friendly email from them:

****************************
Dear [member name],

We've got great news for you! Today, we finished migrating your hosting account, [mydomainname.com], to a new server; you should start noticing improvements within 24 hours. Your visitors – they're really going to appreciate it!

The transition should've been seamless for you, unless your account hosts secondary domains or subdomains. If it does, you'll need to update their primary IP addresses (A records) with whoever controls the domain's DNS (typically the company you used to register your domain) to: ....

***************************

Unfortunately whatever changes occurred to "improve" things made my site go down.  After a bit of research into the "Security" errors I was getting from my code, I found out that they had moved us from a "full trust" environment to a "medium trust" environment.



***************************

System.Security.SecurityException: That assembly does not allow partially trusted callers.

or

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. 

***************************


What this means:

http://support.godaddy.com/help/article/1039/what-is-medium-trust-level-and-how-does-it-affect-my-hosting-account

http://stackoverflow.com/questions/2617454/what-is-medium-trust-in-asp-net

We cannot use reflection at all....

protected dbMyDataContext db = new dbMyDataContext();

However, I could not use MySql-Linq at all anymore in my applications.  I have read different solutions on how to fix this - all involving making changes to the config file for medium trust to allow certain things....  It had something to do with named pipes... or perhaps it used reflection... whatever it was - it was not permitted.

Most larger shared hosting providers (like Godaddy) do not allow customization of medium trust settings.


What works:

  • Connecting to MySql using MySql.Data.dll
  • Object Data Source to connect with data on the page using custom C# objects
  • Dlls that are written in .Net (like C#) and dont use reflection.... and dont reference anything that the medium trust restrictions prohibit
  • I (suspect) that Linq queries of xml files will work... Medium trust normally allows access to files in the website.
  • Telerik RadControls for ASP.NET AJAX work great!  I use them all over the place.  Just use code-behind to bind the grids instead of SqlDataSource.  And some pages (those with Telerik RadGrids) will need to inherit from Telerik.Web.UI.RadAjaxPage for medium trust.  


What Doesn't work:

  • Paypal Express (went back to Paypal Standard)
  • Devart Linq  (I just reverted back to standard MySql data provider - and removed all Linq)
  • SqlDataSource in asp pages... (You can build them in code behind though...)  This apparently uses reflection to bind to the data.  Grids, dropdowns and formviews need to be bound in code-behind... or using Object Data Source tags.
  • Many COTS dlls you may buy... Make sure you ask whether it will work in a medium trust environment out of the box without configuring the mediumtrust_web.config
I have heard that SqlDataSource will work in medium trust - IFF you use Sql Server, not MySql.  So far, I haven't tested this.

What it came down to is a serious re-factoring of my whole CMS to comply with Godaddy preferences..   I had to change all my Linq queries, and a lot of my Grid and FormView bindings to use code behind.

With binding of grids... had to do that in code behind... and using events triggered to methods in code behind.

I hope this helps some of you with similar issues.  If you need someone to help, contact me.

Friday, July 6, 2012

C# JSON serialize and deserialize?

For those who are new to JSON (JavaScript Object Notation), you may have seen all sorts of articles suggesting different serializers...

Sometimes you are working with a data table... sometimes with a serializable object.

But why?  Do you need a separate serializer? What for?

You could use the Built-In .Net one for ASP.net (3.5 and above), or you can roll your own, or you can use one built by someone else like JSON.net    You need to consider what is best for your personal scenario.

http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net

JSON can be used to take an object, or array of objects with serializable properties and put them into JavaScript dictionary objects or array of JavaScript dictionary objects.

Serialized Data - from a string... This can be stored in a file, a table column etc.  It then can be pulled and assessed as serialized data.  When serializing an object, this is the result you may get.  Take notice of the nesting:

Or when passing data between systems...  or from web service to a JavaScript query of the web service, you get something like:

1{
2    "some_number": 108.541,
3    "date_time""2011-04-13T15:34:09Z",
4    "serial_number""SN1234"
5    "more_data": {
6        "field1": 1.0
7        "field2""hello"  
8    }
9}

[ reference above stackoverflow.com link for more details]



Now if you want to access/use this data... using C# code behind

1using System.Web.Script.Serialization;
2
3var jss = new JavaScriptSerializer();
4var dict = jss.Deserializestring,dynamic>>(jsonText);
5
6Console.WriteLine(dict["some_number"]); //outputs 108.541
7Console.WriteLine(dict["more_data"]["field2"]); //outputs hello
[ reference above stackoverflow.com link for more details]


Now if you want to access/use this using JavaScript:

You can either use JavaScript to pull the data from a XML Web Service - OR
you can Response.Write the serialized data to the page.

You normally don't need to build a JSON "serialized" string of an object yourself.  You can use JavaScriptSerializer or one built by another vendor... to do this.

Also, JavaScript loves to use the serialized data - and can bind them into serialized objects.  This can be a single object, or a list of objects.  You can dynamically write the JSON data into the page... and assign to a root variable....   Or you can pull JSON data from a web service.  You don't even need to Deserialize the data for this!  You just need to uncomment the section at the top that says:  [System.Web.Script.Services.ScriptService]  and the web service will send you a serialized object - in JSON format when requested using JavaScript.

example: http://williamsportwebdeveloper.com/cgi/wp/?p=494

------------------------

If this works, then why ever use a commercial product like JSON.Net?

Reason: some commercial products are better at serializing some types of data, handling nulls, and may be fasterhttp://json.codeplex.com/  But Asp.Net 4.0 can handle many peoples needs natively.