在 WCF 中实现 IEnumerable

Implementing IEnumerable in WCF

我正在学习 WCF,并在界面中创建了自定义 class 的 C# fiddle,它有自己的 GetEnumerator() 方法来自定义 class' foreach 行为. class如图:

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();
        }
    }
}

合同如图所示(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);
    }
}

GetProduct()方法的实现如图:

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();
            }
        }
    }
}

客户端调用时:

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();
        }
    }
}

它在 var searchResult = proxy.GetProduct(input); 处失败,例外情况是

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.'

我做了一些试验和错误,如果我删除 IEnumerable 和 foreach 循环,就没有问题,searchResult 是一个有效的对象。为什么会这样?我理解错了吗?

我觉得应该是序列化的问题,WCF需要有具体的类来传递数据。
它不能 return IEnumerable - 尝试使用列表(或 T[] 数组)或具体类型。

您还可以使用 Service Trace Viewer 查找具体问题。

有类似问题的帖子