我正在传递一个SOAP请求,在进行一些更改之前,看看如何将输出前缀从s:en信封更改为soap-env:https://www.vanacosmin.ro/Articles/Read/WCFEnvelopeNamespacePrefix,如下面的示例所示。
<soap-env:Envelope
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:pro="http://www.xxxx">
<soap-env:Header/>
<soap-env:Body>
<pro:getProposalList version="6.66">
<pro:code>323232</pro:code>
</pro:getProposalList>
</soap-env:Body>
而且.
<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ProposalList Version="3.22"
xmlns="http://xxxxx">
<ProposalHeader>
<RefNum>1</RefNum>
<Size>24</Size>
<DateSubmitted>2020-06-18</DateSubmitted>
</ProposalHeader>
</ProposalList>
</s:Body>
但是我需要这样的东西
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ProposalList Version="3.22"
xmlns="http://www.xxxx">
<ProposalHeader>
<RefNum>1</RefNum>
<Size>24</Size>
<DateSubmitted>2020-06-18</DateSubmitted>
</ProposalHeader>
</ProposalList>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
当我传入一个对象,然后实例化这个类时
public class ProposalMessageFormatter : IDispatchMessageFormatter
{
private readonly IDispatchMessageFormatter formatter;
public ProposalMessageFormatter(IDispatchMessageFormatter formatter)
{
this.formatter = formatter;
}
public void DeserializeRequest(Message message, object[] parameters)
{
this.formatter.DeserializeRequest(message, parameters);
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
var message = this.formatter.SerializeReply(messageVersion, parameters, result);
return new ProposalMessage(message);
}
}
IDispatchMessageFormatter formatter
为空,我怀疑这可能是问题的原因
我正在为我的DI和我的global.asax.cs使用autofac,并且在这些例子中没有引用和引用。
builder.RegisterType<ProposalMessageFormatter>().As<IDispatchMessageFormatter>();
builder.RegisterType<ProposalMessageFormatter>().SingleInstance();
builder.RegisterType<ProposalMessageFormatter>().UsingConstructor(typeof(IDispatchMessageFormatter));
返回的错误是
服务器由于内部错误无法处理请求。有关错误的更多信息,请打开服务器上的IncludeExceptionDetailInFaults (来自ServiceBehaviorAttribute或从配置行为),以便将异常信息发送回客户端,或者根据Microsoft .NET Framework文档启动跟踪并检查服务器跟踪日志。
现在更详细地介绍一下这个错误。
<s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode
xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault
</faultcode>
<faultstring xml:lang="en-GB">Object reference not set to an instance of an object.</faultstring>
<detail>
<ExceptionDetail
xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true"/>
<InnerException i:nil="true"/>
<Message>Object reference not set to an instance of an object.</Message>
<StackTrace> at TQ.LPAConnector.Service.MessageFormatter.ProposalMessageFormatter.DeserializeRequest(Message message, Object[] parameters) in C:\Projects\xxx.Service\MessageFormatter\ProposalMessageFormatter.cs:line 17
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace>
<Type>System.NullReferenceException</Type>
</ExceptionDetail>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
正如您从请求中看到的,这不是null,并且在更改之前一直在工作,所以不知道我可能错过了什么。
发布于 2020-06-18 01:49:20
这里是我的演示:
public class ProposalMessageFormatter : IDispatchMessageFormatter
{
private readonly IDispatchMessageFormatter formatter;
public ProposalMessageFormatter(IDispatchMessageFormatter formatter)
{
this.formatter = formatter;
}
public void DeserializeRequest(Message message, object[] parameters)
{
this.formatter.DeserializeRequest(message, parameters);
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
var message = this.formatter.SerializeReply(messageVersion, parameters, result);
return new ProposalMessage(message);
}
}
我是ProposalMessageFormatter。
public class MyOperationBehavior : IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
return;
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.Formatter = new ProposalMessageFormatter(dispatchOperation.Formatter);
}
public void Validate(OperationDescription operationDescription)
{
return;
}
}
我是MyOperationBehavior。
[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false)]
public class MyContractBehaviorAttribute : Attribute, IContractBehavior
{
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
return;
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
OperationDescription op = contractDescription.Operations[0];
op.Behaviors.Add(new MyOperationBehavior());
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
return;
}
}
这是MyContractBehaviorAttribute.Add MyOperationBehavior,通过MyContractBehaviorAttribute到达端点。
最后,我们将该属性添加到WCF服务中。
https://stackoverflow.com/questions/62431873
复制