Enum Can not be serialized in Asp.net Web API - c#

I am dealing with a reference service in ASP.net Web API , when I call one of its function and fill all the required arguments I face this unusual error. The current .NET Framework of my project is 4.5.2, despite nothing happened when it was downgraded to 3.5.
You can cheer me up, if you can give me some points to tackle the problem.
Here is the error I get:
Enum value '0' is invalid for type
'WebApplication8.ServiceReference1.InsuranceCompanyEnum' and cannot be
serialized. Ensure that the necessary enum values are present and are
marked with EnumMemberAttribute attribute if the type has
DataContractAttribute attribute
Here is the code
[System.Runtime.Serialization.DataContractAttribute(Name="InsuranceCompanyEnum", Namespace="http://schemas.datacontract.org/2004/07/IGS.TRN.Common.Entities")]
public enum InsuranceCompanyEnum : int {
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Iran = 2,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Asia = 4,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Dana = 5,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Moealem = 6,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Alborz = 7,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_KarAfarin = 8,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Parsian = 9,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Toseeh = 10,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Sina = 11,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Saman = 12,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Rzai = 13,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Novin = 14,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Dey = 15,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Mihan = 16,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Mellat = 17,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Hafez = 18,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Etekayi_Amin = 19,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Iran_Moein = 20,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Omid = 21,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Pasargad = 23,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Etekayi_Iranian = 25,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Kosar = 26,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Teavon = 32,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Ma = 33,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Asmari = 34,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Sarmad = 35,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Arman = 36,
[System.Runtime.Serialization.EnumMemberAttribute()]
Faghed_Bimeh = 38,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Tejarat_No = 39,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Motaghabel_Etminan_Motahed_Gheshm = 40,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Motaghabel_Kish = 41,
[System.Runtime.Serialization.EnumMemberAttribute()]
Sherkathaye_Edghami_Dana = 42,
[System.Runtime.Serialization.EnumMemberAttribute()]
Etekayi_Bimeh_Markazi_IRR = 43,
[System.Runtime.Serialization.EnumMemberAttribute()]
Hesab_Etekayi_Vijeh = 44,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Hekmat_Saba = 45,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Zendagi_Khavarmiyaneh = 46,
[System.Runtime.Serialization.EnumMemberAttribute()]
Bimeh_Zendagi_Baran = 47,
}
WebApplication8.ServiceReference1.InquiryResponse InsertBillOfLading(WebApplication8.ServiceReference1.BillOfLading BillOfLading);
public partial class BillOfLading
{
[System.Runtime.Serialization.OptionalFieldAttribute()]
private WebApplication13.ServiceReference1.InsuranceCompanyEnum InsuranceCompanyField;
[System.Runtime.Serialization.DataMemberAttribute()]
public WebApplication8.ServiceReference1.InsuranceCompanyEnum InsuranceCompany {
get {
return this.InsuranceCompanyField;
}
set {
if ((this.InsuranceCompanyField.Equals(value) != true)) {
this.InsuranceCompanyField = value;
this.RaisePropertyChanged("InsuranceCompany");
}
}
}
}
What I have been sending to the server is:
ServiceReference1.BillOfLading bill = new ServiceReference1.BillOfLading();
bill.InsuranceCompany = (ServiceReference1.InsuranceCompanyEnum)4;
var result = srv.InsertBillOfLading(bill);

Related

TypeConversion of a string to bytearray

I have a string value:
Example:
string msg = "array('B', [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0,
1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2,
2, 2, 2, 4, 3, 2, 2, 2, 2, 5, 4, 4, 3, 4, 6, 5, 6, 6, 6, 5, 6, 6, 6,
7, 9, 8, 6, 7, 9, 7, 6, 6, 8, 11, 8, 9, 10, 10, 10, 10, 10, 6, 8, 11,
12, 11, 10, 12, 9, 10, 10, 10, 255, 219, 0, 67, 1, 2, 2, 2, 2, 2, 2,
5, 3, 3, 5, 10, 7, 6, 7, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10])"
I want to convert this string value to type byte[].
private byte[] data;
//after type conversion from string to byte array
Debug.Log("data in byte array is: " + data);
Output should be:
data in byte array is: [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0,
1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2,
2, 2, 2, 4, 3, 2, 2, 2, 2, 5, 4, 4, 3, 4, 6, 5, 6, 6, 6, 5, 6, 6, 6,
7, 9, 8, 6, 7, 9, 7, 6, 6, 8, 11, 8, 9, 10, 10, 10, 10, 10, 6, 8, 11,
12, 11, 10, 12, 9, 10, 10, 10, 255, 219, 0, 67, 1, 2, 2, 2, 2, 2, 2,
5, 3, 3, 5, 10, 7, 6, 7, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
Since the actual string msg is a very large value, I want to do this with minimum execution time possible.
Looks like a fairly simple parsing problem. We could run up a full parser for the format, but that's probably well beyond your needs.
Let's assume instead that your msg is always going to be in the format shown, starting with "array('B', [", ending with "])" and having 0 or more comma-separated decimal values. Since any option you choose is going to scan the string at least once, we can design code around doing exactly that.
The actual content contains only 4 types of characters:
digit (0-9)
comma (,)
space ( )
end-array (])
We can iterate through the characters starting at the first character after the start-array ([ at offset 11) and do something with each of those character types: ignore it, update the current value, yield the current value and/or exit.
If all of the assumptions are correct then this is a simple process. One scan through the characters, no allocations in the parser itself. Here's a sample:
IEnumerable<byte> ParseArrayMsg(string msg)
{
if (!msg.StartsWith("array('B', [") || !msg.EndsWith("])"))
yield break;
int value = 0;
for (int i = msg.IndexOf('[') + 1; i < msg.Length; i++)
{
var c = msg[i];
if (c == ',' || c == ']')
{
yield return (byte)value;
value = 0;
if (c == ']')
break;
}
else if (char.IsDigit(c))
value = value * 10 + (int)(c - '0');
else if (c != ' ')
throw new Exception($"Invalid character '{c}' at index '{i}'.");
}
}
The returned enumerable can then be processed however you like.
Your code won't actually produce the output you want, but this will:
var data = ParseArrayMsg(msg).ToArray();
Console.WriteLine($"Data in byte array is: [{string.Join(", ", data.Select(b => b.ToString()))}]");
Data in byte array is: [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0,
0, 1, 0, 1, 0, 0, 255, 219, 0, 67, 0, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 2,
2, 4, 3, 2, 2, 2, 2, 5, 4, 4, 3, 4, 6, 5, 6, 6, 6, 5, 6, 6, 6, 7, 9, 8, 6, 7,
9, 7, 6, 6, 8, 11, 8, 9, 10, 10, 10, 10, 10, 6, 8, 11, 12, 11, 10, 12, 9, 10,
10, 10, 255, 219, 0, 67, 1, 2, 2, 2, 2, 2, 2, 5, 3, 3, 5, 10, 7, 6, 7, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10]
You can try like the following:
string yourString= "Your String";
// Convert a C# string to a byte array
byte[] bytes = Encoding.ASCII.GetBytes(yourString);

Get array items by index

I have two arrays, one with values an one with indices
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
now I want a result array from the items selected by the indices of indices array
// 2, 7, 9, 13, 19
int[] result = new []{ items[1], items[3], items[5], items[6], items[7], items[9] };
Question: Is there a more generic approach for this?
var results = Array.ConvertAll(indices, i => items[i]);
Try using Linq:
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
int[] result = indices
.Select(index => items[index])
.ToArray();
A good old for loop should be able to do this job as well:
int[] items = { 1, 2, 3, 7, 8, 9, 13, 16, 19, 23, 25, 26, 29, 31, 35, 36, 39, 45 };
int[] indices = { 1, 3, 5, 6, 7, 9 };
List<int> resultList = new List<int>();
for (int i = 0; i < indices.Length; i++)
{
resultList .Add(items[indices[i]]);
}
Explanation:
when using the [ ] operator to access a specific index in indices it will return the number. This can again be used to index/access a specific location in items. So you have a double indexing.
EDIT:
If you need the result as an array you can use the ToArray method to convert it:
int [] result = resultList.ToArray();
For the sake of alternative:
int[] result = items.Select((value, index) => new { Index = index, Value = value }) //Add indexes
.Where(w => indices.Contains(w.Index)) //Filter by indexes
.Select(s => s.Value).ToArray(); //Extract values to result array

Get a special folder path by passing an integer instead of the ShellSpecialFolderConstants

While I know that special folders paths can be retrieved by using Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
for an example, to retrieve the path of the desktop, I was wondering if there is a way to retrieve the path without passing the constant ("Desktop" in this case) but the integer refering to it instead.
To make it a bit more clear; what I want to do is the following for an example:
Environment.GetFolderPath(0x00) or Environment.GetFolderPath(0)
The final goal I want to achieve being to browse through an array with only the integers refering to said special folders so I can get the paths for various special folders in a single loop.
Why would you want to make your code less readable? of course you can use numbers, its an enum - but it just means your code is way less readable and people need to remember what the numbers are which is unnecessary
Desktop = 0,
Programs = 2,
Personal = 5,
MyDocuments = 5,
Favorites = 6,
Startup = 7,
Recent = 8,
SendTo = 9,
StartMenu = 11,
MyMusic = 13,
MyVideos = 14,
DesktopDirectory = 16,
MyComputer = 17,
NetworkShortcuts = 19,
Fonts = 20,
Templates = 21,
CommonStartMenu = 22,
CommonPrograms = 23,
CommonStartup = 24,
CommonDesktopDirectory = 25,
ApplicationData = 26,
PrinterShortcuts = 27,
LocalApplicationData = 28,
InternetCache = 32,
Cookies = 33,
History = 34,
CommonApplicationData = 35,
Windows = 36,
System = 37,
ProgramFiles = 38,
MyPictures = 39,
UserProfile = 40,
SystemX86 = 41,
ProgramFilesX86 = 42,
CommonProgramFiles = 43,
CommonProgramFilesX86 = 44,
CommonTemplates = 45,
CommonDocuments = 46,
CommonAdminTools = 47,
AdminTools = 48,
CommonMusic = 53,
CommonPictures = 54,
CommonVideos = 55,
Resources = 56,
LocalizedResources = 57,
CommonOemLinks = 58,
CDBurning = 59
Apparently Merion Huhges feels you are too stupid to be able to apply Environment.GetFolderPath((Environment.SpecialFolder)20); to your code yourself.

List method to get difference of another list with duplicates (listobject.Expect method does not work)

There are two Lists. I need the difference
List<int> list1 = new List<int>() {18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() {18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
var listDif = list2.Except(list1);
foreach (var s in listDif)
Console.WriteLine(s);
Console.Read();
the answer should be 20, 86,78, 25
but it only outputs 86,78
If you want exactly that kind of behaviour you should try this:
List<int> list1 = new List<int>() { 18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() { 18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
// Remove elements of first list from second list
list1.ForEach(l => list2.Remove(l));
list2 = list2.Distinct().ToList();
list2.ForEach(d => Console.WriteLine(d));
Console.Read();
This works fine:
Make a clone of list2
Remove list1 items from list2
Code Sample:
List<int> list1 = new List<int>() { 18, 13, 22, 24, 20, 20, 27, 31, 25, 28 };
List<int> list2 = new List<int>() { 18, 13, 22, 24, 20, 20, 20, 27, 31, 25, 28, 86, 78, 25 };
var diff = list2;
list1.All(x => diff.Remove(x));
You can also perform Remove on list2, however, that will modify list2.
Because you only check which numbers in list1 are missing in list2
But you need to check which numbers in list2 doesn't exist in list1 and in
listDif.
You can do
List<int> diff = new List<int>();
foreach (int num in list1)
{
if (!list2.Contains(num))
{
diff.Add(num);
}
}
foreach (int num in list2)
{
if (!list1.Contains(num) && !diff.contains(num))
{
diff.Add(num);
}
}

How to convert ASCII data to EBCDIC in C#?

I read about conversion of ASCII to EBCDIC using this link;
Convert String from ASCII to EBCDIC in Java?
But this is in java. My requirement is in C#.Net.
So can you please help me with this?
Thanks & Regards,
Krishna Kumar
Here's an implementation (by #Jon Skeet) you might find useful.
I tried the above code, and found it did not work for me.
For example, '04' (0x3034) ascii translated to 0x00f000f4 ebcdic. Problem seemed to be the encoding.
Played with several approches to change this, but finally found the best solution was the most basic. Instead of using Convert.ToChar, I plugged the hex value into the array.
Please see following:
static byte[] ASCIItoEBCDIC(string asciiString)
{
byte[] asciiToEbcdicTable = new byte[256] {
0x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F,0x16,0x05,0x25,0x0B,0x0C,0x0D,0x0E,0x0F,
0x10,0x11,0x12,0x13,0x3C,0x3D,0x32,0x26,0x18,0x19,0x3F,0x27,0x1C,0x1D,0x1E,0x1F,
0x40,0x5A,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,
0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,
0x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,
0xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xBA,0xE0,0xBB,0xB0,0x6D,
0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
0x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xC0,0x4F,0xD0,0xA1,0x07,
0x20,0x21,0x22,0x23,0x24,0x15,0x06,0x17,0x28,0x29,0x2A,0x2B,0x2C,0x09,0x0A,0x1B,
0x30,0x31,0x1A,0x33,0x34,0x35,0x36,0x08,0x38,0x39,0x3A,0x3B,0x04,0x14,0x3E,0xFF,
0x41,0xAA,0x4A,0xB1,0x9F,0xB2,0x6A,0xB5,0xBD,0xB4,0x9A,0x8A,0x5F,0xCA,0xAF,0xBC,
0x90,0x8F,0xEA,0xFA,0xBE,0xA0,0xB6,0xB3,0x9D,0xDA,0x9B,0x8B,0xB7,0xB8,0xB9,0xAB,
0x64,0x65,0x62,0x66,0x63,0x67,0x9E,0x68,0x74,0x71,0x72,0x73,0x78,0x75,0x76,0x77,
0xAC,0x69,0xED,0xEE,0xEB,0xEF,0xEC,0xBF,0x80,0xFD,0xFE,0xFB,0xFC,0xAD,0xAE,0x59,
0x44,0x45,0x42,0x46,0x43,0x47,0x9C,0x48,0x54,0x51,0x52,0x53,0x58,0x55,0x56,0x57,
0x8C,0x49,0xCD,0xCE,0xCB,0xCF,0xCC,0xE1,0x70,0xDD,0xDE,0xDB,0xDC,0x8D,0x8E,0xDF};
byte[] ebcdicBinary = new byte[asciiString.Length];
for (int pos = 0; pos < asciiString.Length; ++pos)
{
int ebcdicIndex = asciiString[pos];
ebcdicBinary[pos] = asciiToEbcdicTable[ebcdicIndex];
}
return ebcdicBinary;
}
Try like below code
public string ConvertASCIItoEBCDIC(string strASCIIString)
{
int[] a2e = new int[256]{
0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
64, 79,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
215,216,217,226,227,228,229,230,231,232,233, 74,224, 90, 95,109,
121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
151,152,153,162,163,164,165,166,167,168,169,192,106,208,161, 7,
32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,225,
65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
88, 89, 98, 99,100,101,102,103,104,105,112,113,114,115,116,117,
118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
};
char chrItem = Convert.ToChar("0");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strASCIIString.Length; i++)
{
try
{
chrItem = Convert.ToChar(strASCIIString.Substring(i, 1));
sb.Append(Convert.ToChar(a2e[(int)chrItem]));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return string.Empty;
}
}
string result = sb.ToString();
sb = null;
return result;
}
and check the saple code on these links
http://kseesharp.blogspot.com/2007/12/convert-ascii-to-ebcdic.html
http://forums.asp.net/t/167516.aspx
http://www.yoda.arachsys.com/csharp/ebcdic/
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/c2b074fd-4293-4bf4-b7fa-1803fc625d43

Categories

Resources