WCF 之 OperationContract


這里主要說的是同名異常:

       [ServiceContract]
        public interface IUserInfo
        {
                [OperationContract]
                string ShowName(string name);

                [OperationContract]
                string ShowName();
        }    

對於以上同名的 OperationContract 如果在客戶端引用服務時,將報異常如下:

解決方法:

 [ServiceContract]
        public interface IUserInfo
        {
                [OperationContract]
                string ShowName(string name);


          //設置別名 [OperationContract(Name
="ShowNameDefault")] string ShowName(); }

這樣客戶端引用不報異常,但ShowName的重載,將不起作用,詳細引用如下:

如果需要同名使用ShowName,則需要自定義,而不使用自動生成的:

class UserInfoServiceClient : ClientBase<IUserInfo>, IUserInfo
      {
            public string ShowName()
            {
                  return this.Channel.ShowNameDefault();
            }
            public string ShowName(string name)
            {
                  return this.Channel.ShowName(name);
            }

            #region IUserInfo 不實現操作

            public Task<string> ShowNameAsync(string name)
            {
                  throw new NotImplementedException();
            }

            public string ShowNameDefault()
            {
                  throw new NotImplementedException();
            }

            public Task<string> ShowNameDefaultAsync()
            {
                  throw new NotImplementedException();
            }

            #endregion

      }
 static void Main(string[] args)
                {
                      UserInfoServiceClient client = new UserInfoServiceClient();

                      Console.WriteLine(client.ShowName());

                        Console.ReadKey();
                }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM