As you may know, Microsoft is getting into the open source community with the ASP.NET and DNX with the mono project.
The .NET Execution Environment (DNX) is a software development kit which allows you to run .NET applications on Windows, Mac, and Linux using different frameworks such as .NET framework, .NET Core, and Mono. However, everything are not roses. There are many incomplete libraries, incompatibilities, lack of documentation, and most of the examples such as doing SQL or SOAP do not work depending on which the library targets you are planning to code for. Therefore, I decided to test the basics on all the library targets. I am starting with DNX451.

Only binary serialization seems to be working in DNX451 with C#. SOAP serialization was not included in the library, I am assuming that such library is available for DNXCORE5.
Below is a code example for those who wish to play with it.

Program.cs

using System;

namespace ConsoleApp1 {
	
	public class Program {
		
		public void Main(string[] args){			
			Console.WriteLine("Main()");
			
			displayWhichDnxIsCompilingOn();
			
			serializationExample();
			
		}
		
		private void displayWhichDnxIsCompilingOn(){
			#if DNX451
			Console.WriteLine("Compiled on DNX451: .NET Framework");
			#endif
			
			#if DNXCORE50
			Console.WriteLine("Compiled on DNXCORE50: .NET Core 5");
			#endif						
		}
				
		private void serializationExample(){
			SerializationExample bse = new SerializationExample();						
		}
			
	}
	
}
SerializationExample.cs

using System;
using System.IO;

using System.Runtime.Serialization.Formatters.Binary;

#if DNXCORE50
	// Assuming Soap formatter is available in DNXCORE50
	using System.Runtime.Serialization.Formatters.Soap;
#endif

using System.Runtime.Remoting.Messaging;

namespace ConsoleApp1 {

	class SerializationExample {
		
		public SerializationExample(){
			Console.WriteLine("SerializationExample()");
	
			ComputerModel[] computerModels = getComputerModels();		
			
			var filename = @"/Users/User/Documents/Projects/Visual Studio Projects/Examples/Serialization Example/computer.bin";		
			IRemotingFormatter formatter = new BinaryFormatter();
			saveComputerModelsInFile(filename, computerModels, formatter);
			displayComputerModelsFromFile(filename, formatter);
			
			// There is not SOAP formatter in DNX451 right now, so this code doesn't work.
			// This code need to be tested with DNXCORE50
			#if DNXCORE50
			filename = @"/Users/User/Documents/Projects/Visual Studio Projects/Examples/Serialization Example/computer.soap";
			formatter = new SoapFormatter();					
			saveComputerModelsInFile(filename, computerModels, formatter);
			displayComputerModelsFromFile(filename, formatter);
			#endif
		}
		
		private ComputerModel[] getComputerModels(){
			ComputerModel[] cm = {
				new ComputerModel(1, "MacBook Pro 6.1"),
				new ComputerModel(2, "MacMini")			
			};
			return cm;
		}
	
		private void saveComputerModelsInFile(String filename, ComputerModel[] computerModels, IRemotingFormatter usingFormatter){
			FileStream fs = new FileStream(filename, FileMode.Create);						
			usingFormatter.Serialize(fs, computerModels); 		
			fs.Close();
		}
		
		private void displayComputerModelsFromFile(String filename, IRemotingFormatter usingFormatter){
			
			FileStream fs = new FileStream(filename, FileMode.Open);
			
			ComputerModel[] serializedData = (ComputerModel[]) usingFormatter.Deserialize(fs);
			
			fs.Close();
			
			foreach (ComputerModel cm in serializedData){
				Console.WriteLine("Description: " + cm.description);
			}
		}
		
	}

}

 

 ComputerModel.cs

using System;

[Serializable]
public class ComputerModel {
	
	private int _id;
	private string _description;
	
	public int id {
		get { return _id; }
		set { _id = value; }		
	}
	
	public string description {
		get { return _description; }
		set { _description = value; }
	}
	
	public ComputerModel(){}
	
	public ComputerModel(int id, string description){
		_id = id;
		_description = description;
	}
}


 Output file: computer.bin

 

Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: Serialization: Binary & SOAP Formatter," in Alejandro G. Carlstein Ramos Mejia Blog, August 31, 2015, http://blog.acarlstein.com/?p=3149.

© 2015, Alejandro G. Carlstein Ramos Mejia. All rights reserved.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette