我正在使用WCF服务,这是我的服务方法:
[ServiceContract]
public interface II3ReceiverService
{
[OperationContract]
string result(Envelope envelope);
}
这是最初的请求部分,供参考:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:drug="http://drug.gateway.mvp.na_svr">
<soapenv:Header>
<OriginatorSystem>I3Logix</OriginatorSystem>
</soapenv:Header>
<soapenv:Body>
<drug:result soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
我收到此错误:
OperationFormatter 遇到无效的消息正文。期望找到名称为“result”且命名空间为“http://tempuri.org/”的节点类型“Element”。找到名称为“drug:result”且命名空间为“http://drug.gateway.mvp.na_svr”的节点类型“Element”
我该如何解决这个问题?
更新
我能够命中服务方法,但有效负载为空。 这是我更新的代码
[ServiceContract(Namespace = "http://drug.gateway.mvp.na_svr")]
public interface II3ReceiverService
{
[OperationContract(Action = "http://drug.gateway.mvp.na_svr/result")]
string result(Envelope report);
}
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
private EnvelopeHeader headerField;
private EnvelopeBody bodyField;
/// <remarks/>
public EnvelopeHeader Header
{
get
{
return this.headerField;
}
set
{
this.headerField = value;
}
}
/// <remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeHeader
{
private string originatorSystemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public string OriginatorSystem
{
get
{
return this.originatorSystemField;
}
set
{
this.originatorSystemField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
private result resultField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://drug.gateway.mvp.na_svr")]
public result result
{
get
{
return this.resultField;
}
set
{
this.resultField = value;
}
}
}
Web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Application Settings -->
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<!-- System.Web Settings -->
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<!-- System.ServiceModel Settings -->
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_I3ReceiverService">
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="I3Receiver.I3ReceiverService" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="I3Receiver.II3ReceiverService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:64863/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpBinding" scheme="http"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<diagnostics>
<messageLogging logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
</system.serviceModel>
<!-- System.WebServer Settings -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
<!-- System.Diagnostics Settings -->
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing">
<listeners>
<add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\logs\WcfTrace.svclog"/>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
<listeners>
<add name="messages" type="System.Diagnostics.XmlWriterTraceListener" initializeData="c:\logs\WcfMessages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
现在如何解决?非常感谢
此错误是由于 WCF 服务期望的消息结构与实际接收到的消息结构不匹配造成的。具体来说,WCF 期望在消息正文中找到名为 result 且命名空间为 http://tempuri.org/ 的 XML 元素,但实际上收到了名为 result 且命名空间为 http://drug.gateway.mvp 的元素.na_svr。因此,WCF 无法正确解析传入的 SOAP 消息,从而导致抛出 OperationFormatter 异常。
所以我想提两个建议:
直接使用自动属性OriginatorSystem,编译器会自动生成支持存储的字段。
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://drug.gateway.mvp.na_svr")]
public Result Result { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://drug.gateway.mvp.na_svr")]
public class Result
{
}
2.配置服务器的日志文件以查看您发送的肥皂到底是什么进一步确认了问题。