WCF xmlSerializer and Data Contract Attributes - wcf

I used XSD2Code to generate C# code from given XSD. This tool generated code snipped as below:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Orders
{
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
public int OrderID {get;set;}
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
public string Description {get;set;}
}
Coud some one please guide me with following queires, please
If I leave the above code as it is, does WCF serializes the above class?
Currently I'm getting this error on wcf test client: ""this Operation is not
supported in WCF Test Client".
Do I need to add DataContract and DataMember on top of the above generated codes?
Which option is beter between DataContract Serializer vs XML Serializer
Thanks you.

It should work, as long as the contract (either the service contract interface or the operation contract method which uses this type) is marked with the [XmlSerializerFormat] attribute
No - if you decorate the contract with [XmlSerializerFormat], the DataContract/DataMember attributes are ignored (the XML serialization attributes, like the ones in this type, are used instead)
The XmlSerializer allows you to completely control the XML in which the types are serialized; the DataContractSerializer (DCS) doesn't (it's more limited). But the DCS has a better performance, so which one is better really depends on your scenario.
The code below shows a service which uses this type, and it works fine, even with the WcfTestClient.
public class StackOverflow_7155154
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Orders
{
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
public int OrderID { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
public string Description { get; set; }
}
[ServiceContract]
[XmlSerializerFormat]
public interface ITest
{
[OperationContract]
Orders GetOrders();
}
public class Service : ITest
{
public Orders GetOrders()
{
return new Orders { Description = "My order", OrderID = 1 };
}
}
public static void Test()
{
//MemoryStream ms = new MemoryStream();
//XmlSerializer xs = new XmlSerializer(typeof(Orders));
//Orders o = new Orders { OrderID = 1, Description = "My order" };
//xs.Serialize(ms, o);
//Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
ITest proxy = factory.CreateChannel();
Console.WriteLine(proxy.GetOrders());
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}

Related

WCF Serialization Error Using NetTCP

Using VS2012 and NetTcpBinding. I am getting the following error when I call the serviceContract from the client - The service is hosted in IIS:
There was an error while trying to serialize parameter CS.ServiceContracts.Zzzzzz.Common:GetZipCodesResult. The InnerException message was 'Type 'System.DelegateSerializationHolder+DelegateEntry' with data contract name 'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.
Here is my ServiceContract:
[ServiceContract(SessionMode = SessionMode.Allowed, Namespace = "CS.ServiceContracts.Zzzzzz.Common",
Name = "IZzzzzzCommonService")]
public interface IZzzzzzCommonService
{
[OperationContract]
GetZipCodesResponse GetZipCodes(GetZipCodesRequest request);
}
Here is my DataContract:
[DataContract]
[Serializable]
public class GetZipCodesResponse : ResponseBase
{
[DataMember(IsRequired = true)]
public List<ZipCodes> ZipCodes { get; set; }
}
And here is the ResponseBase:
[DataContract]
[Serializable]
[KnownType(typeof(GetZipCodesResponse)), XmlInclude(typeof(GetZipCodesResponse))]
public class ResponseBase
{
[DataMember(IsRequired = true)]
public int ResponseCode { get; set; }
[DataMember(IsRequired = false)]
public int ReasonCode { get; set; }
[DataMember(IsRequired = false)]
public string ReasonText { get; set; }
}
Here is the Implementation of the serviceContract Interface:
public class ZzzzzzCommonService : IZzzzzzCommonService
{
public GetZipCodesResponse GetZipCodes(GetZipCodesRequest request)
{
var response = new GetZipCodesResponse();
try
{
response.ZipCodes = ZipCodes.GetCustom(request.ZipCode, request.City, request.State);
}
catch (Exception ex)
{
this.BuildExceptionResponse(response, ex);
}
return response; // I get to this line ok, but here is where the error occurs
}
}
And here is the client code where I am calling the Service:
public void ZipCodes()
{
var endPoint = new EndpointAddress(
"net.tcp://localhost/CS.WebService.Zzzzzz.Common/ZzzzzzCommonService.svc");
var binding = new NetTcpBinding { TransferMode = TransferMode.Buffered, SendTimeout = TimeSpan.MaxValue, ReceiveTimeout = TimeSpan.MaxValue, MaxReceivedMessageSize = 100000000, MaxBufferSize = 100000000 };
using (var channel = new ChannelFactory<IZzzzzzCommonService>(binding, endPoint))
{
try
{
channel.Endpoint.Contract.SessionMode = SessionMode.Allowed;
var proxy = channel.CreateChannel();
var request = new GetZipCodesRequest();
request = new GetZipCodesRequest { ZipCode = "32701" };
response = proxy.GetZipCodes(request);
}
}
}
I have debugged the code and I am getting into the entity and the dataSet is populated with the appropriate rows but when it sends the dataset back is when I get the errors.
Not sure what I am missing. I believe I have the serviceContract and dataContract members decorated correctly so not sure why it is having problems serializing the List.
The problem was the ZipCode class. It was an Entity class and it was not able to be serialized. So I deconstructed the class and it was able to send it back to the client.

Sending complex type as a parameter in SOAP message

I have a WCF Service like following:
public class Service1 : IService1
{
public string GetData(Person person)
{
if (person != null)
{
return "OK";
}
return "Not OK!";
}
Here is my Person class:
[DataContract]
public class Person
{
[DataMember]
public int Age { get; set; }
[DataMember]
public string Name { get; set; }
}
And I'm calling service like that:
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());
factory.Open();
EndpointAddress address = new EndpointAddress(url);
IRequestChannel irc = factory.CreateChannel(address);
using (irc as IDisposable)
{
irc.Open();
string soapMessage = "<GetData><person><Age>24</Age><Name>John</Name></person></GetData>";
XmlReader reader = XmlReader.Create(new StringReader(soapMessage));
Message m = Message.CreateMessage(MessageVersion.Soap11,"http://tempuri.org/IService1/GetData", reader);
Message ret = irc.Request(m);
reader.Close();
return ret.ToString();
}
When I try to send complex type like Person as a parameter to GetData method, person object is coming null. But I have no problem when I send known type like integer, string etc. as a parameter.
How can I manage to send complex type as a parameter to the service method?
I ran into a similar situation, and we ended up changing the interface of the service to be the equivalent of:
public string GetData(string person)
And we did our own object serialization before calling the web service. Immediately within the web service method we would deserialize it, and proceed as normal.

Gettinf Xml Serialization error when returning XMlDocument from WCF service

i have a WCF Service Method which returns XMlDocument i have added the attribut [XmlSerializerFormat]
on the method
Is there any way i can return XmlDocuemnt Object from WCf service
I could get the XmlDocument returned from my WCF Service in the following way.
My WCF service looks as shown below:
[ServiceContract]
[XmlSerializerFormat]
public interface ISampleService
{
[OperationContract]
Test GetXmlData();
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class SampleService : ISampleService
{
public string GetData()
{
return "Hello World";
}
public Test GetXmlData()
{
XmlDocument doc = new XmlDocument();
doc.Load(#"C:\SampleResponse.xml");
return new Test() {Doc = doc};
}
}
[Serializable]
public class Test
{
public XmlDocument Doc { get; set; }
}
The client adds a reference to the WCF Service and then calls the method GetXmlData() which returns a object Test which has the XmlDocuemnt within it.

passing collection or array type input parameter wcf service

I have written a WCf Service which has a Collection type input body parameter and another parameter as query string as following:
[WebInvoke(Method = "PUT", UriTemplate = "users/role/{userID}",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
public bool AssignUserRole(int userID,Collection<int> roleIDs)
{
//do something
return restult;
}
Now when I am trying to pass this parameter I am getting de serializing error. I have tried following format:
<AssignUserRole xmlns="http://tempuri.org/">
<roleIDs>
<roleID>7</roleID>
</roleIDs>
</AssignUserRole>
<AssignUserRole xmlns="http://tempuri.org/">
<ArrayOfroleID>
<roleID>7</roleID>
</ArrayOfroleID>
</AssignUserRole>
<AssignUserRole xmlns="http://tempuri.org/">
<ArrayOfint>
<int>7</int>
</ArrayOfint>
</AssignUserRole>
Can some one help me how can I pass this Array(Collection type Body parameter)?
Thanks.
The correct format would be this:
<AssignUserRole xmlns="http://tempuri.org/">
<roleIDs xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:int>7</a:int>
<a:int>8</a:int>
</roleIDs>
</AssignUserRole>
One easy way to find out what the expected format is for a certain operation is to use a WCF client with the same contract, send a message with it and look at the operation using Fiddler. The program below does that.
public class StackOverflow_6339286
{
[ServiceContract]
public interface ITest
{
[WebInvoke(Method = "PUT", UriTemplate = "users/role/{userID}", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
bool AssignUserRole(string userID, Collection<int> roleIDs);
}
public class Service : ITest
{
public bool AssignUserRole(string userID, Collection<int> roleIDs)
{
return true;
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
WebChannelFactory<ITest> factory = new WebChannelFactory<ITest>(new Uri(baseAddress));
ITest proxy = factory.CreateChannel();
proxy.AssignUserRole("1234", new Collection<int> { 1, 2, 3, 4 });
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
Also notice that there's a problem in your UriTemplate: the path variable {userId} cannot be of type int (it must be a string). This is fixed in the sample code above.
One more thing: if you don't want to use the default namespace for collections / arrays, you can use a [CollectionDataContract] class to change it. If instead of using Collection you used the class below, then the first body you tried should work:
[CollectionDataContract(Namespace = "http://tempuri.org/", ItemName = "roleID")]
public class MyCollection : Collection<int> { }

WCFFacility and WVF 4.0 REST

How do you use the Windsor-Castle WCFFacility with the WCF 4.0 REST services ?
How you you make the link to the factory, when you don't have the .svc file anymore?
TIA
Søren
Using Windsor 3.0 this is pretty straightforward (if I have understood your question correctly, my apologies if I am missing something).
The simplest thing to do to show you is to create a console application and make sure you are referencing:
Castle.Core
Castle.Windsor
Castle.Facilities.WcfIntegration
System.ServiceModel
System.ServiceModel.Web
System.Runtime.Serialization
Now define a RESTful service like this:
[DataContract]
public class Frob
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Fribble { get; set; }
}
[ServiceContract]
public interface IFrobService
{
[OperationContract]
[WebGet(UriTemplate = "/")]
IEnumerable<Frob> GetAllFrobs();
[OperationContract]
[WebGet(UriTemplate = "/{name}")]
Frob GetFrobByName(string name);
}
public class FrobService : IFrobService
{
private readonly List<Frob> _frobs
= new List<Frob>
{
new Frob {Name = "Foob", Fribble = "Soop"},
new Frob {Name = "Hoob", Fribble = "Soop"},
new Frob {Name = "Doob", Fribble = "Noop"}
};
public IEnumerable<Frob> GetAllFrobs()
{
return _frobs;
}
public Frob GetFrobByName(string name)
{
return _frobs
.FirstOrDefault(f =>
f.Name.Equals(name,
StringComparison.OrdinalIgnoreCase));
}
}
Now you have that you can hook up that service into the windsor container like so (and since it is a console application, I will just show you the main method):
public static class Program
{
static void Main()
{
var container = new WindsorContainer();
container
.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
.Register(Component.For<IFrobService>()
.ImplementedBy<FrobService>()
.AsWcfService(new RestServiceModel("http://localhost/frobs")));
Console.ReadKey();
}
}
And that is a WCF REST service hosted by Castle Windsor.
Pointing a browser at: "http://localhost/frobs" will get you all the frobs and pointing a browser at, say, "http://localhost/frobs/Doob" will get you the frob called Doob, you get the idea...