I've got a class which looks a little like this....
[DataContract]
public partial class Area : ModelBase
{
private String name;
private Guid floorId;
private Guid areaTypeId;
private int assetCount;
[DataMember]
public String Name
{
get { return name; }
set { name = value; }
}
[DataMember]
public Guid FloorId
{
get { return floorId; }
set { floorId = value; }
}
public Guid AreaTypeId
{
get { return areaTypeId; }
set { areaTypeId = value; }
}
}
....and I have a Wcf Service Library which has the following interface defined...
IEnumerable<Area> GetSomeStuff(IEnumerable<Area> uploadedAreas);
It's all working just fine, but in my client app (a compact framework application) the AreaTypeId property is exposed?
I thought that if I didn't add the [DataMember] attribute it wouldn't be seen by the client? What am not understanding???
Thanks,
ETFairfax
If you want to hide any property from client then just add [IgnoreDataMember] attribute to that property.
If you are sharing type assembly between client and server It will be visible unless you turn off reusing types from referenced assemblies (click "Configure Service Reference" on proxy).
If you are not sharing assembly it will not be visible because code for class is generated based on exposed contract (you can see it by turning on Show All Files in VS and then go to generated file Reference.cs under service proxy).
DataMember is attribute for DataContractSerializer so if you are sharing assembly the property will be not serialized on server and not initialized on client but it will be visible. Reference
Related
I'm trying to serialize enum values which potentially do not exist yet.
I have an existing project which has several enums in our datacontract for simplicity reason I display one like so:
public partial class TestDTO : ITestDTO
{
public DeleteMe DeleteMeEnum { get; set; }
}
[DataContract]
public enum DeleteMe
{
[EnumMember]
Deleted = 0,
}
Our application has a hidden internal wcf layer which our public web api accesses. A sample Service contract looks like so:
[ServiceContract]
public interface ITestService
{
[OperationContract]
TestDTO GetTestDTO();
}
public class TestService : ITestService
{
public TestDTO GetTestDTO()
{
return new TestDTO() { DeleteMeEnum = (DeleteMe)2 };
}
}
When I call this method from WebApi obviously I get the classic error:
Enum value '2' is invalid for type 'DeleteMe' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.
I can't go and change all of the enums now because we have a massive project, and replacing them would be too much, Also replacing all of our Service Contracts with a new Attibute would be too much.
Does anyone know of a way I can fix this globally, such as replacing the default XMLSerializer with a custom XMLSerializer?
There isn't a nice way to handle this once your application is released. However if you plan for the situation ahead of time, it can be handled.
So for the example above, you can do this:
public partial class TestDTO : ITestDTO
{
[DataMember(Name = "DeleteMeEnum")]
private string DeleteMeEnumString
{
get { return DeleteMeEnum.ToString(); }
set {
DeleteMe _enum;
if (!Enum.TryParse(value, out _enum))
{
_enum = <default value>;
}
DeleteMeEnum = _enum;
}
}
[IgnoreDataMember]
public DeleteMe DeleteMeEnum { get; set; }
}
I have a custom datatype I put in a class Library SharedTypes
namespace SharedTypes
{
public class District
{
public long Id { get; set; }
public string Name { get; set; }
}
}
I then have a WCF server with this service
using System.ServiceModel;
using SharedTypes;
namespace WCF.WCFInterfaces
{
[ServiceContract]
public interface IWcfService
{
[OperationContract]
District GetDistrict(long id);
[OperationContract]
void CreateDistrict(District district);
[OperationContract]
List<District> GetDistricts();
}
}
On the client side I have a Interface (So I inject the implementation)
using SharedTypes;
namespace WcfInterfaces
{
public interface IDistrictManager
{
void CreateDistrict(District district);
District GetDistrict(long id);
List<District> GetDistricts();
}
}
I finally have the implementation the client should use
public class DistrictManager : IDistrictManager
{
private readonly WcfServiceClient _salesService;
public DistrictManager()
{
_salesService = new WcfServiceClient();
}
public void CreateDistrict(District district)
{
_salesService.CreateDistrictAsync(district);
}
public District GetDistrict(long id)
{
return _salesService.GetDistrict(id);
}
public List<District> GetDistricts()
{
var list = _salesService.GetDistricts();
return list.ToList();
}
}
But here the problem arises, this implementation expects to use a version of District it gets from the service reference
WcfClientLibrary.SalesService.District
Instead of
SharedTypes.District
They are the same, but VS dont know that
So I get errors that the interface is not properly implemented because I have 2 different types of the District class.
How can I get the Service reference to use the SharedTypes.District instead? Or is it my way of implementing it that is way off?
Right click your service reference in client project and check "Reuse Types in Referenced Assemblies".
Be sure that you have added SharedTypes.District to your client service reference project.
When adding your WCF reference on the client side. Click on the advanced options.
There is a setting that you can specify to tell it to re-use types from specified assembly(s).
You'll be able to specify the assembly(s).
I got an Employee class and each employee has a list of applied leaves. Is it possible to have the list AppliedLeave as a [DataMember] in WCF?
[DataContract]
public class Employee
{
[DataMember]
public string UserID { get; set; }
[DataMember]
public int EmployeeNumber { get; set; }
[ForeignKey("EmployeeUserID")]
[DataMember]
public List<Leave> AppliedLeave
{
get { return _appliedLeaves; }
set { _appliedLeaves = value; }
}
private List<Leave> _appliedLeaves = new List<Leave>();
...
}
Is there any other way to do this?
thank you for your consideration of this matter
I extend my Question
This is my Leave Class:
[DataContract]
public class Leave
{
[Key()]
[DataMember]
public Guid LeaveId { get; set; }
[DataMember]
public string LeaveType { get; set; }
[DataMember]
public DateTime StartDate { get; set; }
[DataMember]
public string EmployeeUserID { get; set; }
}
this shows ServiceContract ---->
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
Employee GetEmployeeByUserId(string userId);
[OperationContract]
void AssignSupervisor(string userId, string supervisorUserId);
[OperationContract]
void DeleteEmployeeByUserId(string userId);
....
}
In Client application,
EmployeeServiceClient employeeService = new EmployeeServiceClient();
Employee employee = employeeService.GetEmployeeByUserId(id);
But when Employee gathered from the service its shows Null for leaves,
Can somebody help me? what have I done wrong here?
Yes, it is possible to return generics from WCF service operations.
But by default they are casted to Array on client side. This can be customized while proxy generation.
WCF: Serialization and Generics
Also you have to decorate the service with all the types to which generics can be resolved, using KnownTypeAttribute.
Known Types and the Generic Resolver
I also found my server side list would always arrive at the client as a null pointer. After browsing around a lot for this problem it strikes me it is nearly always denied at first ("your code should work")
Found the issue.. I had configured my solution using one "WCF Service" project and one "Winforms app" project with a generated service reference. Both interface and implementation of Service1 were in the WCF service project, as expected. But any list member returned null.
When I put my IService1.cs = the interface only = in a separate class library instead, reference the class library on both sides (using) and generate the service reference again, my list does work ! The generated code on the client side looks much simpler.
I did not need any special attributes, change service reference configuration, or interface references for this.
You could use IList<T> instead of List<T>.
I'm developing a desktop application with .NET. I'd like to save some data into a file in a way that would later give me some degree of freedom in changing the data I'm saving, such as adding new fields, while retaining the possibility to read saves from older formats.
This answer recommends to use DataContractSerializer instead of BinaryFormatter.
However I can't use the [DataContract] attribute on my classes in the project. After using System.Runtime.Serialization; I still get errors about unknown types.
The project targets .NET Framework 4.
I've learned that Data Contracts are part of the WCF framework, I assume I should somehow configure my project to use it. How?
In C# namespaces can be shared across multiple assemblies. You have to add a reference to System.Runtime.Serialization.dll, which contains [DataContract] attribute.
probably you are missing to specify the Know Type attribute when it is needed
Have a look at the below example:
public interface ICustomerInfo
{
string ReturnCustomerName();
}
[DataContract(Name = "Customer")]
public class CustomerTypeA : ICustomerInfo
{
public string ReturnCustomerName()
{
return "no name";
}
}
[DataContract(Name = "Customer")]
public class CustomerTypeB : ICustomerInfo
{
public string ReturnCustomerName()
{
return "no name";
}
}
[DataContract]
[KnownType(typeof(CustomerTypeB))]
public class PurchaseOrder
{
[DataMember]
ICustomerInfo buyer;
[DataMember]
int amount;
}
you have to specify the type of ICustomerInfo otherwise the serialization engine cannot guess the type
Just add wcf service template to your application and declare your function and data members their and reference wcf in your project.
It's an EntLib-Validator-issue again. I'm playing with EntLib 5.0 in C# and .Net 4.0 on XP pro.
I have some business objects (partial classes) generated by T4 templates. So I decided to put their validation attributes in buddy-classes by using MetadataTypeAttribute as definitely recommended by the documentation of entLib 5.0 (msdn).
But the Validator object I get from the ValidatorFactory doesn't know about the validation attributes, defined in the metadata-class.
The business object is defined like this:
[MetadataType(typeof(PatientMetadata))]
public partial class Patient
{
private string _Name;
private int _DiagnosisCount;
public int DiagnosisCount
{
get
{
return _DiagnosisCount;
}
set
{
if (value != _DiagnosisCount)
{
_DiagnosisCount = value;
}
}
}
public string Name
{
get
{
return _Name;
}
set
{
if (value != _Name)
{
_Name = value;
}
}
}
}
And the metadata class like this, according to documentation:
public class PatientMetadata
{
[RangeValidator(4)]
public int DiagnosisCount { get; set; }
[StringLengthValidator(64, ErrorMessage = "Name must not exceed 64 chars.")]
public string Name { get; set; }
}
If I know try to do validation this way:
var factory = ValidationFactory.DefaultCompositeValidatorFactory;
var validator = factory.CreateValidator<Patient>();
...then watching into validator (during debugging) already says, that it's just an AndCompositeValidator without any children validators.
Again, if I put the validation attributes right in the Patient class, it works perfectly.
By now, I have no real idea, what I'm missing here, since I think doing everything according to the docs.
Thanks in advance to you guys!
The property names of the metadata class must match the property names of the main class.
In your case your metadata class should look like:
public class PatientMetadata
{
[RangeValidator(0, RangeBoundaryType.Inclusive, 10, RangeBoundaryType.Ignore)]
public int DiagnosisCount { get; set; }
[StringLengthValidator(6, ErrorMessage = "Name must not exceed 6 chars.")]
public string Name { get; set; }
}
Also, the docs indicate the accepted approach is to declare all return types as object. However, the docs also talk about using properties but in their example use fields so take it under advisement. :)