WCF Client Proxy object Creation Issue - wcf

I am using WCF service library where i return list. Then Create service reference in MVC-4 application. But Cant create proxy client object. Also i did configure service reference and set Collection type to System.Collections.Generic.List but still get problem.
Mu Code In Service Library is :
[ServiceContract]
public interface IStudent
{
// [WebGet(ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<Studentdata> GetData();
}
[DataContract()]
public class Studentdata
{
[DataMember()]
public string StudentId { get; set; }
[DataMember()]
public string FirstName { get; set; }
[DataMember()]
public string LastName { get; set; }
[DataMember()]
public Nullable<int> Age { get; set; }
[DataMember()]
public string Gender { get; set; }
[DataMember()]
public string Batch { get; set; }
[DataMember()]
public string Address { get; set; }
[DataMember()]
public string Class { get; set; }
[DataMember()]
public string School { get; set; }
[DataMember()]
public string Domicile { get; set; }
}
StudentDataAccess obj = new StudentDataAccess();
public List<Studentdata> GetData()
{
var query = obj.getalldata().ToList();
List<Studentdata> obj1 = query.ToList().ConvertAll(new Converter<BtDataLayer.Student, Studentdata>(Converter.ConvertEntStudentToWcfStudent));
return obj1;
}
StudentDataAccess obj = new StudentDataAccess();
is entity project class object
Please help me out for the same. Thanks in advance.

Related

Problem with mapping two objects (with lists)

I am looking for solution my issue... Probably my Shifts class cannot be mapped.
I have entity class Worker:
public class Worker
{
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(200)]
public string PhotoFilePath { get; set; }
public Workplace Workplace { get; set; }
public int WorkplaceId { get; set; }
public List<Service> Services { get; set; }
public List<Shift> Shifts { get; set; }
public IEnumerable<Worker> ToList()
{
throw new NotImplementedException();
}
}
And model WorkerModel:
public int Id { get; set; }
[Required]
[DisplayName("Imię")]
public string Name { get; set; }
[DisplayName("Nazwisko")]
public string LastName { get; set; }
[Display(Name = "Zdjęcie")]
public IFormFile Photo { get; set; }
public string PhotoFilePath { get; set; }
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int WorkplaceId { get; set; }
public List<ServiceModel> Services { get; set; }
public List<ShiftModel> Shifts { get; set; }
}
My default mapper profile:
//Mapping workers
CreateMap<Worker, WorkerModel>();
CreateMap<WorkerModel, Worker>();
And when I try map model to entity class in my action:
Worker worker = _mapper.Map<Worker>(model);
I get an issue:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
This is caused by different mapping types. Take the property Service as an example.
The resource is a type of Service.
But the destination is a type of ServiceModel.
So, they need to be converted. Here is a demo.
I create the Service and ServiceModel according to your model.
public class Service
{
public int serviceID { get; set; }
public string myservice { get; set; }
}
public class ServiceModel
{
public int serviceID { get; set; }
public string myservice { get; set; }
}
This is mapping relationship.
public class AutomapProfile : Profile
{
public AutomapProfile()
{
CreateMap<Worker, WorkerModel>();
CreateMap<WorkerModel, Worker>()
.ForMember(m => m.Services, x => x.MapFrom(y => y.Services.Select(a=>
new Service
{
serviceID=a.serviceID,
myservice=a.myservice
})));
}
}
This is the mapping method.
public IActionResult Index()
{
var model = new WorkerModel
{
Id=1,
Name="names",
//...
Services = new List<ServiceModel>
{
new ServiceModel{ serviceID=1, myservice="service1"},
new ServiceModel{ serviceID=2, myservice="service2"},
},
//...
};
Worker worker = _mapper.Map<Worker>(model);
return Ok(worker);
}
Result.

Can I put all my DataContract and DataMember on an interface and them implement that?

So, I've been looking at all kinds of examples of a WCF class that is to be serialized. I'd like to define all my DataContracts and DataMembers in interfaces and then allow implementation that suits the environment. Is this possible, and if so, are there drawbacks to this approach?
[DataContract]
public class Contact
{
[DataMember]
public int Roll { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public int Age { get; set; }
}
Can instead do this:
[DataContract]
public interface IContact
{
[DataMember]
int Roll { get; set; }
[DataMember]
string Name { get; set; }
[DataMember]
string Address { get; set; }
[DataMember]
int Age { get; set; }
}
public class Contact :IContact
{
public int Roll { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}

Not able to pass List<FilterData> using WCF with wsHttpBinging

I am not able to pass List using WCF with wsHttpBinging. List is a property of FilterResponse class.
Getting the following error.
-Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
//Following is the code.
[DataContract(Namespace = "Abc.Wao.Entity.Response")]
[CollectionDataContract]`
public class FilterResponse : Alcoa.Wao.Entity.Response.Response
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists"), DataMember]
public List<FilterData> FilterData { get; set; }
}
[KnownType(typeof(FilterResponse))]
[CollectionDataContract]
[DataContract(Namespace = "Abc.Wao.Entity.Response")]
public class Response
{
public Response()
{ }
[DataMember]
public string AuthToken { get; set; }
[DataMember]
public string Fault { get; set; }
[DataMember]
public Exception Exception { get; set; }
[DataMember]
public string SessionContext { get; set; }
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WaoService : IWaoService
{
public FilterResponse GetFilterDetails()
{
FilterResponse res = null;
//Call factory
res = Abc.Wao.Factory.CommonFactory.GetFilterDetails();
return res;
}
}
//------------------------------------------------------
[ServiceContract]
[ServiceKnownType(typeof(FilterResponse))]
[ServiceKnownType(typeof(Response))]
public interface IWaoService
{
[OperationContract]
FilterResponse GetFilterDetails();
}
Your property in the DataContract is missing a DataMember attribute:
[DataMember]
public List<FilterData> FilterData { get; set; }

WCF - Give Class DataContract during Compile or Runtime

If I have the code:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
How do I convert it to the code below at runtime or compile time?
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
Can I do this using DynamicProxy? Cecil? or some other open source project?

Can't correctly add associated objects into Entity Framework Context

I have and entity framework project exposed via a data service:
public class VersionContext : DbContext
{
public DbSet<VersionTreeEntry> VersionTreeEntries { get; set; }
public DbSet<PluginState> PluginStates { get; set; }
public static void SetForUpdates()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<VersionContext, Configuration>());
}
}
public class VersionTreeEntry
{
public VersionTreeEntry()
{
Children = new List<VersionTreeEntry>();
PluginStates = new List<PluginState>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public virtual ICollection<VersionTreeEntry> Children { get; set; }
public virtual ICollection<PluginState> PluginStates { get; set; }
public virtual VersionTreeEntry Ancestor { get; set; }
/// <summary>
/// Links to the ProtoBufDataItem Id for the session state.
/// </summary>
public int DataId { get; set; }
public string Notes { get; set; }
[Required]
public DateTime TimeStamp { get; set; }
[MinLength(1, ErrorMessage = "Tag cannot have a zero length")]
[MaxLength(20, ErrorMessage = "A tag name cannot contain over 20 characters")]
public string Tag { get; set; }
public bool IsUiNodeExpanded { get; set; }
[Required]
public string Version { get; set; }
[Required]
public string SessionName { get; set; }
}
public class PluginState
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
public string PluginName { get; set; }
[Required]
public byte[] Data { get; set; }
}
As far as I can see, the data classes are defined correctly. I try to create some new objects and add them into the context, with their relations intact:
var session = new Session();
session.SessionName = "My new session";
VersionTreeEntry versionTreeEntry = new VersionTreeEntry();
versionTreeEntry.SessionName = session.SessionName;
versionTreeEntry.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
versionTreeEntry.TimeStamp = DateTime.Now;
_versionContext.AddToVersionTreeEntries(versionTreeEntry);
foreach (var plugin in session.Plugins)
{
using (var ms = new MemoryStream())
{
plugin.SaveState(ms);
PluginState state = new PluginState();
state.PluginName = plugin.PluginName;
state.Data = ms.ToArray();
versionTreeEntry.PluginStates.Add(state);
}
}
_versionContext.SaveChanges();
The problem is that the PluginState instances never actually get added to the database. If I add code to add them manually to the context, they do get added, but the foreign key pointing back to the VersionTreeEntry is null.
Again, this is a WCF DataService rather than vanilla EF, any idea what might be wrong?
Cheers
Posting the answer here from the comment section.
Agreed. The best way to do this is to call the following API:
_versionContext.AddRelatedObject(versionTreeEntry, "PluginStates", state);
Thanks
Pratik