我正在學習 WCF,并在具有自己的 GetEnumerator() 方法的介面中制作了自定義類的 C# 小提琴來自定義類的 foreach 行為。類如圖所示:
產品類.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是一個有效的物件。為什么會這樣?我理解錯了嗎?
uj5u.com熱心網友回復:
我認為這應該是一個序列化問題,WCF 需要有具體的類來傳遞資料。
它不能回傳 IEnumerable - 嘗試使用 List(或 T[] 陣列)或具體型別。
您還可以使用服務跟蹤查看器來查找特定問題。
有類似問題的帖子
WCF Web 服務錯誤:“服務端點系結不使用 HTTP 協議”?
這可能是由于服務端點系結未使用 HTTP 協議
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/424801.html
上一篇:分裂特征的正確方法
