這里主要說的是同名異常:
[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(); }