在客户端和服务WCF之间丢失值

时间:2018-03-16 21:41:59

标签: c# visual-studio wcf

当我在客户端控制器中的对象上设置一些值,然后尽可能地逐步调试调试器时,我最终会在这里:

public class WCFServiceAuthenticateClientBehavior : IEndpointBehavior, IClientMessageInspector
{

    public WCFServiceAuthenticateClientBehavior(
        HttpRequestBase request,
        string authTokenAlias = "authToken",
        string ajaxTokenAlias = "ajaxToken")
    {

    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(this);
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        request.Headers.Add(...etc); // last call before service is here

        return null;
    }

}

如果我打开立即窗口,我可以打印request.toString()并看到XML具有我期望的值。例如,这个值:

<d4p1:AccrualRevenueRecognition>AccrualEvenly</d4p1:AccrualRevenueRecognition>

但是当我的Service方法的断点被命中时,检查该对象会显示AccrualRevenueRecognition没有反映XML中的这个值。

我的问题是: 我的服务中是否有一个地方可以在我的客户调用之前调试,以查看该值是否丢失?

FWIW,这只发生在枚举中,而AccrualRevenueRecognition是一个看起来像这样的枚举:

    [DataMember]
    [Column("accrual_revenue_recognition")]
    public byte AccrualRevenueRecognitionId
    {
        get
        {
            return (byte)this.AccrualRevenueRecognition;
        }
        set
        {
            AccrualRevenueRecognition = (AccrualRevenueRecognitionChoices) value;
        }
    }

    [DataMember]
    [EnumDataType(typeof(AccrualRevenueRecognitionChoices))]
    public AccrualRevenueRecognitionChoices AccrualRevenueRecognition { get; set; }

    [DataContract]
    [Flags]
    public enum AccrualRevenueRecognitionChoices
    {
        [EnumMember]
        NotSet = 0,
        [EnumMember]
        AccrualEvenly = 1,
        [EnumMember]
        AccrualAtInvoice = 2,
        [EnumMember]
        AccrualAtPayment = 3
    }

包含此枚举的类正在使用[DataContract, Serializable],并且从客户端调用的方法正在使用[VerificationSameOrigin]

1 个答案:

答案 0 :(得分:0)

我得到了这个工作,尽管它是一个hacky解决方案,我希望它能帮助其他人:

void

这是有效的,因为在我的课程中,我实际上是在DB中存储int:

var success = this.Client.MyServiceCall(
    new myObject
    {
        // Because WCF obliterates the enum, casting to byte for its respective ID is needed:
        AccrualRevenueRecognitionId = (byte)viewModel.QualityOfFinancials.AccrualRevenueRecognition,
        CashRevenueRecognitionId = (byte)viewModel.QualityOfFinancials.CashRevenueRecognition
    }
);