使用相同的方法进行PUT和POST

时间:2013-08-02 14:04:02

标签: c# wcf

我正在开发一个WCF Rest服务,我在ServiceContract

上有这个
    [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 个答案:

答案 0 :(得分: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方法的方法;您可以通过在Method = "*"属性上指定WebInvoke来执行此操作。但是如果你走那条路,你应该在操作中验证传入的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;
    }
}