Message or a type that has MessageContractAttribute and other parameters of different types

后端 未结 5 1091
-上瘾入骨i
-上瘾入骨i 2021-02-07 16:13

I\'m developing WCF services where some classes have the [MessageContract] attribute, and some don\'t.

When I try to run the services I get this error mess

5条回答
  •  情书的邮戳
    2021-02-07 16:48

    Solved!

    I can't return String, I have return Greeting object to the client.

    using System;
    using System.ServiceModel;
    using System.Net.Security;
    
    namespace com.blogspot.jeanjmichel.model
    {
        [MessageContract]
        public class Greeting
        {
            private String userGreeting;
    
            private void SetGreeting()
            {
                DateTime now = DateTime.Now;
    
                if (now.Hour >= 7 && now.Hour <= 11)
                {
                    this.userGreeting = "Good morning";
                }
                else if (now.Hour >= 12 && now.Hour <= 17)
                {
                    if (now.Hour == 12 || now.Hour == 13)
                    {
                        this.userGreeting = "Good afternoon, it's lunch time!";
                    }
                    else
                    {
                        this.userGreeting = "Good afternoon";
                    }
                }
                else if (now.Hour >= 18 && now.Hour <= 20)
                {
                    this.userGreeting = "Good evening";
                }
                else
                {
                    this.userGreeting = "Good night";
                }
            }
    
            [MessageBodyMember(Order = 1, ProtectionLevel = ProtectionLevel.EncryptAndSign)]
            public String UserGreeting
            {
                get { return this.userGreeting; }
            }
    
            public Greeting()
            {
                this.SetGreeting();
            }
        }
    }
    
    using System;
    using System.ServiceModel;
    using com.blogspot.jeanjmichel.model;
    
    namespace com.blogspot.jeanjmichel.services.contract
    {
        [ServiceContract(Namespace = "http://jeanjmichel.blogspot.com/services/v0.0.1")]
        public interface IGetGreeting
        {
            [OperationContract]
            Greeting GetGreeting(Credential credential);
        }
    }
    
    using System;
    using System.ServiceModel;
    using com.blogspot.jeanjmichel.services.contract;
    using com.blogspot.jeanjmichel.model;
    
    namespace com.blogspot.jeanjmichel.services
    {
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
                         Namespace = "http://jeanjmichel.blogspot.com/services/v0.0.1")]
        public class GetGreetingService: IGetGreeting
        {
            public Greeting GetGreeting(Credential credential)
            {
                if (String.IsNullOrEmpty(credential.Token))
                {
                    throw new FaultException("Inform the security phrase, and try again.");
                }
                else
                {
                    if (credential.Token.Equals("mySeCuriTyP@ss"))
                    {
                        Greeting g = new Greeting();
                        return g;
                    }
                    else
                    {
                        throw new FaultException("Wrong password.");
                    }
                }
            }
        }
    }
    

提交回复
热议问题