【发布时间】:2014-07-01 14:14:07
【问题描述】:
我有以下场景:
-
服务 Web 项目(Web 应用程序)
- ServiceA.svc
- ServiceB.svc
- ExceptionResponseMessage.cs
-
接口 Web 项目(Web 应用程序)
- 服务参考:
- ServiceAServiceReference
- ServiceBServiceReference
- 服务参考:
在两个服务(A 和 B)中,我都有返回 ExceptionResponseMessage 类的方法。在接口web项目中,我称之为“客户端”,为了方便参考,这个类生成了两次:
- ServiceAServiceReference.ExceptionResponseMessage
- ServiceBServiceReference.ExceptionResponseMessage
我想在我的客户端中创建一个函数来处理响应消息以查看是否有任何错误并正确处理。因为我有两个类的副本,所以我必须创建两个方法:
public static void HandleError(ServiceAServiceReference.ExceptionResponseMessage ex)
{
(Method Implementation here)
}
public static void HandleError(ServiceBServiceReference.ExceptionResponseMessage ex)
{
(Method Implementation here)
}
我想避免这种情况,所以我在不同的类库项目中创建了一个接口,服务和客户端项目都可以访问它。 ExceptionResponseMessage 实现了这个接口。
问题是客户端为“ExceptionResponseMessage”生成的类没有实现接口。我什至在接口和对象中创建了一个自引用的“InnerException”属性,如下所示:
public interface IExceptionResponseMessage {
IExceptionResponseMessage InnerException { get; set; }
}
public class ExceptionResponseMessage : IExceptionResponseMessage
{
public IExceptionResponseMessage InnerException { get; set; }
}
但是当我检查客户端生成的对象时,它说 InnerException 属性是 Object 类型,这表明该接口在客户端中没有得到尊重。
有谁知道如何让客户端类实现接口?
【问题讨论】:
-
客户端是否有包含接口和实现的程序集?如果没有,那么您必须在客户端上创建一个抽象。
-
客户端只有带有接口的程序集。该实现由服务参考自动生成。
-
对,但是如果你有实际的类程序集,它会使用 WCF...(我认为...)
-
否则你将不得不处理自定义类型。
-
问题是我的 ExceptionResponseMessage 继承自 ResponseMessage 主类。服务器可以使用此类及其所有后代,但有些类引用了服务项目中的类,我无法将其放入这个公共库中。