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