【发布时间】:2017-02-09 15:41:30
【问题描述】:
是否可以定义服务使用哪个 url 而不是标准的fabric:/AppName/ServiceName?
我无法找到这是否可以在应用程序级别进行配置。
【问题讨论】:
是否可以定义服务使用哪个 url 而不是标准的fabric:/AppName/ServiceName?
我无法找到这是否可以在应用程序级别进行配置。
【问题讨论】:
是的,您可以将ApplicationManifest.xml 中的服务名称更改为从服务类名称中获取的名称以外的名称。
简短:只需将该服务的ApplicationManifest.xml 中的name 属性更改为其他内容即可。
在代码中:如果我有这项服务:
public interface IJustAnotherStatelessService : IService
{
Task<string> SayHelloAsync(string someValue);
}
internal sealed class JustAnotherStatelessService : StatelessService, IJustAnotherStatelessService
{
// Service implementation
}
像这样在Program.cs注册:
ServiceRuntime.RegisterServiceAsync("JustAnotherStatelessServiceType",
context => new JustAnotherStatelessService(context)).GetAwaiter().GetResult();
在ServiceManifest.xml 中用于该服务
<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest ...>
<ServiceTypes>
<!-- This is the name of your ServiceType.
This name must match the string used in RegisterServiceType call in Program.cs. -->
<StatelessServiceType ServiceTypeName="JustAnotherStatelessServiceType" />
</ServiceTypes>
...
在ApplicationManifest.xml 中,您将获得建议的名称:
<ApplicationManifest ...>
<DefaultServices>
<Service Name="JustAnotherStatelessService">
<StatelessService ServiceTypeName="JustAnotherStatelessServiceType" InstanceCount="[JustAnotherStatelessService_InstanceCount]">
<SingletonPartition />
</StatelessService>
</Service>
</DefaultServices>
</ApplicationManifest>
这将为您提供一个 Uri 服务,例如
fabric:/app_name/JustAnotherStatelessService
现在,继续更改应用程序清单中的名称:
<ApplicationManifest ...>
<DefaultServices>
<Service Name="AwesomeService">
<StatelessService ServiceTypeName="JustAnotherStatelessServiceType" InstanceCount="[JustAnotherStatelessService_InstanceCount]">
<SingletonPartition />
</StatelessService>
</Service>
</DefaultServices>
</ApplicationManifest>
你的服务现在回答
fabric:/app_name/AwesomeService
【讨论】:
fabric:/foobarbazservice,但我认为这是不可能的,因为服务解析器会在应用程序类型中查找服务类型,并且不能随机选择一个!跨度>
您可以从这里使用这个 uri builder (ServiceUriBuilder.cs) 类:https://github.com/Azure-Samples/service-fabric-dotnet-web-reference-app/blob/master/ReferenceApp/Common/ServiceUriBuilder.cs
对于无状态服务,您可以轻松获取代理:
var serviceUri = new ServiceUriBuilder(ServiceName);
var proxyFactory = new ServiceProxyFactory();
var svc = proxyFactory.CreateServiceProxy<IServiceName>(serviceUri.ToUri());
对于有状态服务,您必须指定分区。
var serviceUri = new ServiceUriBuilder(StatefulServiceName);
var proxyFactory = new ServiceProxyFactory();
//this is just a sample of partition 1 if you are using number partitioning.
var partition = new ServicePartitionKey(1);
var svc = proxyFactory.CreateServiceProxy<IStatefulServiceName>(serviceUri.ToUri(), partition);
【讨论】: