如何return 来自WCF 服务应用程序方法的不同类型的响应?

How to return different types of responses from WCF service application method?

我有一个带有方法的 WCF 服务应用程序

public PersonResponse(PersonRequest request)
{
     return new PersonResponse { Id = 1, Name = "Mark" };
}

我对该方法有 2 种不同的反应

    [DataContract]
    public class PersonResponse 
    {
        int id;
        string name;

        [DataMember]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

    [DataContract]
    public class ErrorResponse
    {
        int errorCode;
        string errorText;

        [DataMember]
        public int? ErrorCode
        {
            get { return errorCode; }
            set { errorCode = value; }
        }

        [DataMember]
        public string ErrorText
        {
            get { return errorText; }
            set { errorText = value; }
        }
    }

是否可以编写一种方法来 return 这些响应中的任何一个,但不能同时响应两者?

我可以做类似的事情

    [DataContract]
    public class Response
    {
        PersonResponse personResponse;
        ErrorResponse errorResponse;

        [DataMember]
        public bool PersonResponse 
        {
            get { return personResponse; }
            set { personResponse= value; }
        }

        [DataMember]
        public string ErrorResponse 
        {
            get { return errorResponse; }
            set { errorResponse= value; }
        }
    }

public Response(PersonRequest request)
{
     return new Response{ PersonResponse = New PersonResponse { Id = 1, Name = "Mark" }, ErrorResponse = new ErrorResponse { ErrorCode = null, ErrorText = null } };
}

但我只需要 return 编辑一种类型的响应,而不是两种。

我试过了

public PersonResponse(PersonRequest request)
{
     throw new WebFaultException<ErrorResponse>(new ErrorResponse { ErrorCode = 103 ErrorText = "Try again later" }, HttpStatusCode.Conflict);
}

但似乎这种方式只是 return 异常而不是 ErrorResponse。

我也尝试像这样向响应添加接口层,然后 return将该接口作为响应

    public interface IResponse {}

    [DataContract]
    public class PersonResponse : IResponse
    {
        int id;
        string name;

        [DataMember]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

    [DataContract]
    public class ErrorResponse : IResponse
    {
        int errorCode;
        string errorText;

        [DataMember]
        public int? ErrorCode
        {
            get { return errorCode; }
            set { errorCode = value; }
        }

        [DataMember]
        public string ErrorText
        {
            get { return errorText; }
            set { errorText = value; }
        }
    }

public IResponse(PersonRequest request)
{
     return new PersonResponse { Id = 1, Name = "Mark" };
}

但是,当我将此服务添加为对另一个项目 ID 的引用时,不会生成任何响应类型(Iresponse、ErrorResonse 或 PersonResponse),并且在我尝试调用该方法时抛出错误(异常消息 https://ibb.co/Zdsbr9p)

我觉得你可以试试FaultContractAttribute。错误协定允许您指定将在 SOAP 错误中返回的备用响应。
示例接口:

[ServiceContract]
    interface IService
    {
        [OperationContract]
        [FaultContract(typeof(ErrorResponse))]
         PersonResponse  GetResponse();
    }

服务:

class Service : IService
{
    public PersonResponse GetResponse()
    {
        if (success)
        {
            return new PersonResponse();
        }
        else
        {
            throw new FaultException<ErrorResponse>(new ErrorResponse() 
            {
                ErrorMessage = "Something Happened"
            })
        }
    }
} 

然后客户端可以通过捕获FaultException<ErrorResponse>:

来处理故障
var serviceProxy = new ServiceProxy();

try 
{
    var dataObj = serviceProxy.GetResponse();
}
catch (FaultException<ErrorResponse> error)
{
    ErrorResponse detail = error.Detail;
    Console.WriteLine(detail.ErrorMessage);
}

来源:How best should a Wcf service return different objects for the same method
相似度: