protobuf-net Sub-message not read correctly - c#

I'm currently testing protobuf-net (latest version), but intermittently I'm getting "Sub-message not read correctly" exception while deserializing. So far there's no apparent pattern to reproduce this error, and the data is always the same.
I googled this error and so far people reported this error only when dealing with big data (>20MB), which I'm not doing.
Can anyone point out whether this is a bug (and if it is, any possible solution to fix/circumvent this?), or am I missing some steps? Below is the code I'm using:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ProtoBuf;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const string message = "Cycle {0}: {1:N2} ms - avg: {2:N2} ms - min: {3:N2} - max: {4:N2}";
const int loop = 1000;
var counter = new Stopwatch();
var average = 0d;
var min = double.MaxValue;
var max = double.MinValue;
for (int i = 0;; i++)
{
var classThree = Create();
counter.Reset();
counter.Start();
Parallel.For(0, loop, j =>
{
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, classThree);
using (var ms2 = new MemoryStream(ms.ToArray()))
{
var des = Serializer.Deserialize<ClassThree>(ms2);
var aaa = des;
}
}
});
counter.Stop();
var elapsed = counter.Elapsed.TotalMilliseconds;
average += elapsed;
min = Math.Min(min, elapsed);
max = Math.Max(max, elapsed);
var currentAverage = average / (i + 1);
Console.Clear();
Console.WriteLine(message, i, elapsed, currentAverage, min, max);
Thread.Sleep(0);
}
}
private static ClassThree Create()
{
var classOne = new ClassSix()
{
// properties
p_i1 = -123,
p_i2 = 456,
p_l1 = -456,
p_l2 = 123,
p_s = "str",
p_f = 12.34f,
p_d = 56.78d,
p_bl = true,
p_dt = DateTime.Now.AddMonths(-1),
p_m = 90.12m,
p_b1 = 12,
p_b2 = -34,
p_c = 'c',
p_s1 = -21,
p_s2 = 43,
p_ts = new TimeSpan(12, 34, 56),
p_id = Guid.NewGuid(),
p_uri = new Uri("http://www.google.com"),
p_ba = new[] { (byte)1, (byte)3, (byte)2 },
p_t = typeof(ClassTwo),
p_sa = new[] { "aaa", "bbb", "ccc" },
p_ia = new[] { 7, 4, 9 },
p_e1 = EnumOne.Three,
p_e2 = EnumTwo.One | EnumTwo.Two,
p_list = new List<ClassFive>(new[]
{
new ClassFive()
{
i = 1,
s = "1"
},
new ClassFive()
{
i = 2,
s = "2"
}
}),
// fields
f_i1 = -123,
f_i2 = 456,
f_l1 = -456,
f_l2 = 123,
f_s = "str",
f_f = 12.34f,
f_d = 56.78d,
f_bl = true,
f_dt = DateTime.Now.AddMonths(-1),
f_m = 90.12m,
f_b1 = 12,
f_b2 = -34,
f_c = 'c',
f_s1 = -21,
f_s2 = 43,
f_ts = new TimeSpan(12, 34, 56),
f_id = Guid.NewGuid(),
f_uri = new Uri("http://www.google.com"),
f_ba = new[] { (byte)1, (byte)3, (byte)2 },
f_t = typeof(ClassTwo),
f_sa = new[] { "aaa", "bbb", "ccc" },
f_ia = new[] { 7, 4, 9 },
f_e1 = EnumOne.Three,
f_e2 = EnumTwo.One | EnumTwo.Two,
f_list = new List<ClassFive>(new[]
{
new ClassFive()
{
i = 1,
s = "1"
},
new ClassFive()
{
i = 2,
s = "2"
}
})
};
var classThree = new ClassThree()
{
ss = "333",
one = classOne,
two = classOne
};
return classThree;
}
}
public enum EnumOne
{
One = 1,
Two = 2,
Three = 3
}
[Flags]
public enum EnumTwo
{
One = 1,
Two = 2,
Three = 4
}
[ProtoContract, ProtoInclude(51, typeof(ClassSix))]
public class ClassOne
{
// properties
[ProtoMember(1)]
public int p_i1 { set; get; }
[ProtoMember(2)]
public uint p_i2 { set; get; }
[ProtoMember(3)]
public long p_l1 { set; get; }
[ProtoMember(4)]
public ulong p_l2 { set; get; }
[ProtoMember(5)]
public string p_s { set; get; }
[ProtoMember(6)]
public float p_f { set; get; }
[ProtoMember(7)]
public double p_d { set; get; }
[ProtoMember(8)]
public bool p_bl { set; get; }
[ProtoMember(9)]
public DateTime p_dt { set; get; }
[ProtoMember(10)]
public decimal p_m { set; get; }
[ProtoMember(11)]
public byte p_b1 { set; get; }
[ProtoMember(12)]
public sbyte p_b2 { set; get; }
[ProtoMember(13)]
public char p_c { set; get; }
[ProtoMember(14)]
public short p_s1 { set; get; }
[ProtoMember(15)]
public ushort p_s2 { set; get; }
[ProtoMember(16)]
public TimeSpan p_ts { set; get; }
[ProtoMember(17)]
public Guid p_id { set; get; }
[ProtoMember(18)]
public Uri p_uri { set; get; }
[ProtoMember(19)]
public byte[] p_ba { set; get; }
[ProtoMember(20)]
public Type p_t { set; get; }
[ProtoMember(21)]
public string[] p_sa { set; get; }
[ProtoMember(22)]
public int[] p_ia { set; get; }
[ProtoMember(23)]
public EnumOne p_e1 { set; get; }
[ProtoMember(24)]
public EnumTwo p_e2 { set; get; }
[ProtoMember(25)]
public List<ClassFive> p_list { set; get; }
// fields
[ProtoMember(26)]
public int f_i1 = 0;
[ProtoMember(27)]
public uint f_i2 = 0;
[ProtoMember(28)]
public long f_l1 = 0L;
[ProtoMember(29)]
public ulong f_l2 = 0UL;
[ProtoMember(30)]
public string f_s = string.Empty;
[ProtoMember(31)]
public float f_f = 0f;
[ProtoMember(32)]
public double f_d = 0d;
[ProtoMember(33)]
public bool f_bl = false;
[ProtoMember(34)]
public DateTime f_dt = DateTime.MinValue;
[ProtoMember(35)]
public decimal f_m = 0m;
[ProtoMember(36)]
public byte f_b1 = 0;
[ProtoMember(37)]
public sbyte f_b2 = 0;
[ProtoMember(38)]
public char f_c = (char)0;
[ProtoMember(39)]
public short f_s1 = 0;
[ProtoMember(40)]
public ushort f_s2 = 0;
[ProtoMember(41)]
public TimeSpan f_ts = TimeSpan.Zero;
[ProtoMember(42)]
public Guid f_id = Guid.Empty;
[ProtoMember(43)]
public Uri f_uri = null;
[ProtoMember(44)]
public byte[] f_ba = null;
[ProtoMember(45)]
public Type f_t = null;
[ProtoMember(46)]
public string[] f_sa = null;
[ProtoMember(47)]
public int[] f_ia = null;
[ProtoMember(48)]
public EnumOne f_e1 = 0;
[ProtoMember(49)]
public EnumTwo f_e2 = 0;
[ProtoMember(50)]
public List<ClassFive> f_list = null;
}
[ProtoContract]
public class ClassSix : ClassOne
{
}
[ProtoContract]
public class ClassTwo
{
}
[ProtoContract]
public interface IClass
{
[ProtoMember(1)]
string ss
{
set;
get;
}
[ProtoMember(2)]
ClassOne one
{
set;
get;
}
}
[ProtoContract]
public class ClassThree : IClass
{
[ProtoMember(1)]
public string ss { set; get; }
[ProtoMember(2)]
public ClassOne one { set; get; }
[ProtoMember(3)]
public ClassSix two { set; get; }
}
[ProtoContract]
public class ClassFour
{
[ProtoMember(1)]
public string ss { set; get; }
[ProtoMember(2)]
public ClassOne one { set; get; }
}
[ProtoContract]
public class ClassFive
{
[ProtoMember(1)]
public int i { set; get; }
[ProtoMember(2)]
public string s { set; get; }
}
}

Updated to rev. 669 and so far haven't encounter the error again. So I'm reporting this as fixed for now.

Related

New items are not saved to character class when logged out and logged back in

My "Hero" created character class has this code:
public virtual User User { get; set; }
public override List<Weapon> WeaponSack { get; set; } = new() { Weapon.ElectricityWand, Weapon.ElementalStaff };
Whenever I log back into a character, these items overwrite the weapons added through gameplay.
My looting class has this code which is supposed to add and save looted weapons to the character. The weapons are added successfully and I can use them until I log out and they get overwritten.
I am able to modify player attributes like health and experience and save those by logging out and in, but I cannot see why the weapons won't stick:
{
var CurrentCharacter = SelectCharacter.CurrentHero;
//Damage player for a percentage of their health
var damage = CurrentCharacter.Health - (CurrentCharacter.Health * 0.70);
context.User.Update(user);
CurrentCharacter.LoseHealth(CurrentCharacter, CurrentCharacter.Health, (int)damage);
context.SaveChanges();
Console.WriteLine($"You have lost {damage} health.");
Console.WriteLine($"You have {CurrentCharacter.Health} health left.");
Console.WriteLine("Press any key to collect your loot!");
Console.ReadKey();
//Generate a random weapon
Random random = new Random();
var weapon = new Weapon();
var weaponName = new List<string> { "Sword", "Axe", "Mace", "Dagger", "Bow", "Staff", "Spear", "Wand", "Hammer", "Sickle" };
var weaponType = new List<string> { "Heavy", "Defensive", "Offensive", "Light", "Damaged", "Reinforced", "Magic", "Wooden", "Metallic", "Colorful" };
var weaponLevel = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var weaponDamage = new List<int> { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
var weaponRarity = new List<string> { "Common", "Uncommon", "Rare", "Epic", "Legendary" };
var weaponRarityChance = new List<int> { 70, 15, 10, 4, 1 };
var weaponRarityChanceTotal = weaponRarityChance.Sum();
var weaponRarityChanceRandom = random.Next(1, weaponRarityChanceTotal + 1);
var weaponRarityChanceIndex = 0;
for (var i = 0; i < weaponRarityChance.Count; i++)
{
weaponRarityChanceIndex += weaponRarityChance[i];
if (weaponRarityChanceRandom <= weaponRarityChanceIndex)
{
weapon.Rarity = weaponRarity[i];
break;
}
}
weapon.Name = weaponName[random.Next(0, weaponName.Count)];
weapon.Degradation = random.Next(1, 15);
weapon.Type = weaponType[random.Next(0, weaponType.Count)];
weapon.Damage = weaponDamage[random.Next(0, weaponDamage.Count)];
weapon.Level = weaponLevel[random.Next(0, weaponLevel.Count)];
context.Hero.Update(CurrentCharacter);
CurrentCharacter.WeaponSack.Add(weapon);
context.SaveChanges();
Console.WriteLine($"You have found a [level {weapon.Level}] [{weapon.Rarity}] ({weapon.Type} {weapon.Name})! It has {weapon.Degradation} uses left.");
Console.WriteLine("Press any key to continue your adventure!");
Console.ReadKey();
Adventure.AdventureStart(user, hero);
break;
}
When weapons are looted, they are tied to the UserId in the database, but they won't stay in the Hero's weapon list. So I have a whole table of weapons tied to Id's that are inaccessible.
Is the public override of list in the Hero class my problem? I tried to find a way to retrieve the weapons that are tied to the userId and populate that in but I could not figure it out.
Classes as requested:
namespace HeroModels
{
public class Weapon
{
public int Id { get; set; }
public string Name { get; set; }
public int Damage { get; set; }
public int Level { get; set; }
public string Rarity { get; set; }
public string Type { get; set; }
public int Degradation { get; set; }
public Weapon(string name, int damage, int level, string rarity, string type, int degradation)
{
Name = name;
Damage = damage;
Level = level;
Rarity = rarity;
Type = type;
Degradation = 10;
}
public Weapon()
{
}
/*Magic weapons*/
public static Weapon ElectricityWand =
new(name: "Electricity Wand", damage: 5, level: 1, rarity: "Common", type: "Magic", degradation: 10);
public static Weapon ElementalStaff =
new(name: "Elemental Staff", damage: 30, level: 5, rarity: "Common", type: "Magic", degradation: 10);
public int CalculateDamage(Weapon weapon)
{
Random random = new();
var damage = random.Next(weapon.Damage);
return damage;
}
}
}
Hero class :
using HeroModels;
using SolutionItems;
using System.ComponentModel.DataAnnotations;
namespace UserRegistration
{
public class Hero : IHero
{
public Hero()
{
}
[Key]
public new int Id { get; set; }
public override string? Name { get; set; } = "Harry Potter";
public override string Class { get; set; } = "Mage";
public override int Health { get; set; } = 100;
public override int Damage { get; set; } = 10;
public override int Armor
{ get; set; } = 5;
public override int Level
{ get; set; } = 1;
public override int Experience
{ get; set; } = 0;
public override int Gold
{ get; set; } = 0;
public override int Strength
{ get; set; } = 10;
public virtual User User { get; set; }
public virtual List<Weapon> WeaponSack { get; set; } = new() { Weapon.ElectricityWand, Weapon.ElementalStaff };
public override List<Food> FoodSack { get; set; } = new() { Food.trout, Food.salmon };
public override string Attack(IHero hero)
{
return "";
}
public override int GainExperience(IHero hero, int experience)
{
return hero.Experience += experience;
}
public override int GainGold(IHero hero, int gold)
{
return hero.Gold += gold;
}
public override string GainHealth(IHero hero, int health)
{
hero.Health += health;
if (hero.Health >= 100)
{
hero.Health = 100;
}
return $"Your health is now at {hero.Health}!";
}
public override string LoseHealth(IHero hero, int health, int damage)
{
hero.Health -= damage;
if (hero.Health <= 0)
{
Environment.Exit(1);
return "You have died.";
}
return $"You have taken {damage} damage. Your health is now at {hero.Health}!";
}
public override string LevelUp(IHero hero)
{
hero.Level += 1;
var level = hero.Level;
return $"Congratulations! You are now level {Level}";
}
}
}
How I load
private DbContext LogIn()
{
Console.WriteLine("Please enter your username");
string username = Console.ReadLine();
Console.WriteLine("Please enter your password");
string password = Console.ReadLine();
using var context = new HeroesDBContext(_optionsBuilder.Options);
var usercompare = new HeroesDBContext().User.Where(u => u.Username == username && u.Password == password);
if (!usercompare.Any())
{
Console.WriteLine("User not found");
WelcomeScreen();
}
CurrentHero = context.Hero.FirstOrDefault(h => h.User.Username == username);
CurrentUser = context.User.Where(u => u.Username == username && u.Password == password).FirstOrDefault();
context.SaveChanges();
Adventure.AdventureStart(CurrentUser, CurrentHero);
return context;
}
in the LogIn method you can load your weapon by eager Loading as following
CurrentHero = context.Hero.Include(h=>h.WeaponSack).FirstOrDefault(h => h.User.Username == username);

create json structure for in wcf(dynamic key for json)

How to declare dynamic key using class?
{
"balls": {"a8bf081d-eaef-44db-ba25-97ed8c0b30ef": {"team": {"runs": 1,
"extras": 0,
"ball_count": 1,
"wicket": 0
}
},
}}
Based on your last Json format, still not valid but close.
You have the following class
public class Team
{
public int runs { get; set; }
public int extras { get; set; }
public int ball_count { get; set; }
public int wicket { get; set; }
}
Using a Dictionary<string, Team> you can simply:
var objFoo = new
{
balls = new Dictionary<string, Team>
{
{
"a8bf081d-eaef-44db-ba25-97ed8c0b30ef",
new Team{
runs=1,
extras=0,
ball_count=1,
wicket=0
}
}
}
};
var result = JsonConvert.SerializeObject(objFoo);
online Exemple

EDI Fabric .ToEDI() has empty group fields

I'm using EDIFabric to build up an interchange and single message. I have a very simple format I need to output to an EDI x12 string. My code populates the interchange and group properly (viewing the local variables and collections), however when I run .ToEdi() on my interchange object I'm getting back empty message groups
My Output:
"ISA*00* *00* *IS*SenderID *IS*ReceiverID *160809*1008*^*00501*000001ISA*0*P*:~GS*FA*GS_02_SenderCode*GS_03_ReceiverCode*21160809*1008*00001GS06*X*005010~ST~AK1~AK2~IK3~IK5~AK9~SE~GE*000001*00001GS06~IEA*00001*000001ISA~"
Empty when shouldn't be
ST~AK1~AK2~IK3~IK5~AK9~SE~GE
Creation class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EdiFabric.Framework.Envelopes.X12;
using EdiFabric.Definitions.Hipaa_005010_999_X231;
using EdiFabric.Framework.Messages;
using EdiFabric.Framework.Envelopes;
namespace X12PayloadProcessing
{
public class DS_M_999
{
private DS_M_999_Interchange _Interchange;
private DS_M_999_Group _Group;
private Message _EDI_Message;
private S_ISA _EDI_ISA = new S_ISA();
private S_IEA _EDI_IEA = new S_IEA();
public Interchange _EDI_Interchange = new Interchange();
private S_GE _EDI_GE = new S_GE();
private S_GS _EDI_GS = new S_GS();
private Group _EDI_Group = new Group();
private M_999 _EDI_999 = new M_999();
private S_ST _EDI_ST = new S_ST();
private S_SE _EDI_SE = new S_SE();
private S_AK1 _EDI_AK1 = new S_AK1();
private S_AK2 _EDI_AK2 = new S_AK2();
private S_AK9 _EDI_AK9 = new S_AK9();
private S_IK3 _EDI_IK3 = new S_IK3();
private S_IK5 _EDI_IK5 = new S_IK5();
private List<G_TS999_2000> _EDI_2000_List = new List<G_TS999_2000>();
private G_TS999_2000 _EDI_2000 = new G_TS999_2000();
private G_TS999_2100 _EDI_2100 = new G_TS999_2100();
private List<G_TS999_2100> _EDI_2100_List = new List<G_TS999_2100>();
public DS_M_999(DS_M_999_Group Group, DS_M_999_Interchange Interchange)
{
_Group = Group;
_Interchange = Interchange;
_EDI_Interchange.Groups = new List<Group>();
_EDI_ISA.D_744_1 = _Interchange.ISA_01_Authorization.PadRight(2).Substring(0,2);
_EDI_ISA.D_745_2 = _Interchange.ISA_02_AuthInfo.PadRight(10).Substring(0,10);
_EDI_ISA.D_746_3 = _Interchange.ISA_03_SecurityQualifier.PadRight(2).Substring(0, 2);
_EDI_ISA.D_747_4 = _Interchange.ISA_04_Password.PadRight(10).Substring(0, 10);
_EDI_ISA.D_704_5 = _Interchange.ISA_05_SenderQualifier.PadRight(2).Substring(0, 2);
_EDI_ISA.D_705_6 = _Interchange.ISA_06_SenderId.PadRight(15).Substring(0,15);
_EDI_ISA.D_704_7 = _Interchange.ISA_07_ReceiverQualifier.PadRight(2).Substring(0, 2);
_EDI_ISA.D_706_8 = _Interchange.ISA_08_ReceiverId.PadRight(15).Substring(0, 15);
_EDI_ISA.D_373_9 = _Interchange.ISA_09_DateReceived.PadRight(6).Substring(0, 6);
_EDI_ISA.D_337_10 = _Interchange.ISA_10_TimeRecieved.PadRight(4).Substring(0, 4);
_EDI_ISA.D_726_11 = _Interchange.ISA_11_RepetitionSeparator.PadRight(1).Substring(0, 1);
_EDI_ISA.D_703_12 = _Interchange.ISA_12_X12Version.PadRight(5).Substring(0, 5);
_EDI_ISA.D_709_13 = _Interchange.ISA_13_UniqueNumberCounter.PadLeft(9,'0');
_EDI_ISA.D_749_14 = _Interchange.ISA_14_AcknoledgementRequested;
_EDI_ISA.D_748_15 = _Interchange.ISA_15_UsageIndicator;
_EDI_ISA.D_701_16 = _Interchange.ISA_16_CompElementSeparator;
_EDI_IEA.D_405_1 = _Interchange.IEA_01_FunctionalGroupCounter.PadLeft(5, '0');
_EDI_IEA.D_709_2 = _Interchange.IEA_02_UniqueNumberCounter.PadLeft(9, '0');
_EDI_Interchange.Iea = _EDI_IEA;
_EDI_Interchange.Isa = _EDI_ISA;
_EDI_GE.D_97_1 = _Interchange.GE_01_TransactionSetCounter.PadLeft(6, '0');
_EDI_GE.D_28_2 = _Interchange.GE_02_UniqueNumberCounter.PadLeft(9, '0');
_EDI_Group.Ge = _EDI_GE;
_EDI_GS.D_479_1 = _Interchange.GS_01.PadRight(2).Substring(0, 2);
_EDI_GS.D_142_2 = _Interchange.GS_02_SenderCode;
_EDI_GS.D_124_3 = _Interchange.GS_03_ReceiverCode;
_EDI_GS.D_29_4 = _Interchange.GS_04_DateReceived.PadRight(6).Substring(0, 8); //datetime.ToString("yyyyMMdd")
_EDI_GS.D_30_5 = _Interchange.GS_05_TimeReceived.PadRight(4).Substring(0, 4); //datetime.ToString("HHmm")
_EDI_GS.D_28_6 = _Interchange.GS_06_UniqueNumberCounter.PadLeft(9,'0');
_EDI_GS.D_455_7 = _Interchange.GS_07_AgencyCode.PadRight(1).Substring(0, 1);
_EDI_GS.D_480_8 = _Interchange.GS_08_X12Version.PadRight(6).Substring(0, 6);
_EDI_Group.Gs = _EDI_GS;
//Set ST Block
_EDI_ST.D_ST01 = X12_ID_143.Item999;
_EDI_ST.D_ST02 = _Group.ST_02_TransactionSetCounter;
_EDI_ST.D_ST03 = X12_ID_1705.Item005010X231A1;
_EDI_999.S_ST = _EDI_ST;
//Set AK1 Block
_EDI_AK1.D_AK101 = _Group.AK_101_FunctionalIdentifier;
_EDI_AK1.D_AK102 = _Group.AK_102_GroupControlNumber;
_EDI_AK1.D_AK103 = _Group.AK_103_VersionIdentifier;
_EDI_999.S_AK1 = _EDI_AK1;
_EDI_AK2.D_AK201 = _Group.AK_201_TransactionSetIdentifier;
_EDI_AK2.D_AK202 = _Group.AK_202_ControlNumber;
_EDI_AK2.D_AK203 = _Group.AK_203_VersionIdentifier;
_EDI_2000.S_AK2 = _EDI_AK2;
//Set IK3 Block
_EDI_IK3.D_IK301 = _Group.IK_301_MissingSegment;
_EDI_IK3.D_IK302 = _Group.IK_302_PositionInTransactionSet;
_EDI_IK3.D_IK304 = X12_ID_620.Item3; // _Group.IK_304_ErrorCode;
_EDI_2100.S_IK3 = _EDI_IK3;
//Set IK5 Block
_EDI_IK5.D_IK501 = X12_ID_717.R;
_EDI_IK5.D_IK502 = X12_ID_618.Item5;
_EDI_2000.S_IK5 = _EDI_IK5;
//Set Lists
_EDI_2100_List.Add(_EDI_2100);
_EDI_2000.G_TS999_2100 = _EDI_2100_List;
_EDI_2000_List.Add(_EDI_2000);
_EDI_999.G_TS999_2000 = _EDI_2000_List;
//AK9 Block
_EDI_AK9.D_AK901 = X12_ID_715.R;//_Group.AK_901_RejectIndicator;
_EDI_AK9.D_AK902 = _Group.AK_902_NumberOfTransactionSets;
_EDI_AK9.D_AK903 = _Group.AK_903_NumberOfTransactionSets;
_EDI_AK9.D_AK904 = _Group.AK_904_NumberAccepted;
_EDI_999.S_AK9 = _EDI_AK9;
//SE Block
_EDI_SE.D_SE01 = _Group.SE_01_SegmentCounter;
_EDI_SE.D_SE02 = _Group.SE_02_TransactionSetCounter;
_EDI_999.S_SE = _EDI_SE;
_EDI_Message = new Message(_EDI_999);
_EDI_Group.Messages.Add(new Message((object)_EDI_999));
_EDI_Interchange.Groups.Add(_EDI_Group);
}
public string GetEdiString()
{
var parsedXml = _EDI_Interchange.ToEdi();
return string.Concat(parsedXml);
}
}
public class DS_M_999_Interchange
{
public string ISA_01_Authorization { get; set; } //default 00
public string ISA_02_AuthInfo { get; set; } //defualt ""
public string ISA_03_SecurityQualifier { get; set; } //default 00
public string ISA_04_Password { get; set; } //default ""
//get from 270/276
public string ISA_05_SenderQualifier { get; set; }
public string ISA_06_SenderId { get; set; }
public string ISA_07_ReceiverQualifier { get; set; }
public string ISA_08_ReceiverId { get; set; }
public string ISA_09_DateReceived { get; set; } //default YYMMDD
public string ISA_10_TimeRecieved { get; set; } //default HHMM
public string ISA_11_RepetitionSeparator { get; set; } //default ^
public string ISA_12_X12Version { get; set; } //default 00501
public string ISA_13_UniqueNumberCounter { get; set; } //will be generated by app
public string ISA_14_AcknoledgementRequested { get; set; } //default 0
public string ISA_15_UsageIndicator { get; set; } //default P
public string ISA_16_CompElementSeparator { get; set; } //default :
//Interchange GS
public string GS_01 { get; set; } //default FA
//from 270/276
public string GS_02_SenderCode { get; set; }
public string GS_03_ReceiverCode { get; set; }
public string GS_04_DateReceived { get; set; } //default CCYYMMDD
public string GS_05_TimeReceived { get; set; } //default HHMM
public string GS_06_UniqueNumberCounter { get; set; } //will be generated by app
public string GS_07_AgencyCode { get; set; } //default "X"
public string GS_08_X12Version { get; set; } //default 00501X231A1
public string GE_01_TransactionSetCounter { get; set; } //number of transaction sets output typically 1
public string GE_02_UniqueNumberCounter { get; set; } //same as GS06
public string IEA_01_FunctionalGroupCounter { get; set; } //number of functional groups typically 1
public string IEA_02_UniqueNumberCounter { get; set; } //same as ISA_13
public DS_M_999_Interchange()
{
ISA_01_Authorization = "00";
ISA_02_AuthInfo = "";
ISA_03_SecurityQualifier = "00";
ISA_04_Password = "";
ISA_09_DateReceived = DateTime.Now.ToString("yyMMdd");
ISA_10_TimeRecieved = DateTime.Now.ToString("HHMM");
ISA_11_RepetitionSeparator = "^";
ISA_12_X12Version = "005010";
ISA_13_UniqueNumberCounter = "1ISA"; //Need to generate/track
ISA_14_AcknoledgementRequested = "0";
ISA_15_UsageIndicator = "P";
ISA_16_CompElementSeparator = ":";
GS_01 = "FA";
GS_04_DateReceived = (DateTime.Now.Year / 100 + 1).ToString() + DateTime.Now.ToString("yyMMdd");
GS_05_TimeReceived = DateTime.Now.ToString("HHMM");
GS_06_UniqueNumberCounter = "1GS06"; //Need to generate/track
GS_07_AgencyCode = "X";
GS_08_X12Version = "005010X231A1";
GE_01_TransactionSetCounter = "1";
GE_02_UniqueNumberCounter = GS_06_UniqueNumberCounter;
IEA_01_FunctionalGroupCounter = "1";
IEA_02_UniqueNumberCounter = ISA_13_UniqueNumberCounter;
}
}
public class DS_M_999_Group
{
public string ST_01_TransactionSetIdentifier { get; set; } //default 999
public string ST_02_TransactionSetCounter { get; set; } //will be generated by app startiing at 0001 (always 0001 in our instance)
public string ST_03_X12Version { get; set; } //default 005010x231a1
//from 270 /276
public string AK_101_FunctionalIdentifier { get; set; }
public string AK_102_GroupControlNumber { get; set; }
public string AK_103_VersionIdentifier { get; set; }
public string AK_201_TransactionSetIdentifier { get; set; }
public string AK_202_ControlNumber { get; set; }
public string AK_203_VersionIdentifier { get; set; }
public string IK_301_MissingSegment { get; set; } //set from x12ParserHelper MissingFields
public string IK_302_PositionInTransactionSet { get; set; } //need to figure this out by parsing incoming edi
public string IK_304_ErrorCode { get; set; } //default 3
public string IK_501_RejectIndicator { get; set; } //default R
public string IK_502_RejectCode { get; set; } //default 5
public string AK_901_RejectIndicator { get; set; } //default R
//from 270/276
public string AK_902_NumberOfTransactionSets { get; set; }
public string AK_903_NumberOfTransactionSets { get; set; }
public string AK_904_NumberAccepted { get; set; } //default 0
public string SE_01_SegmentCounter { get; set; } //#of segments in tx set ST to SE
public string SE_02_TransactionSetCounter { get; set; } //same as ST02
public DS_M_999_Group()
{
ST_01_TransactionSetIdentifier = "999";
ST_02_TransactionSetCounter = "0001";
ST_03_X12Version = "005010X231A1";
IK_304_ErrorCode = "3";
IK_501_RejectIndicator = "R";
IK_502_RejectCode = "5";
AK_901_RejectIndicator = "R";
AK_904_NumberAccepted = "0";
SE_01_SegmentCounter = "1";
SE_02_TransactionSetCounter = ST_02_TransactionSetCounter;
}
}
}
Test Class:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X12PayloadProcessing;
namespace X12PayloadProcessing.Tests
{
[TestClass]
public class DS_M_999_Tests
{
[TestMethod]
public void DSM999_ToEdiStringShouldBeValid()
{
DS_M_999_Interchange Interchange = new DS_M_999_Interchange();
Interchange.ISA_05_SenderQualifier = "ISA_05_SenderQualifier";
Interchange.ISA_06_SenderId = "SenderID"; //15 space padded
Interchange.ISA_07_ReceiverQualifier = "ISA_06_ReciverQualifier";
Interchange.ISA_08_ReceiverId = "ReceiverID"; //15 space padded
Interchange.GS_02_SenderCode = "GS_02_SenderCode";
Interchange.GS_03_ReceiverCode = "GS_03_ReceiverCode";
DS_M_999_Group Group = new DS_M_999_Group();
Group.AK_101_FunctionalIdentifier = "AK_101_FunctionalIdentifier";
Group.AK_102_GroupControlNumber = "AK_102_GroupControlNumber";
Group.AK_103_VersionIdentifier = "AK_103_VersionIdentifier";
Group.AK_201_TransactionSetIdentifier = "AK_201_TransactionSetIdentifier";
Group.AK_202_ControlNumber = "AK_202_ControlNumber";
Group.AK_203_VersionIdentifier = "AK_203_VersionIdentifier";
Group.IK_301_MissingSegment = "IK_301_MissingSegment";
Group.IK_302_PositionInTransactionSet = "IK_302_PositionInTransactionSet";
Group.AK_902_NumberOfTransactionSets = "AK_902_NumberOfTransactionSets";
Group.AK_903_NumberOfTransactionSets = "AK_903_NumberOfTransactionSets";
DS_M_999 DS_M_999 = new DS_M_999(Group, Interchange);
string ediString = DS_M_999.GetEdiString();
var x = DS_M_999._EDI_Interchange.ToEdi();
Assert.IsNotNull(ediString);
}
}
}
The test will pass as is, but it's not generating the expected EDI output. Obewon Kenobe Please help, your our only hope.
The problem seems to be in the creation of the GS, where in D_480_8 the full version number needs to be set, so that this:
_EDI_GS.D_480_8 = _Interchange.GS_08_X12Version.PadRight(6).Substring(0, 6);
should be changed to this:
_EDI_GS.D_480_8 = _Interchange.GS_08_X12Version;
It's the way the parser determines the correct type - it pulls out the X231A1 part (or the Origin) from the GS instead of the ST or MessageContext.
I found the issue to be lying in the SDK code that got translated into my own code base. The GS.D_480_8 SHOULD NOT be padded as in
_EDI_GS.D_480_8 = _Interchange.GS_08_X12Version.PadRight(6).Substring(0, 6);
The SDK File in question is GRP.cs code:
public void Version(string versionNo)
{
versionNo = versionNo.PadRight(6);
Group.Gs.D_480_8 = versionNo.Substring(0, 6);
}
My particular file was a GS08 version of 12 chars long which when not all present the EDIFabric framework was not handling.

protobuf does not deserialize object corrctly

I have three classes:
[ProtoContract]
public class Message
{
[ProtoMember(1)]
public int MethodId { set; get; }
[ProtoMember(2)]
public CustomArgs Arguments { set; get; }
}
[ProtoContract]
public class CustomArgs
{
[ProtoMember(1)]
public int IntVal { set; get; }
[ProtoMember(2)]
public string StrVal { set; get; }
[ProtoMember(3)]
public CycleData Cd { set; get; }
}
[ProtoContract]
public class CycleData
{
[ProtoMember(1)]
public int Id { set; get; }
[ProtoMember(2, AsReference = true)]
public CycleData Owner { set; get; }}
So when I create objects then serialize and deserialize it the Arguments property stay null but orignal object have a value. The sample code is:
static void Main(string[] args)
{
CycleData cd = new CycleData()
{
Id = 5
};
cd.Owner = cd;
CustomArgs a = new CustomArgs()
{
IntVal = 5,
StrVal = "string",
Cd = cd
};
Message oldMsg = new Message()
{
MethodId = 3,
Arguments = a
};
Stream st = new MemoryStream();
ProtoBuf.Serializer.Serialize(st, oldMsg);
var newMsg = ProtoBuf.Serializer.Deserialize<Message>(st);
}
So newMsg.Arguments is null after deserialize. What i do wrong?
You have a simple error. Once you serialize/write to the memstream, the .Pointer remain at the end of the stream. Deserializing immediately after using on the same stream fails because there is nothing to read after that point. Just reset it:
using (Stream st = new MemoryStream())
{
ProtoBuf.Serializer.Serialize(st, oldMsg);
st.Position = 0; // point to start of stream
var newMsg = ProtoBuf.Serializer.Deserialize<Message>(st);
}
I also put the stream in a using block to dispose of it.

How can i add list data in my object in visual studio 2005?

Below codes run perfectly but i want to re generate simply
static void YeniMethodListele()
{
Calısan calisan = new Calısan(){ ID=1, Ad="xxx", SoyAd="yyy"};
List<Calısan> myList = new List<Calısan>();
myList.Add(calisan);
MyCalısan myCalısan = new MyCalısan() { list = myList };
//myCalısan.list.Add(calisan);
foreach (Calısan item in myCalısan.list)
{
Console.WriteLine(item.Ad.ToString());
}
}
}
public class Calısan
{
public int ID { get; set; }
public string Ad { get; set; }
public string SoyAd { get; set; }
}
public class MyCalısan
{
public List<Calısan> list { get; set; }
public MyCalısan()
{
list = new List<Calısan>();
}
}
Here is a sample of a couple of ways to create the list a little more simply. Note the small change to the Calısan object to give it a default constructor and an overloaded constructor.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
////Calısan calisan = new Calısan() { ID = 1, Ad = "xxx", SoyAd = "yyy" };
MyCalısan myCalısan = new MyCalısan();
//option 1:
//==========
myCalısan.list.AddRange(new[] { new Calısan() { ID = 1, Ad = "xxx", SoyAd = "yyyy" }, new Calısan() { ID = 2, Ad = "blah", SoyAd = "jiggy" } });
//option 2:
//=========
myCalısan.list.AddRange(new[] { new Calısan(1, "xxx", "yyy"), new Calısan(2, "blah", "jiggy") });
////myCalısan.list.Add(calisan);
foreach (Calısan item in myCalısan.list)
{
Console.WriteLine(item.Ad.ToString());
}
Console.ReadKey();
}
}
public class Calısan
{
public Calısan() { }
public Calısan(int id, string ad, string soyad)
{
ID = id;
Ad = ad;
SoyAd = soyad;
}
public int ID { get; set; }
public string Ad { get; set; }
public string SoyAd { get; set; }
}
public class MyCalısan
{
public List<Calısan> list { get; set; }
public MyCalısan()
{
list = new List<Calısan>();
}
}
}

Categories

Resources