问题
I have been researching Azure's Service Fabric Reliable Services and looking at the samples too.
I am now building a simple proof-of-concept application with a recommended setup: A stateless Web API service with a stateful service behind (1 partition).
I've been going round in circles trying to find the easiest way for the API service to talk to the stateful service behind. It looks like the stateful service will have to exposed using Web API too (as shown in the example WordCount app).
Am I correct thinking that in order to consume a stateful service it needs to expose itself via HTTP/WCF etc. using something like OwinCommunicationListener : ICommunicationListener
from the examples?
回答1:
If the Stateful and Stateless service are in the same cluster (In your case i assume that both services are in the same application type (same solution) ), You can call the Stateful service from Stateless service Using 'Service Proxy'
If you want to consume the stateful service from outside the cluster, you need to open an API communication listener.
ServiceProxy class is available from "Microsoft.ServiceFabric.Services".
Service Proxy only works within the same cluster.
usage
IServiceClass proxy = ServiceProxy.Create<IServiceClass >(new Uri(fabric:/your_service));
In order to proxy to works, the Methods in the stateful service needs to be exposed using an Interface.
回答2:
When the services lives inside the same app, you can access the service instance something like:
public static MyServices.Interfaces.IMyStatefulService GetMyStatefulService()
{
var proxyLocation = new ServiceUriBuilder("MyStatefulService");
var partition = new ServicePartitionKey(1); //provide the partitionKey for stateful services. for stateless services, you can just comment this out
return ServiceProxy.Create<MyServices.Interfaces.IMyStatefulService>(proxyLocation.ToUri(), partition);
}
ServiceProxy is from the Microsoft.ServiceFabric.Services.Remoting.Client namespace.
And you interface code will look something like:
public interface IMyStatefulService : IService
{
Task<MyResponseResult> DoSomething(MyRequest request);
}
来源:https://stackoverflow.com/questions/36589435/accessing-a-stateful-service