首页 > 其他 > 详细

WCF 之 OperationContract

时间:2015-03-05 18:50:17      阅读:232      评论:0      收藏:0      [点我收藏+]

这里主要说的是同名异常:

       [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();
                }

 

WCF 之 OperationContract

原文:http://www.cnblogs.com/yipeng-yu/p/4316391.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!