Alternative to Using an Entity as a Parameter to a

2020-03-17 07:50发布

问题:

Howdy, ya'll! First question on StackOverflow! :-)

So here's the scenario: We're working on a web app with Silverlight 4 and using WCF RIA Services 1.0 SP1 Beta for the web service. I have my entities in the Entity Framework Designer, but I'm using a slightly-modified ADO.NET C# POCO Entity Generator template to generate the classes.

What I'd like to do is have a method inside a Domain Service with the following signature:

[EnableClientAccess]
public class ResultService : DomainService
{
    [Invoke]
    public SerializableResult CalculateResult(EntityOne e1, EntityTwo e2);
}

I am returning both EntityOne and EntityTwo to the client through queries in other services, like so:

[EnableClientAccess]
public class EntityOneService : DomainService
{
    public IQueryable<EntityOne> GetEntityOnes();
}

[EnableClientAccess]
public class EntityOneService : DomainService
{
    public IQueryable<EntityTwo> GetEntityTwos();
}

Those classes are successfully being generated in the Silverlight project. The SerializableResult does not have a key.

When I try to compile, I get the following error: "Operation named 'CalculateResult' does not conform to the required signature. Parameter types must be an entity or complex type, a collection of complex types, or one of the predefined serializable types."

In my research, the most helpful information I found were in the comments of this post by Jeff Handley.

Of note, Peter asked in a comment:

I get an 'does not conform to the required signature ...' compile error if my complex object has an [Key] Attribute. When I remove this attribute I can use the object as parameter for an Invoke operation.

Jeff's response:

This is by design. Complex objects cannot have Key properties. If you have a Key the class gets treated as an Entity.

So it sounds as if any further efforts to try to get my method to work will be futile. However, I was wondering if anyone else has come across this problem, and what they did to solve it.

Thanks very much!

回答1:

I have the following and it works for me.

namespace BusinessApplication2.Web
{
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;

    [EnableClientAccess()]
    public class DomainService1 : DomainService
    {
        public IQueryable<EntityOne> GetEntityOnes()
        {
            return null;
        }

        public IQueryable<EntityTwo> GetEntityTwos()
        {
            return null;
        }

        [Invoke]
        public SerializableResult GetSerializableResult(EntityOne one, EntityTwo two)
        {
            return new SerializableResult() { Result = "It woooooorrrked!" };
        }
    }

    public class EntityOne
    {
        [Key]
        public int Id { get; set; }
    }

    public class EntityTwo
    {
        [Key]
        public int Id { get; set; }
    }

    public class SerializableResult
    {
        public string Result { get; set; }
    }
}


回答2:

Many thanks to Mr. Jeff Handley and Mr. Dinesh Kulkarni for the answer (through Twitter).

In order for an Entity to be used as a parameter in an invoke method, that Entity must be exposed through a query method existing within the same DomainService. The intention for this restriction is that

"Each domain service needs to be able to stand on its own."

By adding two dummy Query methods (see Jeff's answer for an example), I was able to compile my code.