我遇到了从wcf服务返回复杂类型的问题,并且觉得缺乏解决它的知识。问题是当尝试返回一个复杂类型(它包含其他复杂类型)时,wcf服务会制动连接。问题是wcf返回另一个复杂类型没有任何问题。 所以,这是它产生的例外:
System.ServiceModel.CommunicationException 的HResult = 0x80131501 Message =接收到http://localhost:52943/FivePlusService.svc的HTTP响应时发生错误。这可能是由于服务端点绑定不使用HTTP协议。这也可能是由于服务器中止HTTP请求上下文(可能是由于服务关闭)。请参阅服务器日志以获取更多详 来源= mscorlib程序 堆栈跟踪: 在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg) 在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 type) 在TestService.ServiceLocal.IFivePlusService.GetBoardByCodeTest(String boardCode) 在C:\ Users \ Gricla30 \ source \ repos \ FivePlus \ FivePlus \ TestService \ Connected Services \ ServiceLocal \ Reference.cs:line 1991中的TestService.ServiceLocal.FivePlusServiceClient.GetBoardByCodeTest(String boardCode) at TestService.Program.Main(String [] args)在C:\ Users \ Gricla30 \ source \ repos \ FivePlus \ FivePlus \ TestService \ Program.cs:第23行
内部异常1:WebException:底层连接已关闭: 接收时发生意外错误。
内部异常2:IOException:无法从传输中读取数据 连接:远程强制关闭现有连接 主机。
内部异常3:SocketException:现有连接是 被远程主机强行关闭
这是我的代码: 这是客户端,我称之为wcf方法
class Program
{
//static FivePlusService.FivePlusServiceClient client = new FivePlusService.FivePlusServiceClient();
static ServiceLocal.FivePlusServiceClient serviceLocal = new ServiceLocal.FivePlusServiceClient();
static void Main(string[] args)
{
ServiceLocal.Board bLocal = serviceLocal.GetBoardByCodeTest("01-01");
Console.WriteLine(bLocal.BoardId + " Price : " + bLocal.BoardPrice);
}
}
这是WCF方法(在界面中标记为[OperationContract]):
namespace FivePlusWcfService
{
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[ExceptionHandlerBehavior(typeof(ExceptionHandler))]
public class FivePlusService : IFivePlusService
{
IBllAdd adder = new BllAdder();
IBllSendEmail sender = new BllMailSender();
IBllReturnData returner = new BllReturner();
IBllVerify verifier = new BllVerifier();
IBllEdit editor = new BllEditor();
public Board GetBoardByCodeTest(string boardCode)
{
return returner.GetBoardByCode(boardCode);
}
}
}
现在进入服务器端:
namespace BllServiceToDal.Realization
{
public class BllReturner : IBllReturnData
{
IDalReturnData returner = new DalDataReturner();
public Board GetBoardByCode(string boardCode)
{
BoardsDB.DBTables.Board boardD = returner.GetBoardByCode(boardCode);
return Convertor.BoardDbToPoco(boardD);
}
}
}
!!!在测试的时候我发现这个方法RETURNS类型的Board对象没有任何问题。 下一个方法只是从DB获取数据,并不重要。 接下来可能有用的是Board class:
namespace POCOlib
{
[DataContract]
public enum Sides : byte { A = 1, B }
[DataContract]
public enum Types : byte { billboard = 1, pryzmatron, brandmauer }
[DataContract]
[KnownType(typeof(BoardAddress))]
[KnownType(typeof(BoardImage))]
[KnownType(typeof(Period))]
[KnownType(typeof(City))]
[KnownType(typeof(Street))]
public class Board
{
[DataMember]
public int BoardId { get; set; }
[DataMember]
public string BoardCode { get; set; }
[DataMember]
public Types BoardType { get; set; }
[DataMember]
public Sides BoardSide { get; set; }
[DataMember]
public virtual BoardAddress BoardAddress { get; set; }
[DataMember]
public string DorsCode { get; set; }
[DataMember]
public virtual BoardImage Img { get; set; }
[DataMember]
public decimal BoardPrice { get; set; }
[DataMember]
public bool IsAvailable { get; set; }
[DataMember]
public virtual Period OccupationPeriod { get; set; }
}
}
我试图做的事情: 1.将[Serializable]属性添加到Board类 2.在枚举中添加了0个元素(因为你可以看到它们从1开始) 3.删除所有复杂类型(地址,期间,图像) 4.在本地和主机上测试WCF 没有任何帮助((
有人可以帮我吗?
答案 0 :(得分:0)
[DataContract]
public enum Sides : byte { A = 1, B }
[DataContract]
public enum Types : byte { billboard = 1, pryzmatron, brandmauer }
首先它必须在您的班级董事会内。
下一步需要使用DataContract和EnumMember属性修饰枚举
namespace POCOlib
{
[DataContract]
[KnownType(typeof(BoardAddress))]
[KnownType(typeof(BoardImage))]
[KnownType(typeof(Period))]
[KnownType(typeof(City))]
[KnownType(typeof(Street))]
public class Board
{
[DataMember]
public int BoardId { get; set; }
[DataMember]
public string BoardCode { get; set; }
[DataMember]
public Types BoardType { get; set; }
[DataMember]
public Sides BoardSide { get; set; }
[DataMember]
public virtual BoardAddress BoardAddress { get; set; }
[DataMember]
public string DorsCode { get; set; }
[DataMember]
public virtual BoardImage Img { get; set; }
[DataMember]
public decimal BoardPrice { get; set; }
[DataMember]
public bool IsAvailable { get; set; }
[DataMember]
public virtual Period OccupationPeriod { get; set; }
[DataContract]
public enum Sides : byte
{
[EnumMember]
A = 1,
[EnumMember]
B
}
[DataContract]
public enum Types : byte
{
[EnumMember]
billboard = 1,
[EnumMember]
pryzmatron, brandmauer
}
}
}
然后将这些枚举类型添加到您的服务合同或类 Board 。
[ServiceKnownType(typeof(Sides))]
[ServiceKnownType(typeof(Types))]