In a separate project/solution I have a class named CompleteField that is referenced in multiple WCF projects/solutions.
The WCF generates a partial class from CompleteField which is not desired.
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
[System.Runtime.Serialization.DataContractAttribute(Name="CompleteField", Namespace="http://schemas.datacontract.org/2004/07/Common.Web.Models")]
public partial class CompleteField : object
{
//...
}
Using the attribute KnowType I thought I would be able to omit this behavior (since I have done this in the past).
Below my CompleteField class
[DataContract]
[KnownType(typeof(ValidatorType))]
[KnownType(typeof(ObjectType))]
[KnownType(typeof(ICompleteField))]
public class CompleteField : ICompleteField
{
[DataMember]
public string Name { get; set; }
[DataMember]
public ObjectType Type { get; set; }
}
public interface ICompleteField
{
string Name { get; set; }
ObjectType Type { get; set; }
}
[DataContract]
public enum ValidatorType
{
[EnumMember]
//...
}
[DataContract]
public enum ObjectType
{
[EnumMember]
//...
}
public CompleteField(ObjectType type)
{
this.Type = type;
}
}
Does anyone know why the WCF still generates the partial class this?
The partial class is generated because I used a Constructor in CompleteField.
Related
I am using protobuf-net version 2.3.2.0.
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
[ProtoInclude(12, typeof(SubClass))]
public class BaseClass
{
public string Id { get; set; }
public string Name { get; set; }
}
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class SubClass : BaseClass
{
public string PropertySub { get; set; }
}
As my base class is inherited from many child classes.
How can I avoid placing attribute [ProtoInclude] on my base classes so that props in my base class deserialize as expected.
Here is EF Models class, Auto generate.
namespace MySys.Models
{
using System;
using System.Collections.Generic;
public partial class CustomerInfo
{
public System.Guid CustomerInfoID { get; set; }
}
}
Here is My partial class
public partial class CustomerInfo
{
public string CustomerType { get; set; }
public Nullable<System.Guid> OperatorUserID { get; set; }
}
But When I used this code to get list, the value of CustomerType is null.
List<CustomerInfo> CustomerInfoList = new List<CustomerInfo>(db.Database.SqlQuery<CustomerInfo>("EXEC usp_GetCustomerInfoList #PageSize,#PageIndex,#WhereStr,#OrderbyStr,#TotalRecord output", param_pagesize, param_pageindex, param_wherestring, param_orderstr, param_totalrecord));
Anyone can tell me how to do it correct?
The two files is in the same assemblies.
Thanks.
The partial class is used to extend the entity type and those properties will be ignored during query.
You can try creating a derived type instead.
public partial class CustomerInfo
{
public string CustomerType { get; set; }
public Nullable<System.Guid> OperatorUserID { get; set; }
}
public class CustomCustomerInfo : CustomerInfo {}
And map the query to the derived type.
SqlQuery<CustomCustomerInfo>("EXEC usp_GetCustomerInfoList ...")
I have the following partial class, which provides Metadata to my Database first models.
namespace Model.Metadata.Routing
{
[MetadataType(typeof(RoutingMetadata))]
public partial class Routing
{
}
public partial class RoutingMetadata
{
[DefaultValue("%")]
public string Slot { get; set; }
[Required(ErrorMessage = "This field is requied")]
[DefaultValue(0)]
public int BlockStart { get; set; }
[Required(ErrorMessage = "This field is requied")]
[DefaultValue(499)]
public int BlockEnd { get; set; }
[DefaultValue(-1)]
}
}
Now I want to add a constructor for the Routing Class to default my values,
public Routing()
{
Slot="%";
}
Where do I add the constructor?
[EDIT]
Other half of partial Class Routing
public partial class Routing
{
public string Slot { get; set; }
public int BlockStart { get; set; }
public int BlockEnd { get; set; }
}
You can put the constructor in any one of the partial class definitions, it's up to you where you think it makes the most logical sense.
Of course, all parts of a partial class definition need to have the same class name and namespace, or they are different classes. Routing and RadioRouting are not the same class, because they don't have the same name.
i have two data classes which hold only data members(no functions). One is CallTask the other is SmsTask. These two classes have some common properties like ID, Tel. I put these common properties in a seperate interface class and i use this interface class in my project whenever appropriate.
Now i added a WCFService to my project to share data between clients and server. Consider the following class design:
public interface IGsmTask : IComparable
{
string TaskID { get; set; }
string SessionID { get; set; }
string Tel { get; set; }
}
class CallTask : IGsmTask
{
#region IGsmTask Members
public string TaskID { get; set; }
public string SessionID { get; set; }
public string Tel { get; set; }
#endregion
}
class SmsTask : IGsmTask
{
#region IGsmTask Members
public string TaskID { get; set; }
public string SessionID { get; set; }
public string Tel { get; set; }
#endregion
public string SmsText { get; set; }
}
in this design, i want to host CallTask, SmsTask, and IGsmTask to the clients to use these in service methots like the following;
[OperationContract]
public void AddTask(IGsmTask task)
{
}
i tried to mark [DataContract] on IGsmTask but it gives me complition error. Isnt there any methot that i can use interfaces as DataContracts? Or how should i use KnownAttributes types in this synerio?
Thanks.
As far as I know using interfaces as datacontracts is not possible. You may use a base class and add knowntype attributes on the otherhand.
Fer: Everything is Possible with the right design.
If the issue is:
a class is a data contract
&&
1 or more of its properties must be an interface...
public interface ICustomInterface
{
int Property1 {get;set}
}
[DataContract]
public class MyClass
{
[DataMember(Name="_myInterface")]
public ICustomInterface MyInterface {get;set;}
}
The issue is that when the de-serialization occurs --
There is no way to turn the data into a class that implements ICustomInterface.
The Solution is to create a concrete class that does Implement the interface, and cast the getter/setter of the public property (that is of type interface) into a private property of the concrete class.
public class CustomInterfaceImplementor: ICustomInterface
{
public int Property1 {get;set;}
}
[DataContract]
public class MyClass
{
[DataMember(Name="_myInterface")]
private CustomInterfaceImplementor _MyInterface;
public ICustomInterface MyInterface
{
get {return (_MyInterface as ICustomInterface);}
set {_MyInterface = (value as CustomInterfaceImplementor);}
}
}
I have a base class with a few properties:
// must include any derived classes here as known types or else they will throw errors on serialization
[KnownType(typeof(CollaborationEventMeasureDistance))]
[DataContract]
public partial class CollaborationEvent
{
public bool HasBeenTransported { get; set; }
public Guid MessageBoxGuid { get; set; }
public CollaborationEvent()
{
HasBeenTransported = false;
}
}
And a derived class with some properties of its own:
public class CollaborationEventMeasureDistance : CollaborationEvent
{
public Geometry Geometry { get; set; }
}
When I serialize the derived class, all of its properties are serialized, but the properties it inherits from the base class are not:
<CollaborationEvent i:type="CollaborationEventMeasureDistance">
<Geometry xmlns:d4p1="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry"
i:type="d4p1:Polyline">
<d4p1:spatialReference>
<d4p1:wkid>26910</d4p1:wkid>
</d4p1:spatialReference>
<d4p1:paths>
<d4p1:points>
<d4p1:point>
<d4p1:spatialReference>
<d4p1:wkid>26910</d4p1:wkid>
</d4p1:spatialReference>
<d4p1:x>460892.23924271885</d4p1:x>
<d4p1:y>5367682.5572773879</d4p1:y>
</d4p1:point>
<d4p1:point>
<d4p1:spatialReference i:nil="true" />
<d4p1:x>461001.35841108358</d4p1:x>
<d4p1:y>5367648.5755294543</d4p1:y>
</d4p1:point>
</d4p1:points>
</d4p1:paths>
</Geometry>
</CollaborationEvent>
Can anyone point out what I am doing wrong?
I expect my XML to look more like:
<CollaborationEvent i:type="CollaborationEventMeasureDistance">
<HasBeenTransported>True</HasBeenTransported>
<MessageBoxGuid>blah</MessageBoxGuid>
<Geometry xmlns:d4p1="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry"
i:type="d4p1:Polyline">
<d4p1:spatialReference>
<d4p1:wkid>26910</d4p1:wkid>
</d4p1:spatialReference>
<d4p1:paths>
<d4p1:points>
<d4p1:point>
<d4p1:spatialReference>
<d4p1:wkid>26910</d4p1:wkid>
</d4p1:spatialReference>
<d4p1:x>460892.23924271885</d4p1:x>
<d4p1:y>5367682.5572773879</d4p1:y>
</d4p1:point>
<d4p1:point>
<d4p1:spatialReference i:nil="true" />
<d4p1:x>461001.35841108358</d4p1:x>
<d4p1:y>5367648.5755294543</d4p1:y>
</d4p1:point>
</d4p1:points>
</d4p1:paths>
</Geometry>
</CollaborationEvent>
Thanks
Assuming your Geometry class is serializable, try something like this:
[DataContract, Serializable]
public class CollaborationEventMeasureDistance : CollaborationEvent
{
[DataMember]
public Geometry Geometry { get; set; }
}
[KnownType(typeof(CollaborationEventMeasureDistance))]
[DataContract, Serializable]
public partial class CollaborationEvent
{
[DataMember]
public bool HasBeenTransported { get; set; }
[DataMember]
public Guid MessageBoxGuid { get; set; }
public CollaborationEvent()
{
HasBeenTransported = false;
}
}