首页 文章

在客户端,WCF Operation Contract的返回值为null!有解决方案吗

提问于
浏览
2

我在我的WCF服务中有一个操作 Contract ,它返回一个实际上是消息 Contract 的类的实例 . (不是数据 Contract ) . 消息合约具有属性> [MessageBodyMember]的属性

[MessageContract(WrapperName="AuthorizarionResponse", IsWrapped="true")]
public class AuthorizationResponse
{
  [MessageBodyMember] public string role {get;set;};
  [MessageBodyMember] public Organization organization {get; set;};
}

[ServiceContract]
interface IAuthorization
{
    [OperationContract]
    public AuthoriztionResponse GetAuthorizationResult(AuthorizationRequestMessage request);          

}

Organization 类使用XmlSerializer . 它不使用DataContract,因为我希望从现有的ASMX客户端使用WCF服务 . 当我调试服务并在Operation Contract方法中看到返回值时,我可以看到我想通过此操作 Contract 从服务返回的所有内容 .

但在客户端,我得到空值!

一切都在没有任何异常/错误的情况下结束 . Fiddler2不会给出任何红色/错误标记!怎么会出错?

2 回答

  • 1

    问题是服务发送未按客户 Contract 代码预期形成的回复肥皂消息 . 序列化程序根据客户端应用程序中代理代码中定义的Contract解析Soap消息,但是,如果收到的soap消息不符合预期,则序列化程序会静默跳过内容并继续前进 . 因此,没有错误,也没有填充对象,因为序列化程序没有找到预期的内容 .

    因此,您需要确定客户端如何预期肥皂消息形成..

  • 3

    您可以按照here所述启用跟踪日志记录

    <system.diagnostics>
        <sources>
            <source     name="System.ServiceModel"
                        switchValue="Information, ActivityTracing"
                        propagateActivity="true">
                <listeners>
                    <add    name="traceListener"
                            type="System.Diagnostics.XmlWriterTraceListener"
                            initializeData= "c:\wcf.svclog" />
                </listeners>
            </source>
        </sources>
    </system.diagnostics>
    

    然后使用 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin 或其中一个兄弟文件夹中的 SvcTraceViewer.exe

相关问题