3

我正在开发 WCF 休息服务,我有这个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);toPOSTPUT

    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;
    }

我正在遵循这种模式来做到这一点。

但是,我收到一个错误:

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

我该如何解决这个问题?

4

1 回答 1

1

You cannot have two methods with the same signature - that's a C# issue, not a WCF one. You have basically two choices to go here. The first is to have two different methods, and have one call the other, or have a third method called by both):

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);
}

And the implementation:

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;
    }
}

Another alternative would be to have one single method which accepts multiple HTTP methods; you can do that by specifying Method = "*" on the WebInvoke attribute. But if you go that route you should validate, within the operation, that the incoming HTTP verb is one of those you expect:

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

And the implementation:

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;
    }
}
于 2013-08-02T20:28:43.400 回答