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);
到POST
和PUT
:
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
我怎樣才能解決這個問題呢?
這可以通過以下一倍發佈,看看是否有幫助:http://stackoverflow.com/questions/555073/enable-multiple-http-methods-on-a-single-operation – cocogorilla
你有沒有嘗試把'WebInvoke'屬性都放在同一個方法上? – Jay