首页 > 解决方案 > WCF 响应不是映射总是返回 null

问题描述

我有以下 WSDL 文件并使用 Visual Studio 创建了 WCF 连接服务。它创建了 Reference.cs 文件(附在下面的 WSDL 文件内容之后)。当我厌倦了调用带有请求正文的服务时,它击中了服务器,但响应没有映射到 WCF 端,我在soap UI 中尝试了同样的方法,在那里得到响应。

WSDL:参考.cs

namespace ServiceReference
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.ServiceModel.ServiceContractAttribute(Namespace="urn:cdc:iisb:2011", ConfigurationName="ServiceReference.HL7SOAPEndPointSvcSoap")]
public interface HL7SOAPEndPointSvcSoap
{
    
    [System.ServiceModel.OperationContractAttribute(Action="Address", ReplyAction="*")]
    System.Threading.Tasks.Task<ServiceReference.submitMessageResponse> submitMessageAsync(ServiceReference.submitMessageRequest request);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class submitMessageRequest
{
    
    [System.ServiceModel.MessageBodyMemberAttribute(Name="submitMessage", Namespace="urn:cdc:iisb:2011", Order=0)]
    public ServiceReference.submitMessageRequestBody Body;
    
    public submitMessageRequest()
    {
    }
    
    public submitMessageRequest(ServiceReference.submitMessageRequestBody Body)
    {
        this.Body = Body;
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.Runtime.Serialization.DataContractAttribute(Namespace="")]
public partial class submitMessageRequestBody
{
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=0)]
    public string userid;
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=1)]
    public string password;
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=2)]
    public string dataownerid;
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=3)]
    public string cdphprogramid;
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=4)]
    public string cdphprogramenvironment;
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=5)]
    public string action;
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=6)]
    public string messagecontent;
    
    public submitMessageRequestBody()
    {
    }
    
    public submitMessageRequestBody(string userid, string password, string dataownerid, string cdphprogramid, string cdphprogramenvironment, string action, string messagecontent)
    {
        this.userid = userid;
        this.password = password;
        this.dataownerid = dataownerid;
        this.cdphprogramid = cdphprogramid;
        this.cdphprogramenvironment = cdphprogramenvironment;
        this.action = action;
        this.messagecontent = messagecontent;
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class submitMessageResponse
{
    
    [System.ServiceModel.MessageBodyMemberAttribute(Name="submitMessageResponse", Namespace="urn:cdc:iisb:2011", Order=0)]
    public ServiceReference.submitMessageResponseBody Body;
    
    public submitMessageResponse()
    {
    }
    
    public submitMessageResponse(ServiceReference.submitMessageResponseBody Body)
    {
        this.Body = Body;
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.Runtime.Serialization.DataContractAttribute(Namespace="")]
public partial class submitMessageResponseBody
{
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=0)]
    public string status;
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=1)]
    public string @return;
    
    public submitMessageResponseBody()
    {
    }
    
    public submitMessageResponseBody(string status, string @return)
    {
        this.status = status;
        this.@return = @return;
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
public interface HL7SOAPEndPointSvcSoapChannel : ServiceReference.HL7SOAPEndPointSvcSoap, System.ServiceModel.IClientChannel
{
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
public partial class HL7SOAPEndPointSvcSoapClient : System.ServiceModel.ClientBase<ServiceReference.HL7SOAPEndPointSvcSoap>, ServiceReference.HL7SOAPEndPointSvcSoap
{
    
    /// <summary>
    /// Implement this partial method to configure the service endpoint.
    /// </summary>
    /// <param name="serviceEndpoint">The endpoint to configure</param>
    /// <param name="clientCredentials">The client credentials</param>
    static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
    
    public HL7SOAPEndPointSvcSoapClient() : 
            base(HL7SOAPEndPointSvcSoapClient.GetDefaultBinding(), HL7SOAPEndPointSvcSoapClient.GetDefaultEndpointAddress())
    {
        this.Endpoint.Name = EndpointConfiguration.HL7SOAPEndPointSvcSoap.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
    
    public HL7SOAPEndPointSvcSoapClient(EndpointConfiguration endpointConfiguration) : 
            base(HL7SOAPEndPointSvcSoapClient.GetBindingForEndpoint(endpointConfiguration), HL7SOAPEndPointSvcSoapClient.GetEndpointAddress(endpointConfiguration))
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
    
    public HL7SOAPEndPointSvcSoapClient(EndpointConfiguration endpointConfiguration, string remoteAddress) : 
            base(HL7SOAPEndPointSvcSoapClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
    
    public HL7SOAPEndPointSvcSoapClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(HL7SOAPEndPointSvcSoapClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
    
    public HL7SOAPEndPointSvcSoapClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }
    
    public System.Threading.Tasks.Task<ServiceReference.submitMessageResponse> submitMessageAsync(ServiceReference.submitMessageRequest request)
    {
        return base.Channel.submitMessageAsync(request);
    }
    
    public virtual System.Threading.Tasks.Task OpenAsync()
    {
        return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
    }
    
    public virtual System.Threading.Tasks.Task CloseAsync()
    {
        return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
    }
    
    private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
    {
        if ((endpointConfiguration == EndpointConfiguration.HL7SOAPEndPointSvcSoap))
        {
            System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
            result.MaxBufferSize = int.MaxValue;
            result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
            result.MaxReceivedMessageSize = int.MaxValue;
            result.AllowCookies = true;
            result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
            return result;
        }
        throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
    }
    
    private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
    {
        if ((endpointConfiguration == EndpointConfiguration.HL7SOAPEndPointSvcSoap))
        {
            return new System.ServiceModel.EndpointAddress("address" +
                    "address");
        }
        throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
    }
    
    private static System.ServiceModel.Channels.Binding GetDefaultBinding()
    {
        return HL7SOAPEndPointSvcSoapClient.GetBindingForEndpoint(EndpointConfiguration.HL7SOAPEndPointSvcSoap);
    }
    
    private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
    {
        return HL7SOAPEndPointSvcSoapClient.GetEndpointAddress(EndpointConfiguration.HL7SOAPEndPointSvcSoap);
    }
    
    public enum EndpointConfiguration
    {
        
        HL7SOAPEndPointSvcSoap,
    }
}}

WCF 代码

                BasicHttpBinding b = new BasicHttpBinding();
            b.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
            b.Security.Transport.ClientCredentialType =
                HttpClientCredentialType.Certificate;
            EndpointAddress ea = new EndpointAddress("endpointaddress");
            HL7SOAPEndPointSvcSoapClient hL7SOAPEndPointSvcSoapClient = new HL7SOAPEndPointSvcSoapClient(b, ea);
            hL7SOAPEndPointSvcSoapClient.ClientCredentials.ClientCertificate.SetCertificate(certificatedetails parameters);
            string userid = "", password = "", dataownerid = "", cdphprogramid = "", cdphprogramenvironment = "", action = "", messagecontent = @"MSH";
            submitMessageRequest submitMessageRequest = new submitMessageRequest()
            {
                Body = new submitMessageRequestBody(userid, password, dataownerid, cdphprogramid, cdphprogramenvironment, action, messagecontent)
            };
            hL7SOAPEndPointSvcSoapClient.OpenAsync();
            var submitMessageResponse = await hL7SOAPEndPointSvcSoapClient.submitMessageAsync(submitMessageRequest);

总是得到 submitMessageResponse.body.@retun=null; submitMessageResponse.body.status=null

标签: c#wcfsoapwsdlwcf-binding

解决方案


幸运的是,在发布问题本身时发现了问题。我正在分享我的答案,也许它会对某人有所帮助。

问题出在 Visual Studio 生成的 Reference.cs 文件中。代码中缺少命名空间。之后我添加了命名空间,它开始正确映射。

 [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.Runtime.Serialization.DataContractAttribute(Namespace= "correctNamespace")]
public partial class submitMessageResponseBody
{
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=0)]
    public string status;
    
    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=1)]
    public string @return;
    
    public submitMessageResponseBody()
    {
    }
    
    public submitMessageResponseBody(string status, string @return)
    {
        this.status = status;
        this.@return = @return;
    }
}

推荐阅读