【问题标题】:Is it normal to return actor's proxy from service从服务中返回演员的代理是否正常
【发布时间】:2017-09-02 07:11:53
【问题描述】:

我有一些服务接受一些数据,然后,我认为应该返回一个用一些值初始化的演员。

public class MyService : StatefulService, IMyService
{
    public IMyActor DoThings(Data data)
    {
        var actor = ActorProxy.Create<IMyActor>(new ActorId(Guid.NewGuid()));  
        actor.Init(data);
        //some other things
        return actor;
    }
}

另一个服务会这样做:

var service = ServiceProxy.Create<ICommandBrokerService>(new Uri("fabric:/App"), ServicePartitionKey.Singleton);
var actor = service.DoThings(data);
var state = actor.GetState();
//...

那么,以这种方式返回演员是否可以,或者我应该返回演员的 ID 并在呼叫视线上请求代理?

统一更新: 根据@LoekD 的回答,我做了一个包装器来保证类型安全。

[DataContract(Name = "ActorReferenceOf{0}Wrapper")]
public class ActorReferenceWrapper<T>
{
    [DataMember]
    public ActorReference ActorReference { get; private set; }
    public ActorReferenceWrapper(ActorReference actorRef)
    {
        ActorReference = actorRef ?? throw new ArgumentNullException();
    }

    public T Bind()
    {
        return (T)ActorReference.Bind(typeof(T));
    }

    public IActorService GetActorService(IActorProxyFactory serviceProxy=null)
    {
        return ActorReference.GetActorService(serviceProxy);
    }

    public TService GetActorService<TService>(IActorProxyFactory serviceProxyFactory) where TService : IActorService
    {
       return serviceProxyFactory.CreateActorServiceProxy<TService>(ActorReference.ServiceUri,
                ActorReference.ActorId);
    }

    public static implicit operator ActorReference(ActorReferenceWrapper<T> actorRef)
    {
        return actorRef.ActorReference;
    }

    public static explicit operator ActorReferenceWrapper<T>(ActorReference actorReference)
    {
        return new ActorReferenceWrapper<T>(actorReference);
    }
}

【问题讨论】:

    标签: azure actor azure-service-fabric service-fabric-actor


    【解决方案1】:

    不可以,SF 远程处理中使用的类型必须是DataContractSerializable。你使用的合约只能有字段和属性,没有方法。

    因此,不要返回代理,而是返回 Actor Reference

    接下来,使用Bind 从中创建一个代理。

    【讨论】:

    • 所以,我将在返回的 Actor Reference 对象上调用 Bind(typeof(IMyActor))。但它看起来不那么类型安全,不是吗?我的意思是,查看服务的接口签名并不清楚将返回什么类型的演员。另外,我检查了我的本地集群,从另一个服务返回 Actor Proxy 时没有错误。
    • 不,这不是描述性合同。但是你可以让 ActorReference 成为一个更大的合约的属性,该合约包含更多关于 Actor 类型的信息,以使其更加用户友好,
    • 我用一些类型安全的解决方法更新了问题。
    猜你喜欢
    • 2011-02-21
    • 2018-08-03
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2013-06-22
    相关资源
    最近更新 更多