我有一些接受一些数据的服务然后,我认为应该返回一个用一些值初始化的actor。
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();
//...
那么,以这种方式回复演员是否可以,或者我应该返回演员的身份并在通话时请求代理?
UPD: 根据@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);
}
}
答案 0 :(得分:1)
不,SF远程处理中使用的类型必须为DataContractSerializable
。您使用的合同只能包含字段和属性,没有方法。
因此,不是返回代理,而是返回Actor Reference。
接下来,使用Bind从中创建代理。