'WCF pass object from Windows Service to asp.Net application
today I'm trying to pass object with some members via a WCF endpoint I've got two classes :
Person class:
{...}
[DataContract]
public class Person
{
[DataMember]
public string Firstname { get; set; }
[DataMember]
public string Lastname { get; set; }
}
Population class:
{...}
[DataContract]
public class Population
{
[DataMember]
public string Name { get; set; }
[DataMember]
public List<Person> Persons { get; set; }
}
I've got a service like this to serve the endpoint from Windows Service :
{...}
[ServiceContract]
public interface IService
{
[OperationContract]
Population GetPopulation();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
public Population GetPopulation()
{
List<Person> persons = new List<Person>();
Person person1 = new Person() { Firstname = "Cameron", Lastname = "Randall" };
Person person2 = new Person() { Firstname = "Francesca", Lastname = "Horton" };
persons.Add(person1);
persons.Add(person2);
Population population = new Population();
population.Name = "Name of population";
population.Persons = persons;
return population;
}
}
The endpoint is created like this:
{...}
protected override void OnStart(string[] args)
{
var address = "net.tcp://localhost:6565/GetElements";
var uris = new Uri[1];
uris[0] = new Uri(address);
IService service = new Service();
ServiceHost serviceHost = new ServiceHost(service, uris);
var binding = new NetTcpBinding(SecurityMode.None);
serviceHost.AddServiceEndpoint(typeof(IService), binding, "");
serviceHost.Open();
}
I created a console app to test my program, like this :
{...}
internal class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
var address = "net.tcp://localhost:6565/GetElements";
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
var endpoint = new EndpointAddress(address);
var channel = new ChannelFactory<IService>(binding, endpoint);
var proxy = channel.CreateChannel();
var result = proxy?.GetPopulation();
if (result != null)
result.Persons.ForEach(p => Console.WriteLine(p.FirstName));
Console.WriteLine("Press any key to finish.");
Console.ReadKey();
}
}
The problem is: When I use the console application, all works fine, but I'm trying to use it from a IIS application in asp.Net and when the response come, the result is a Population object with all members null like this :
Name = null;
Persons = null;
I don't know why... I must do something in my asp.Net application ?
This is the code that I use to query my method :
public void Execute()
{
var address = "net.tcp://localhost:6565/GetElements";
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
var endpoint = new EndpointAddress(address);
var channel = new ChannelFactory<IService>(binding, endpoint);
var proxy = channel.CreateChannel();
var result = proxy?.GetPopulation();
if (result != null)
{
result.Persons.ToList<Person>().ForEach(person => Console.WriteLine($"Firstname: {person.Firstname} | Lastname: {person.Lastname}"));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
