none
Fields or Message Parameter in WCF RRS feed

  • Question

  • Hello!

    Test in project and has an interface with another system that is the same as the client system, ie, it must be exactly the same message model that I consult. The question is:

    As it is called (symbol, parameter, etc.) and as adding the fields (CreationDateTime and InterchangeId) the message from my WCF to be different is exactly like the following:

    <soapenv: Envelope xmlns: soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: ant = "http: //dominio.cliente/ant"><soapenv: cabeçalho /><soapenv: Corpo><ant: insertProduct CreationDateTime = "?" InterchangeId = "?"><ant: ProductId> 10000 </ ant: ProductId><ant: Lines><! - 1 ou mais repetições: -><ant: ProductLine><formulário> COD> 01020020 </ ant: COD><ant: Quantity> 100 </ ant: Quantity></ ant: ProductLine></ ant: Lines></ ant: insertProduct></ soapenv: Body></ soapenv: Envelope>


    Wolney

    Friday, November 23, 2018 9:35 PM

All replies

  • Hi Wolney Rodrigues,
    Considering that WCF forum is an English forum, I wish you provide English version SOAP message.
    Besides, according to your description, I am not clear about what you mainly want to express. When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem.  That code should be…
      Minimal: use as litter code as possible that still produces the same problem.
      Complete: provide all parts needs to reproduce the problem.
      Verifiable: Test the code you’re about to provide to make sure it reproduces the problem.
    Feel free to let me know if there is anything I can help with.
    Best Regards
    Abraham

    Monday, November 26, 2018 5:38 AM
    Moderator
  • Hi Abraham,

    I'm sorry for the inconvenience

    My WCF service is functional. Here is an example of the template I want:

    Actual model
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:ant="http://temp-site.com.br/ant">
       <soapenv:Header/>
       <soapenv:Body>
    	<tem:person>
    		<name>Jessica Alba</name>
    		<age>35</age>
    	</person>
       </soapenv:Body>
    </soapenv:Envelope>
    
    
    Desired Model (age parameter)
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:ant="http://temp-site.com.br/ant">
       <soapenv:Header/>
       <soapenv:Body>
    	<person age="35">
    		<name>Jessica Alba</name>
    		</person>
    	</person>
       </soapenv:Body>
    </soapenv:Envelope>

    best regards
    Wolney


    Wolney


    Monday, November 26, 2018 3:40 PM
  • Hi Wolney,
    You could use the XmlSerializer to specify the format of the SOAP message.
    I have made a demo, wish it is useful to you.
    Server-side.

    static void Main(string[] args)
            {
                using (ServiceHost sh=new ServiceHost(typeof(Calculator)))
                {
                    sh.Open();
                    Console.Write("Service is ready....");
                    Console.ReadLine();
                    sh.Close();
                }
            }
    [ServiceContract]
        public interface ICalculator
        {
            [OperationContract]
            [XmlSerializerFormat]
            string SayHello([MessageParameter(Name="Product")]Product p);
        }
    
        public class Calculator : ICalculator
        {
            public string SayHello(Product p)
            {
                return $"Name:{p.Name}\nPrice:{p.Price}";
            }
        }
        public class Product
        {
            [XmlAttribute(AttributeName ="ID")]
            public int ID{ get; set; }
            [XmlElement(ElementName ="Name")]
            public string Name { get; set; }
            [XmlElement(ElementName ="Price")]
            public double Price { get; set; }
    
    }
    
    
    
    



    Web.config

    <system.serviceModel>
        <services>
          <service name="Server4.Calculator" behaviorConfiguration="mybev">
            <endpoint address="" binding="wsHttpBinding" contract="Server4.ICalculator" bindingConfiguration="mybinding">
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:1500"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <bindings>
          <wsHttpBinding>
            <binding name="mybinding">
              <security mode="None">
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior name="mybev">
              <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    

    Client.

            static void Main(string[] args)
            {
                Product p = new Product()
                {
                    ID = 1,
                    Name = "Banana",
                    Price = 34.63
                };
    
                ServiceReference1.CalculatorClient client = new ServiceReference1.CalculatorClient();
                try
                {
                   var result=client.SayHello(p);
                    Console.WriteLine(result);
                }
                catch (Exception)
                {
                    throw;
                }
            }
    
    
    

    Result.
    https://i.stack.imgur.com/hmSqv.png
    You could also use the MessageContract to customize the format of the soap message.
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-message-contracts
    Best Regards
    Abraham

    Wednesday, November 28, 2018 4:29 AM
    Moderator