2017-02-09 78 views
0

是否可以定义服务使用哪个网址而不是标准fabric:/AppName/ServiceName服务结构指定结构URL

我找不到这是否可配置或不在应用程序级别。

回答

0

,您可以在ApplicationManifest.xml更改服务的名称不是从服务的类名取名字以外的东西。

Short:只是将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> 

这会给你一个开放的,以您服务像

fabric:/app_name/JustAnotherStatelessService 

现在,继续前进,在应用程序清单更改名称:

<ApplicationManifest ...> 
    <DefaultServices> 
    <Service Name="AwesomeService"> 
     <StatelessService ServiceTypeName="JustAnotherStatelessServiceType" InstanceCount="[JustAnotherStatelessService_InstanceCount]"> 
     <SingletonPartition /> 
     </StatelessService> 
    </Service> 
    </DefaultServices> 
</ApplicationManifest> 

和您的服务现在答案来

fabric:/app_name/AwesomeService 
+0

谢谢你的回应,我相信我不清楚我的要求。我想知道你是否可以让应用程序监听'fabric:/ foobarbazservice',但我不认为这是可能的,因为服务解析器在应用程序类型内寻找服务类型,并且不能,比如说随机选择一个! – Mardoxx

+1

不,在那部分你是对的,你被限制在url的{applicationName}/{serviceName_set_in_ApplicationManifest}方案。我没有看到任何方式设置完全自定义的网址给予织物运输方式建立它的Url内部。 – yoape

0

您可以使用此URI建设者从这里(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);