c# - 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
解决方案
幸运的是,在发布问题本身时发现了问题。我正在分享我的答案,也许它会对某人有所帮助。
问题出在 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;
}
}
推荐阅读
- javascript - 如何使用带有点击事件的多个 div 来切换表单中的特定 div
- ssh - 如何流浪 ssh 到不同的客人 prort
- c++ - 任意擦洗内存,同时避免读取访问冲突
- php - 使用 XDebug、VSCode 和 Docker 进行调试
- python - AttributeError:'module'对象没有属性'Serial'与pi和arduino的连接
- javascript - 使用html中的链接调用JS函数
- mysql - 生成的 protoc 文件创建错误的目标源
- python - 序列化程序无效
- appsettings - 我正在将 .Net Framework 4.7.2 WebApi 转换为 asp.net Core3.0,并且需要使用 Http.Sys 并将其侦听器设置为 appsettings.json 中的一个项目
- mysql - 还剩 5 天时,有没有办法显示临近的 DueDate 客户?VB.net mysql