2013-08-02 53 views
3

我正在開發一個WCF REST服務,我有這對我的ServiceContract用同樣的方法把和POST

[OperationContract] 
    [WebInvoke(Method = "POST", 
     UriTemplate = "/users", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Bare)] 
    User AddOrUpdateUser(User user); 

    [OperationContract] 
    [WebInvoke(Method = "PUT", 
     UriTemplate = "/users", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Bare)] 
    User AddOrUpdateUser(User user); 

我將使用User AddOrUpdateUser(User user);POSTPUT

public User AddOrUpdateUser(User user) 
    { 
     if (user == null) 
      throw new ArgumentNullException("user", "AddOrUpdateUser: user is null"); 

     using (var context = new AdnLineContext()) 
     { 
      context.Entry(user).State = user.UserId == 0 ? 
             EntityState.Added : 
             EntityState.Modified; 
      context.SaveChanges(); 
     } 

     return user; 
    } 

我跟着這pattern來做到這一點。

但是,我得到一個錯誤:

The type 'MyCompanyWCFService.IMyCompanyService' already contains a definition for 
'AddOrUpdateUser' with the same parameters 

我怎樣才能解決這個問題呢?

+1

這可以通過以下一倍發佈,看看是否有幫助:http://stackoverflow.com/questions/555073/enable-multiple-http-methods-on-a-single-operation – cocogorilla

+0

你有沒有嘗試把'WebInvoke'屬性都放在同一個方法上? – Jay

回答

1

你不能有兩種方法具有相同的簽名 - 這是一個C#的問題,而不是一個WCF一個。你基本上有兩種選擇去這裏。第一種方法是有兩種不同的方法,並且具有一個呼叫中的其他的,或者具有由兩個被稱爲第三方法):

public interface ITest 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", 
     UriTemplate = "/users", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Bare)] 
    User AddUser(User user); 

    [OperationContract] 
    [WebInvoke(Method = "PUT", 
     UriTemplate = "/users", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Bare)] 
    User UpdateUser(User user); 
} 

和實現:

public class Service 
{ 
    public User AddUser(User user) { return AddOrUpdateUser(user); } 
    public User UpdateUser(User user) { return AddOrUpdateUser(user); } 
    private User AddOrUpdateUser(User user) 
    { 
     if (user == null) 
      throw new ArgumentNullException("user", "AddOrUpdateUser: user is null"); 

     using (var context = new AdnLineContext()) 
     { 
      context.Entry(user).State = user.UserId == 0 ? 
             EntityState.Added : 
             EntityState.Modified; 
      context.SaveChanges(); 
     } 

     return user; 
    } 
} 

另一種替代方法是將有一種接受多種HTTP方法的方法;您可以通過在WebInvoke屬性上指定Method = "*"來完成此操作。但是,如果你去,你應該驗證這條路線,在操作中,傳入的HTTP動詞是其中的一個你期待:

public interface ITest 
{ 
    [OperationContract] 
    [WebInvoke(Method = "*", 
     UriTemplate = "/users", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Bare)] 
    User AddOrUpdateUser(User user); 
} 

和實現:

public class Service 
{ 
    public User AddOrUpdateUser(User user) 
    { 
     var method = WebOperationContext.Current.IncomingRequest.Method.ToUpperInvariant(); 
     if (method != "POST" || method != "PUT") 
     { 
      throw new WebFaultException(HttpStatusCode.MethodNotAllowed); 
     } 

     if (user == null) 
      throw new ArgumentNullException("user", "AddOrUpdateUser: user is null"); 

     using (var context = new AdnLineContext()) 
     { 
      context.Entry(user).State = user.UserId == 0 ? 
             EntityState.Added : 
             EntityState.Modified; 
      context.SaveChanges(); 
     } 

     return user; 
    } 
}