1

我无法让这个工作,我已经用谷歌搜索并且可能找到了关于如何做到这一点的每一页,所有两个!

基本上我只是想让 SF Remoting V2 从无状态的 .NET Core 2 MVC 应用程序工作到有状态的服务。

这是我所做的:

控制器中的客户端代码:(尽可能简化):

public class ValuesController : Controller
{

    [HttpGet]
    public async Task<IEnumerable<string>> Get()
    {

         // Provide certificate details.
        var serviceProxyFactory = new ServiceProxyFactory((c) => new FabricTransportServiceRemotingClientFactory());

        var proxy = serviceProxyFactory.CreateServiceProxy<ICallMe>(new Uri("fabric:/SFExperiment/Stateful1"));

        var value3 = await proxy.GetGreeeting("Bob");

        return new[] { "value1", "value2", value3 };
    }

服务代码接口:

using System.Threading.Tasks;
using Microsoft.ServiceFabric.Services.Remoting;
using Microsoft.ServiceFabric.Services.Remoting.FabricTransport;


[assembly: FabricTransportServiceRemotingProvider(RemotingListener = 
RemotingListener.V2Listener, RemotingClient = RemotingClient.V2Client)]

namespace Stateful1.Abstractions
{

 public interface ICallMe : IService
 {
     Task<string> GetGreeeting(string name);
 }
}

服务代码:

    Public sealed class Stateful1 : StatefulService, ICallMe
{
    public Stateful1(StatefulServiceContext context)
        : base(context)
    { }

    protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return this.CreateServiceRemotingReplicaListeners();
    }

    public Task<string> GetGreeeting(string name)
    {
        return Task.FromResult(@"Congratulations, you have remoting working. :-) ");
    }

我在下面添加到 ServiceManifest.xml

  <Resources>
<Endpoints>
  <!-- To enable Service remonting for remoting services V2-->
  <Endpoint Name="ServiceEndpointV2" />

  <Endpoint Name="ReplicatorEndpoint" />
</Endpoints>
</Resources>

它不起作用..我得到以下异常:

选择器 {1} 的分区键/ID“{0}”无效

我究竟做错了什么?

4

1 回答 1

3

在创建服务代理的调用中,您必须指定分区键,因为您要连接到有状态服务。

long partitionKey = 0L;  //TODO: Determine partition key
var proxy = serviceProxyFactory.CreateServiceProxy<ICallMe>(new Uri("fabric:/SFExperiment/Stateful1"), new ServicePartitionKey(partitionKey), TargetReplicaSelector.PrimaryReplica);

此外,请确保您重用您的服务代理工厂,而不是创建一个新的。例如,看看这段代码。

于 2017-11-23T10:18:51.157 回答