Simple WCF Service [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I tried to write my First WCF service and here i have some problems,
First,I create a WCF Project,Then i added Entity Model.After that i added IEmpService.svc file.then i'm going to get a List of Customers.
I follow THIS BLOG POST
IEmpService
[ServiceContract]
public interface IEmpService
{
[OperationContract]
List<Customer> GetAllCustomers();
}
EmpService
public class EmpService : IEmpService
{
public EmpDBEntities dbent = new EmpDBEntities(); // I can't create thisone inside GetAllCustomer method.
public List<Customer> GetAllCustomers
{
//var x = from n in dbent.Customer select n; // This is what i need to get but in here this program not recognize `var` also.
//return x.ToList<Customer>();
}
}
Can anyope please tell me which point i'm missing ? or whythis problem happend? how to solve this ?

Not really sure what your question is, but did you define "Customer" as a DataContract? If that's the object that your service returns, you need to define it so the client can use it.

I am still confused by your question, but I will attempt an answer.
Your Customer class needs to have a DataContract with DataMembers if you want to return it.
You probably saw this example:
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
Also, do not return List. Microsoft has List defined, but this is a web service - the rest of the world (apple, android, linux, php, etc) will not know how to interpret a List.
Instead, change your function's signature to an array of strings.
[OperationContract]
string[] GetAllCustomers();

You should remove public keyword if you want to create this inside GetAllCustomer method. Like this:
public List<Customer> GetAllCustomers()
{
EmpDBEntities dbent = new EmpDBEntities();
var x = from n in dbent.Students select n;
return x.ToList<Student>();
}

Related

How can I deserialize a simple Json with square brackets in c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I don´t know what I´m doing wrong, but this is the Json I´m trying to read:
[{
"object":
{
"Weigh": 4000
}
}]
I really don´t know why I need the "object": part, but if I remove it, the code doesn´t work.
Here is my API Rest:
[HttpPost]
public string GetMixerTime([FromBody]JsonObject<WeighMix>[] json)
{
IList<WeighMix> listawm = JsonConvert.DeserializeObject<List<WeighMix>>(json.ToString());
return listawm.ToString();
}
WeighMix class:
public class WeighMix
{
public double Weigh { get; set; }
public WeighMix()
{
}
public WeighMix(double weigh)
{
Weigh = weigh;
}
}
Thanks a lot.
The Square Brackets signifies Arrays in Json. Your Json is an array of type (say) A, which has a single property called object of type B (B matches the definition of WeighMix).
You need to change your class declaration as
public class RootObject
{
[JsonProperty("object")]
public WeighMix Object{get;set;}
}
// Define other methods and classes here
public class WeighMix
{
public double Weigh { get; set; }
public WeighMix()
{
}
public WeighMix(double weigh)
{
Weigh = weigh;
}
}
You can now deserialize using
var result = JsonConvert.DeserializeObject<List<RootObject>>(json);
Sample Input
var json = #"[ { 'object': { 'weigh': 4000.0 }, 'json': '{\'Weigh\':4000.0}' } ]";
Sample Output

Using Generics to minimize repeated code C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have 3 classes Manager, TeamLead, Employee. These are generated from Database as I am using Entity framework. I can't do any changes here.
Class Manager()
{
string AddressLine1;
string AddressLine2;
string AddressLine3;
.
.
//Some other props
}
Class TeamLead()
{
string AddressLine1;
string AddressLine2;
string AddressLine3;
.
.
//Some other props
}
Class Employee()
{
string AddressLine1;
string AddressLine2;
string AddressLine3;
.
.
//Some other props
}
Now I another class in some other assembly,
Class AddressDetails
{
string AddressLine1;
string AddressLine2;
string AddressLine3;
}
And the current piece of code is,
Class WorkingPlace
{
Manager _manager;
TeamLead _teamLead;
Employee _employee;
// all the above obejects getting filled in various functions in this class
AddressDetails _addressDetails;
private SomeMethod1()
{
.
.
//doing some works
.
.
FillAddress("Manager");
}
private SomeMethod2()
{
.
.
//doing some works
.
.
FillAddress("TeamLead");
}
private FillAddress(string type)
{
if(type == "Manager")
{
_addressDetails.AddressLine1 = _manager.AddressLine1;
_addressDetails.AddressLine1 = _manager.AddressLine1;
_addressDetails.AddressLine1 = _manager.AddressLine1;
}
else if( type == "TeamLead")
{
_addressDetails.AddressLine1 = _teamLead.AddressLine1;
_addressDetails.AddressLine1 = _teamLead.AddressLine1;
_addressDetails.AddressLine1 = _teamLead.AddressLine1;
}
}
}
I want this to be,
FillAddress<T>(T typeObj)
{
_addressDetails.AddressLine1 = typeObj.AddressLine1;
_addressDetails.AddressLine1 = typeObj.AddressLine1;
_addressDetails.AddressLine1 = typeObj.AddressLine1;
}
//and Call it as,
FillAddress<Manager>(_manager);
Is it possible, I am new to Generics, I want to code effectively. Experts please help me. any other comments other than this let me know.
Not answering the question, but I see a need for the code to be refactored,
TO avoid repetitive code here,
Consider creating Address class an adding that as a reference in all the other classes
Class AddressDetails
{
string AddressLine1;
string AddressLine2;
string AddressLine3;
}
Then make your other classes like,
class Manager
{
AddressDetails address;
public Manager(AddressDetails address){
this.address=address;
}
}
class Employee
{
AddressDetails address;
public Manager(AddressDetails address){
this.address=address;
}
}
Even better, Use base class for all you Employees, as Manager and TeamLead all are employees and all have an address. Let others derive from it.
Now to answer your question, derive an interface from AddressDetails class and let your other classes implement it. Then create your generic method
FillAddress(IAddressable employee, Addresdetails newAddress)
{
employee.AddresssLine1=newAddress.AddressLine1;
..........
}
As i see your implementation, you may need to use some reflection. Assuming you have already instantiated _addressDetail, you could do something like this:
void FillAddress<T>(T value){
var typeInfo = typeOf(value);
_addressDetails.AddressLine1 = typeInfo.GetProperty("AddressLine1")?.GetValue(value) as string;
_addressDetails.AddressLine2 = typeInfo.GetProperty("AddressLine2")?.GetValue(value) as string;
_addressDetails.AddressLine3 = typeInfo.GetProperty("AddressLine3")?.GetValue(value) as string;
}
Here is a good MSDN article about generic methods. It should explain everything you need to know.
To answer your question:
Yes, that is possible.
The way you formatted your guess at a generic method is correct. Also, you can omit the <T> part, as described by the aforementioned article, as the compiler will infer the type from the arguments.

C# - Add node to serialized class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm very new to C# and please excuse me if i'm asking you stupid question.
I don't know how to add node to serialized class.
Here is my code:
namespace DuloGames.UI
{
[Serializable]
public class UISpellInfo
{
public int ID;
public string Name;
public Sprite Icon;
public string Description;
public float Range;
public float Cooldown;
public float CastTime;
public float PowerCost;
public float test;
[BitMask(typeof(UISpellInfo_Flags))]
public UISpellInfo_Flags Flags;
}
}
Here is how i try add new node to the serialized class above from another class:
using DuloGames.UI;
public class Character : MonoBehaviour
{
private void AddToNode()
{
UISpellInfo serializedNode = new UISpellInfo();
serializedNode.Add(new UISpellInfo()
{
ID = 1,
Name = "test",
Description = "description"
});
}
}
This is how i try to add new node to the serialized class but it seems i'm not doing it correctly. Can you please help me out ?
I'm not sure what your are trying to do but you UISpellInfo class is just a POCO that has no Add method right?
What are you trying to do.. I think you need to have a List of UISpellInfo object before you can add something to it?
List<UISpellInfo> nodes = new List<UISpellInfo>();
nodes.Add(new UISpellInfo{ID = 1, Name = "test", Description = "description"});
This should work i think.
But then again what will you do with serializedNode, i don't see you are doing anything with it?

Framework for handling serialization in "key:value;key:value;key:value;key:value;" format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Of course it's easy to write the code to deserialize from this format. I've already done it, but I don't like.
The single responsibility principle states that I should have a generic class that worries only about this kind of serialization. And the task is generic enough to be coped by a framework.
If you converted it to a JSON string like (which should be easy)
var jsonArray = “[{'key':'value'}, {'key':'value'}, {'key':'value'}, {'key':'value'}]”;
then you could easily deserialize it with Json.NET into whatever you want and Json.NET takes care of converting the values to the right types for you:
MyType1[] result = JsonConvert.Deserialize<MyType1[]>(jsonArray);
MyType2[] result = JsonConvert.Deserialize<MyType2[]>(jsonArray);
public class MyType1
{
public string key { get; set; }
public string value { get; set; }
}
public class MyType2
{
public string key { get; set; }
public double value { get; set; }
}
or even just as a dictionary (I hope I have the syntax correct, I didn't test it):
var jsonDic = “{{'key':'value'}, {'key':'value'}, {'key':'value'}, {'key':'value'}}”;
var result = JsonConvert.Deserialize<Dictionary<string, string>>(jsonDic);
The single responsibility class (just as an example):
public class KeyValueParser
{
public static TResult ParseKeyValueString<TResult>(string keyValueString)
{
keyValueString = ConvertToJson(keyValueString);
TResul result = JsonConvert.DeserializeObject<TResult>(keyValueString);
return result;
}
private static string ConvertToJson(string keyValueString)
{
// convert keyValueString to json
}
}
usage
var jsonDic = “{{'key':'value'}, {'key':'value'}, {'key':'value'}, {'key':'value'}}”;
var result = KeyValueParser.ParseKeyValueString<Dictionary<string, string>>(jsonDic);
I don't really understand the question.
If it is something your program does a lot then move the function to some area that it is easy to get too (or a nuget package if a lot of your systems need it). If it happens in one place in your code put it quite close to that place.

Making a list in the property [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a list like that.
bread.BreadAdd(new Bread("DF6", "8", 16, 500));
bread.BreadAdd(new Bread("ZER6D23", "5", 8, 1000));
As I understood, I need to create a code in the bread class, but furthermore I have no idea is has to be in the property? If so, where exactly?
edit: uups. I wrote it wrong. it has to be in a function.
Can be something like this:
using SCG = System.Collections.Generic;
public class Bread {
public class Bread(string name, string version, int foodValue, int weight) {
// ...
}
}
public class Breads {
private readonly SCG.List breads = new SCG.List();
public void AddBread(Bread bread) {
breads.Add(bread);
}
public SCG.IEnumerable Breads {
get { return breads; }
}
}
See below.. You can create a method to add it to a private List variable and then expose it via property or you can have a setter for BreadList property and call breadClass.BreadList.Add((new Bread("DF6", "8", 16, 500));. Either option should be fine.
public class BreadClass
{
private List<Bread> lstbread;
public List<Bread> BreadList
{
get
{
return bread;
}
}
public void BreadAdd(Bread bread)
{
lstbread.Add(bread)
}
}

Categories