Here is a simple Service Fabric stateless service with WCF communication and it's client - a console app. It works well on the local cluster, client gets responce from the service. But I don't know how to communicate with a service if I deploy it in the cloud. What should I do to access it from console app?
SF Stateless service with WCF communications:
Contract:
[ServiceContract]
public interface IPresidentialService
{
[OperationContract]
Task<string> GetInfo();
}
Service:
internal sealed class PresidentialService : StatelessService, IPresidentialService
{
public PresidentialService(StatelessServiceContext context) : base(context)
{
}
public Task<string> GetInfo() => Task.FromResult($"Node {Context.NodeContext.NodeName} operating");
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[]
{
new ServiceInstanceListener(context =>
new WcfCommunicationListener<IPresidentialService>(wcfServiceObject: this, serviceContext: context,
endpointResourceName: "WcfServiceEndpoint",
listenerBinding: WcfUtility.CreateTcpListenerBinding()))
};
}
}
}
Client console app:
WCF client:
public class PresidentialServiceClient : ServicePartitionClient<WcfCommunicationClient<IPresidentialService>>
{
public PresidentialServiceClient(
ICommunicationClientFactory<WcfCommunicationClient<IPresidentialService>> communicationClientFactory,
Uri serviceUri, ServicePartitionKey partitionKey = null,
TargetReplicaSelector targetReplicaSelector = TargetReplicaSelector.Default, string listenerName = null,
OperationRetrySettings retrySettings = null) : base(communicationClientFactory, serviceUri, partitionKey,
targetReplicaSelector, listenerName, retrySettings)
{
}
public Task<string> GetInfo() => InvokeWithRetryAsync(client => client.Channel.GetInfo());
}
Client App:
private static void Main(string[] args)
{
var binding = WcfUtility.CreateTcpClientBinding();
var partitionResolver = ServicePartitionResolver.GetDefault();
var wcfClientFactory =
new WcfCommunicationClientFactory<IPresidentialService>(binding,
servicePartitionResolver: partitionResolver);
var serviceUri = new Uri("fabric:/Application5/PresidentialService");
var client = new PresidentialServiceClient(wcfClientFactory, serviceUri, ServicePartitionKey.Singleton);
do
{
Console.WriteLine(client.GetInfo().Result);
Console.ReadKey();
} while (true);
}
Added to ServiceManifest.xml:
<Endpoints>
<Endpoint Name="WcfServiceEndpoint" />
</Endpoints>
UPDATE
Changed ServicePartitionResolver:
var partitionResolver = new ServicePartitionResolver("sfapp.westeurope.cloudapp.azure.com:19000");
Still not works.
UPDATE
Added a load balancer rule for TCP port 777.
When the service is running in the cloud, you can't use the default resolver.
The default ServicePartitionResolver assumes that the client is
running in same cluster as the service. If that is not the case,
create a ServicePartitionResolver object and pass in the cluster
connection endpoints.
Try something like
ServicePartitionResolver resolver = new ServicePartitionResolver("mycluster.cloudapp.azure.com:19000");
https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-wcf
Related
I'm trying to make a simple communication between two microservices. So far as a receiver
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[]
{
new ServiceInstanceListener((context) =>
new WcfCommunicationListener<ITest>(
wcfServiceObject: new Test(),
serviceContext: context,
endpointResourceName: "ProgramTestEndpoint",
listenerBinding: WcfUtility.CreateTcpListenerBinding()
),
name: "ProgramTestListener"
)
};
}
public class Test : ITest
{
public async Task<int> ReturnsInt()
{
return 2;
}
}
[ServiceContract]
public interface ITest
{
[OperationContract]
Task<int> ReturnsInt();
}
And I did add to service manifest the endpoint.
<Endpoint Name ="ProgramTestEndpoint"/>
The microservice that wants to communicate has this code
protected override async Task RunAsync(CancellationToken cancellationToken)
{
// TODO: Replace the following sample code with your own logic
// or remove this RunAsync override if it's not needed in your service.
await Task.Delay(5000);
CloudClient<ITest> transactionCoordinator = new CloudClient<ITest>
(
serviceUri: new Uri($"{Context.CodePackageActivationContext.ApplicationName}/MyStateless"),
partitionKey: new ServicePartitionKey(0),
clientBinding: WcfUtility.CreateTcpClientBinding(),
listenerName: "MyStateless"
);
int iterations = await transactionCoordinator.InvokeWithRetryAsync(client => client.Channel.ReturnsInt());
ServiceEventSource.Current.ServiceMessage(this.Context, "Test-{0}", ++iterations);
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations);
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
}
}
This is my first project in service fabric, I'm not sure what I'm doing wrong, but with this code the application can't receive the return value of the ReturnsInt task.
When creating a connection to a stateless service, you should use the ServicePartitionKey.Singleton partition key. In some cases you you don't need to specify one at all, for example when using ServiceProxyFactory to create a connection to a stateless service.
Using new ServicePartitionKey(0) was causing the client to try and connect to an endpoint that didn't exist.
I am porting an existing Cloud Service WorkerRole to Service Fabric as a stateless service. The original Cloud Service uses SignalR and Service Bus (as a SignalR backplane), to send notifications out to any client listening. There is a Startup class that does some of the setup:
class Startup
{
public void Configuration(IAppBuilder app)
{
String connectionString = "Endpoint=sb://[name].servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=[key]";
GlobalHost.DependencyResolver.UseServiceBus(connectionString, "InSys");
app.MapSignalR();
Notifications.Hub = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
}
}
In the OnStart() method in for the WorkerRole I kick-off OWIN with:
var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpEndpoint"];
var baseUri = $"{endpoint.Protocol}://{endpoint.IPEndpoint}";
var app = WebApp.Start<Startup>(new StartOptions(url: baseUri));
How is this (i.e., connection the to SignalR Service Bus Backplane) done for a stateless service within Service Fabric?
With the help of https://github.com/marcinbudny/SignalRSelfHostScaleOut (which is an example of scaleout using Redis) I think I have this licked.
In the ServiceManifest.xml I added the following EndPoint:
<Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="8322" />
I also added a Startup class:
public static class Startup
{
public static void ConfigureApp(IAppBuilder app)
{
String connectionString = "Endpoint=sb://[name].servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=[value]";
GlobalHost.DependencyResolver.UseServiceBus(connectionString, "InSys");
app.MapSignalR();
Notifications.Hub = GlobalHost.ConnectionManager.GetHubContext<InSysMainHub>();
}
}
An OwinCommunicationListener class was also added:
public class OwinCommunicationListener : ICommunicationListener
{
private readonly ServiceEventSource eventSource;
private readonly Action<IAppBuilder> startup;
private readonly ServiceContext serviceContext;
private readonly string endpointName;
private readonly string appRoot;
private IDisposable webApp;
private string publishAddress;
private string listeningAddress;
public OwinCommunicationListener(Action<IAppBuilder> startup, ServiceContext serviceContext, ServiceEventSource eventSource, string endpointName)
: this(startup, serviceContext, eventSource, endpointName, null)
{
}
public OwinCommunicationListener(Action<IAppBuilder> startup, ServiceContext serviceContext, ServiceEventSource eventSource, string endpointName, string appRoot)
{
if (startup == null)
{
throw new ArgumentNullException(nameof(startup));
}
if (serviceContext == null)
{
throw new ArgumentNullException(nameof(serviceContext));
}
if (endpointName == null)
{
throw new ArgumentNullException(nameof(endpointName));
}
if (eventSource == null)
{
throw new ArgumentNullException(nameof(eventSource));
}
this.startup = startup;
this.serviceContext = serviceContext;
this.endpointName = endpointName;
this.eventSource = eventSource;
this.appRoot = appRoot;
}
public Task<string> OpenAsync(CancellationToken cancellationToken)
{
var serviceEndpoint = this.serviceContext.CodePackageActivationContext.GetEndpoint(this.endpointName);
var protocol = serviceEndpoint.Protocol;
int port = serviceEndpoint.Port;
if (this.serviceContext is StatefulServiceContext)
{
StatefulServiceContext statefulServiceContext = (StatefulServiceContext) serviceContext;
listeningAddress = string.Format(
CultureInfo.InvariantCulture,
"{0}://+:{1}/{2}{3}/{4}/{5}",
protocol,
port,
string.IsNullOrWhiteSpace(appRoot)
? string.Empty
: appRoot.TrimEnd('/') + '/',
statefulServiceContext.PartitionId,
statefulServiceContext.ReplicaId,
Guid.NewGuid());
}
else if (serviceContext is StatelessServiceContext)
{
listeningAddress = string.Format(
CultureInfo.InvariantCulture,
"{0}://+:{1}/{2}",
protocol,
port,
string.IsNullOrWhiteSpace(appRoot)
? string.Empty
: appRoot.TrimEnd('/') + '/');
}
else
{
throw new InvalidOperationException();
}
publishAddress = listeningAddress.Replace("+", FabricRuntime.GetNodeContext().IPAddressOrFQDN);
try
{
eventSource.Message("Starting web server on " + listeningAddress);
webApp = WebApp.Start(listeningAddress, appBuilder => startup.Invoke(appBuilder));
eventSource.Message("Listening on " + this.publishAddress);
return Task.FromResult(this.publishAddress);
}
catch (Exception ex)
{
eventSource.Message("Web server failed to open endpoint {0}. {1}", this.endpointName, ex.ToString());
StopWebServer();
throw;
}
}
public Task CloseAsync(CancellationToken cancellationToken)
{
this.eventSource.Message("Closing web server on endpoint {0}", this.endpointName);
this.StopWebServer();
return Task.FromResult(true);
}
public void Abort()
{
this.eventSource.Message("Aborting web server on endpoint {0}", this.endpointName);
this.StopWebServer();
}
private void StopWebServer()
{
if (this.webApp != null)
{
try
{
this.webApp.Dispose();
}
catch (ObjectDisposedException)
{
// no-op
}
}
}
}
And then finally I changed the CreateServiceInstanceListeners method in my stateless service code to:
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[]
{
new ServiceInstanceListener(serviceContext => new OwinCommunicationListener(Startup.ConfigureApp, serviceContext, ServiceEventSource.Current, "ServiceEndpoint"))
};
}
Create a Stateless service With Owin listener. Then in start up configure for signalR and backplane(service bus or sql). The issue ideally you would face is with a negotiate(Hand shake between Signalr client with server) At this point try configure for cross origin request, a sample code for persistent connection will look like below.
Also note the line appBuilder.UseAesDataProtectorProvider("Your Key") as it is important. The result of this is that you wont end up getting HTTP 400 to connect most of the time. This is because SignalR will make at least 2 requests at handshake and those will usually hit two different machines.
Thanks to marcin budny on the explanation.
var config = new HttpConfiguration();
// Configure your origins as required.
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
FormatterConfig.ConfigureFormatters(config.Formatters);
RouteConfig.RegisterRoutes(config.Routes);
appBuilder.UseWebApi(config);
GlobalHost.DependencyResolver.UseServiceBus("yourconnection string comes here", "signalrbackplaneserver");
appBuilder.UseAesDataProtectorProvider("some password");
appBuilder.Map("/echo", map =>
{
map.UseCors(CorsOptions.AllowAll).RunSignalR<MyEndPoint>();
});
Hi I'm trying to get dependency injection to work using Castle Windsor in a console app to host WCF service in Azure using the example in:
https://azure.microsoft.com/en-gb/documentation/articles/service-bus-dotnet-how-to-use-relay/
My WCF service works fine using Castle Windsor (Small sample):
public class PersonService : IPersonService
{
private readonly IPersonManager _personManager;
public PersonService(IPersonManager personManager)
{
_personManager = personManager;
}
public Message Create(Person person)
{
Message result = _ personManager.Create(person);
}
}
Castle Windsor (in wcf service Global.asax)
private void BuildWindsorContainer ()
{
var container = new WindsorContainer();
container.Kernel.AddFacility<WcfFacility>();
container.Kernel.Register(
Component.For< IPersonManager >().ImplementedBy<PersonManager >(),
Component.For<IPersonService>()
.ImplementedBy<PersonService>()
.Named("WebS.PersonService"),
);
}
In My hosted Client which calls the WCF service I have:
private static ServiceHost CallPersonService()
{
BuildWindsorContainer ();
var sh = new ServiceHost(typeof (PersonService));
sh.AddServiceEndpoint(typeof (IPersonService), new NetTcpBinding(), "net.tcp://localhost:37050/person");
sh.AddServiceEndpoint(typeof (IPersonService), new NetTcpRelayBinding(),
ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, "person"))
.Behaviors.Add(new TransportClientEndpointBehavior
{
TokenProvider =
TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", Key)
});
private void BuildWindsorContainer ()
{
var container = new WindsorContainer();
container.Kernel.AddFacility<WcfFacility>();
container.Kernel.Register(
Component.For< IPersonManager >().ImplementedBy<PersonManager >(),
Component.For<IPersonService>()
.ImplementedBy<PersonService>()
.Named("WebS.PersonService"),
);
}
How do I get the dependencies to be injected into service from the client?
var sh = new ServiceHost(typeof (PersonService));
sh.AddServiceEndpoint(typeof (IPersonService), new NetTcpBinding(), "net.tcp://localhost:37050/person");
IPersonManager is null in service when the client runs.
Try
var sh = (ServiceHost)new DefaultServiceHostFactory()
.CreateServiceHost(typeof(PersonService).AssemblyQualifiedName, new Uri[] { });
I've seen several examples on Internet setting up an NInject Factory modifying svc file. I don't have this svc file (or I don't know where's it).
I've a slight WCF service:
IService interface:
[ServiceContract]
public interface IFileSystemPluginService
{
[OperationContract]
void saveConfiguration(Configuration.Configuration configuration, string userId);
[OperationContract]
Configuration.Configuration getConfiguration(string userId);
}
IService implementation:
public class Service : IService
{
private IUserConfigurable userConfigurablePlugin;
public Service(IUserConfigurable configurablePlugin)
{
this.configurablePlugin = configurablePlugin;
}
}
In order to kick the service off I'm setting configuration up in code:
private void initializeWCFService()
{
string baseAdress = "xxx";
Uri baseAddressUri = new Uri(baseAdress);
this.serviceHost = new System.ServiceModel.ServiceHost(typeof(Service), baseAddressUri);
this.serviceHost.AddServiceEndpoint(
typeof(IService),
new System.ServiceModel.WSHttpBinding(),
string.Empty
);
System.ServiceModel.Description.ServiceMetadataBehavior smb = new System.ServiceModel.Description.ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri(baseAdress);
this.serviceHost.Description.Behaviors.Add(smb);
this.serviceHost.Open();
}
I need to inject a IUserConfigurable object in Service constructor.
I would like to write an automated regression test whereby my service is started and I can assert that my client can retrieve some things from the service.
Private Blah proxy;
[SetUp]
public void SetUp()
{
proxy= new Blah();
}
[Test]
public void GetStuff()
{
var result = proxy.GetStuff();
Assert.NotNull(result);
}
This doesn't work because my service isn't running. How would I make start my service before the tests?
For integration/acceptance testing of WCF services I suggest you to use Self Hosted WCF Service. Examples you can find here:
Creating a Self Hosted WCF Service
Self Hosting Windows Communication Foundation Services
Create self-hosted service on fixture setup, and close it on fixture tear down:
EndpointAddress address = new EndpointAddress("http://localhost:8080/service1");
ServiceHost host;
IService1 service;
[TestFixtureSetUp]
public void FixtureSetUp()
{
var binding = new BasicHttpBinding();
host = new ServiceHost(typeof(Service1), address.Uri);
host.AddServiceEndpoint(typeof(IService1), binding, address.Uri);
host.Open();
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
if (host == null)
return;
if (host.State == CommunicationState.Opened)
host.Close();
else if (host.State == CommunicationState.Faulted)
host.Abort();
}
With service hosted you can get service proxy:
var binding = new BasicHttpBinding();
ChannelFactory<IService1> factory =
new ChannelFactory<IService1>(binding, address);
service = factory.CreateChannel();
And your test will look like:
[Test]
public void ShouldReturnSomeStuff()
{
var result = service.GetStuff();
Assert.NotNull(result);
}