I'm learning about WCF, and made a C# fiddle of custom class in the interface that has its own GetEnumerator() method to customize the class' foreach behaviour. The class is as shown:
ProductClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ProductInterfaces
{
public class ProductData : IEnumerable
{
public string Name { get; set; }
public string ProductNumber { get; set; }
public string Color { get; set; }
public double ListPrice { get; set; }
List<string> myData = new List<string>(new string[] { "test1", "test2", "test3", "test4" });
public IEnumerator<string> GetEnumerator()
{
foreach(string val in myData)
{
yield return val;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
And the contract is as shown (IWCFProductService.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ProductInterfaces
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IWCFProductService" in both code and config file together.
[ServiceContract]
public interface IWCFProductService
{
[OperationContract]
ProductData GetProduct(string productNumber);
}
}
The implementation of the GetProduct() method is as shown:
using ProductInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ProductService
{
public class WCFProductService : IWCFProductService
{
public ProductData GetProduct(string productNumber)
{
using (adventureworksEntities database = new adventureworksEntities())
{
var table_of_products = database.products;
ProductData desired_product = new ProductData();
foreach (var p in table_of_products)
{
if (p.ProductNumber == productNumber)
{
Console.WriteLine("Test using a custom foreach of ProductData class");
foreach (string i in desired_product)
{
Console.WriteLine(i);
}
desired_product.Name = p.Name;
desired_product.ProductNumber = p.ProductNumber;
desired_product.Color = p.Color;
desired_product.ListPrice = p.ListPrice;
return desired_product;
}
}
throw new Exception();
}
}
}
}
When called by the client:
using ProductInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace ProductClient
{
class Program
{
static void Main(string[] args)
{
// Client creates channel factory by passing in the name of the endpoint (i.e. ProductServiceEndpoint)
ChannelFactory<IWCFProductService> channelFactory = new ChannelFactory<IWCFProductService>("ProductServiceEndpoint");
// Create a proxy i.e. create a channel
IWCFProductService proxy = channelFactory.CreateChannel();
Console.WriteLine("Input product name to be searched:");
string input = Console.ReadLine();
var searchResult = proxy.GetProduct(input);
Console.WriteLine(searchResult.Name);
Console.ReadLine();
}
}
}
It fails at var searchResult = proxy.GetProduct(input);
, with exception saying
System.ServiceModel.CommunicationException: 'An error occurred while receiving the HTTP response to http://localhost:9999/ProductService. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.'
I did some trial and error, and if I remove IEnumerable and the foreach loop, there is no issue and searchResult
is a valid object. Why is this happening? Am I understanding something wrong?
CodePudding user response:
I think it should be a serialization issue, WCF needs to have concrete classes to pass data.
It cannot return IEnumerable - try using List (or T[] array) or concrete types.
You can also use the Service Trace Viewer to find specific issues.
Posts with similar questions
WCF Web Service error: "Service endpoint binding not using HTTP protocol"?
This could be due to the service endpoint binding not using the HTTP protocol