I have a WCF service with a service interface
[ServiceContract]
public interface IMyService
{
[OperationContract]
[ServiceKnownType(typeof(Person))]
[ServiceKnownType(typeof(Employee))]
IPerson GetPerson();
}
and my Implementation of GetPerson is
public IPerson GetPerson()
{
IPerson obj = new Person();
obj.FirstName = "Bhuvan";
obj.LastName = "Ram";
return obj;
}
And in my client as simple I used
KnownType.MyServiceClient obj = new KnownType.MyServiceClient();
Person objp = (Person)obj.GetPerson()'
But when I am trying to access, I am receiving an error
The underlying connection was closed: The connection was closed unexpectedly.
and my stack trace is like this
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)..
My Employee Class is
>
[DataContract(Name = "Employee")]
[KnownType(typeof(IEmployee))]
public class Employee:IEmployee
{
[DataMember]
public string EmployeeName
{
get;set;
}
}
>
My Person Class is
[DataContract(Name = "Person")]
[KnownType(typeof(IPerson))]
public class Person: IPerson
{
#region IPerson Members
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
#endregion
}
How do I resolve this?
There might be a serialization issue on Concrete class Person.
[ServiceContract]
public interface IMyService
{
[OperationContract]
IPerson GetPerson();
}
public interface IPerson
{
string FirstName { get; set; }
string LastName { get; set; }
}
[DataContract(Name = "Person")]
[KnownType(typeof(IPerson))]
public class Person : IPerson
{
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
}
Can use ServiceKnownType attribute on the method signature, rather than using the KnownType attribute on the whole class
[OperationContract]
[ServiceKnownType(typeof(Person))]
IPerson GetPerson();
Source: Check this link for detail
Ref : Using Knowntype
Note:
Use Knowntype everywhere or ServiceKnowntype on ServiceContract only.
Try with unique namespace to DataContract of Person and Employee classes.
Related
I have a simple entity, and one of its property is required:
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember (IsRequired=true)]
public string LastName { get; set; }
}
This is the interface of the service:
[ServiceContract]
public interface IService1
{
[OperationContract]
Person DoubleLastName(Person person);
}
and this is the service:
public class Service1 : IService1
{
public Person DoubleLastName (Person person)
{
return new Person { FirstName = person.LastName, LastName =
person.LastName};
}
}
And here is the problem: When the client sends an object to this service,
without the required property, everything works.
Shouldn't I get an exception?
using (Service1Client myProxy = new Service1Client())
{
Person person1 = new Person { }; //Here I don't notify the required value.
Person person = myProxy.DoubleLastName(person1);
}
This is because DataMemberAttribute.IsRequired applies to .NET Framework 3.0:
For more information about it, you can refer to this link.
I tried to use .NET Framework3.0 for testing and got an exception:
I am working on a wcf webservice. This service uses a third party webservice which I have added as a service reference.
Now I want to publish some properties of this proxyclient to clients who uses my wcfservice, without defining an own class and doing the mapping.
The auto generated code is done as partial class.
public partial class Person : object,
System.ComponentModel.INotifyPropertyChanged
{
public string FirstName;
public string LastName;
...
}
I tried to override these properties by using the MetadataType-Attribute and adding the [DataMember]-Attribute to properties. But this seams to work only for EF.
[DataContract]
[MetadataType(typeof(PersonMetaData))]
public partial class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
public class PersonMetaData
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
Trying to implement an interface didn't help, the properties are invisible on client.
[DataContract]
public partial class Person : IPerson
{}
public interface IPerson
{
[DataMember]
string FirstName { get; set; }
[DataMember]
string LastName { get; set; }
}
Any idea?
Guido
On my service XmlSerializer failed to serialize the auto-generated class cause of PropertyChanged-event.
If I work with DataContractSerializer and decorate my auto-generated class with [DataContract], I'm not able to decorate the properties by inheritance with [DataMember] because the attibute is not inheritable.
So I extended this partial class by wrapper properties.
[DataContract]
public partial class Person
{
[DataMember]
public string FirstNameWrapper
{
get
{
return this.FirstName;
}
set
{
this.FirstName = value;
}
}
}
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.
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; }
I have a type MyParameter that i pass as a parameter to a wcf service
[Serializable]
public class MyParameter : IXmlSerializable
{
public string Name { get; set; }
public string Value { get; set; }
public string Mytype { get; set; }
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XElement e = XElement.Parse(reader.ReadOuterXml());
IEnumerable<XElement> i = e.Elements();
List<XElement> l = new List<XElement>(i);
Name = l[0].Name.ToString();
Value = l[0].Value.ToString();
Mytype = l[0].Attribute("type").Value.ToString();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteStartElement(Name);
writer.WriteAttributeString("xsi:type", Mytype);
writer.WriteValue(Value);
writer.WriteEndElement();
}
#endregion
}
The service contract looks like this:
[ServiceContract]
public interface IOperation
{
[OperationContract]
void Operation(List<Data> list);
}
Where data defines a data contract
[DataContract]
public class Data
{
public string Name { get; set; }
public List<MyParameter> Parameters{ get; set; }
}
When I run the service and test it
I get rhe exception in readXml of MyParameter
"the prefix xsi is not defined"
xsi should define the namespace "http://w3.org/2001/xmlschema-instance"
How do I fix the problem
I am very new to this so a sample code will be very very very helpful
thanks
Add:
writer.WriteAttributeString("xmlns","xsi", null,#"http://w3.org/2001/xmlschema-instance");