遵循此article/tutorial之后,我注意到所生成的SOAP信封主体中的元素包含前缀mic:
。
如何删除前缀?
我使用MessageContract:
namespace GettingStartedLib.Message
{
[MessageContract]
public class CalculationMessage
{
public CalculationMessage() { }
public CalculationMessage(string operation, double firstNumber, double secondNumber, double result)
{
Operation = operation;
FirstNumber = firstNumber;
SecondNumber = secondNumber;
Result = result;
}
public CalculationMessage(CalculationMessage message)
{
Operation = message.Operation;
FirstNumber = message.FirstNumber;
SecondNumber = message.SecondNumber;
Result = message.Result;
}
[MessageHeader]
public string Operation { get; set; }
[MessageBodyMember]
public double FirstNumber { get; set; }
[MessageBodyMember]
public double SecondNumber { get; set; }
[MessageBodyMember]
public double Result { get; set; }
}
}
服务使用哪个:
namespace GettingStartedLib
{
public class CalculatorService : ICalculator
{
public CalculationMessage Calculate(CalculationMessage request)
{
CalculationMessage response = new CalculationMessage(request);
switch(request.Operation.ToLower())
{
case "add":
response.Result = request.FirstNumber + request.SecondNumber;
break;
case "divide":
response.Result = request.FirstNumber / request.SecondNumber;
break;
case "multiply":
response.Result = request.FirstNumber * request.SecondNumber;
break;
case "substract":
response.Result = request.FirstNumber - request.SecondNumber;
break;
default:
response.Result = 0.0D;
break;
}
return response;
}
}
}
使用哪个接口:
using GettingStartedLib.Message;
using System.ServiceModel;
namespace GettingStartedLib
{
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
CalculationMessage Calculate(CalculationMessage message);
}
}
生成的SOAP信封如下所示:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mic="http://Microsoft.ServiceModel.Samples">
<soapenv:Header>
<mic:Operation>?</mic:Operation>
</soapenv:Header>
<soapenv:Body>
<mic:CalculationMessage>
<!--Optional:-->
<mic:FirstNumber>?</mic:FirstNumber>
<!--Optional:-->
<mic:Result>?</mic:Result>
<!--Optional:-->
<mic:SecondNumber>?</mic:SecondNumber>
</mic:CalculationMessage>
</soapenv:Body>
</soapenv:Envelope>