通过ServiceProxy访问无状态服务失败+ ASP.NET 5 Web API项目抛出健康状况错误

时间:2016-03-22 14:55:29

标签: c# asp.net .net microservices azure-service-fabric

我是微软天蓝色服务面料的新手。对于我的硕士学位,我必须在服务结构中开发一个微服务方法原型。经过几个小时的研究,我仍然没有解决我的问题。

我希望在https://azure.microsoft.com/en-us/documentation/articles/service-fabric-add-a-web-frontend/中的Web前端访问我(在本地光纤集群中部署的)无状态服务。最简单的方法是将ASP .NET 5 Web Api项目添加到Service Fabric应用程序,并在ServiceProxy中进行ValuesController方法调用。所以我将此代码添加到我的解决方案中:

ValuesController.cs:

[Route("api/[controller]")]
public class ValuesController : Controller
{
  // GET api/values/IObject
  [HttpGet("{interfaceName}")]
  public async Task<string> Get(string interfaceName)
  {
    var serviceName = "fabric:/DataServiceFabric/MasterDataMService";
    var masterDataService = ServiceProxy.Create<IMasterDataMService>(new Uri(serviceName));
    var result = await masterDataService.GetMasterDataByName(interfaceName);
    return result.Content;
  }
}

在F5部署后,我的浏览器不会自动导航到我的网络前端。通过查看Service Fabric Explorer,我的ASP .NET 5应用程序将引发运行状况错误:

Kind        Health State  Description
=============================================================================
Partitions  Error         Unhealthy partitions: 100% (1/1), MaxPercentUnhealthyPartitionsPerService=0%.
Partition   Error         Unhealthy partition: PartitionId='413...', AggregatedHealthState='Error'.
Event       Error         Error event: SourceId='System.FM', Property='State'. Partition is below target replica or instance count.

在此this问题之后,“分区低于目标副本或实例计数”表示我的服务中的未处理异常阻止其启动。但我无法在Service Fabric Explorer中找到堆栈strace来调试此故障。这是我的ASP .NET Web服务的ServiceManifest.xml

ServiceManifest.xml(Web1):

<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="Web1" Version="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
   <ServiceTypes>
      <StatelessServiceType ServiceTypeName="Web1Type">
         <Extensions>
            <Extension Name="__GeneratedServiceType__">
               <GeneratedNames xmlns="http://schemas.microsoft.com/2015/03/fabact-no-schema">
                  <DefaultService Name="Web1Service" />
                  <ServiceEndpoint Name="Web1TypeEndpoint" />
               </GeneratedNames>
            </Extension>
         </Extensions>
      </StatelessServiceType>
   </ServiceTypes>
   <CodePackage Name="C" Version="1.0.0">
      <EntryPoint>
         <ExeHost>
            <Program>approot\runtimes\dnx-clr-win-x64.1.0.0-rc1-update1\bin\dnx.exe</Program>
            <Arguments>--appbase approot\src\Web1 Microsoft.Dnx.ApplicationHost Microsoft.ServiceFabric.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener</Arguments>
            <WorkingFolder>CodePackage</WorkingFolder>
            <ConsoleRedirection FileRetentionCount="5" FileMaxSizeInKb="2048" />
         </ExeHost>
      </EntryPoint>
   </CodePackage>
   <Resources>
      <Endpoints>
         <Endpoint Name="Web1TypeEndpoint" Protocol="http" Type="Input" Port="80" />
      </Endpoints>
   </Resources>
</ServiceManifest>

这是我的ApplicationManifest.xml我的服务架构解决方案:

ApplicationManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="DataServiceFabricType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
   <Parameters>
      <Parameter Name="ActorTestServiceActorService_PartitionCount" DefaultValue="10" />
      <Parameter Name="MasterDataMService_InstanceCount" DefaultValue="-1" />
   </Parameters>
   <ServiceManifestImport>
     <ServiceManifestRef ServiceManifestName="Web2Pkg" ServiceManifestVersion="1.0.0" />
     <ConfigOverrides />
   </ServiceManifestImport>
   <ServiceManifestImport>
      <ServiceManifestRef ServiceManifestName="Web1" ServiceManifestVersion="1.0.0" />
   </ServiceManifestImport>
   <ServiceManifestImport>
      <ServiceManifestRef ServiceManifestName="ActorTestServicePkg" ServiceManifestVersion="1.0.0" />
   </ServiceManifestImport>
   <ServiceManifestImport>
      <ServiceManifestRef ServiceManifestName="MasterDataMServicePkg" ServiceManifestVersion="1.0.0" />
      <ConfigOverrides />
   </ServiceManifestImport>
   <DefaultServices>
      <Service Name="Web1Service">
         <StatelessService ServiceTypeName="Web1Type">
            <SingletonPartition />
         </StatelessService>
      </Service>
      <Service Name="ActorTestServiceActorService" GeneratedIdRef="761ee3cf-5a3a-49d8-9c57-aa3480d1acf1">
         <StatelessService ServiceTypeName="ActorTestServiceActorServiceType">
            <UniformInt64Partition PartitionCount="[ActorTestServiceActorService_PartitionCount]" LowKey="-9223372036854775808" HighKey="9223372036854775807" />
         </StatelessService>
      </Service>
      <Service Name="MasterDataMService">
         <StatelessService ServiceTypeName="MasterDataMServiceType" InstanceCount="[MasterDataMService_InstanceCount]">
            <SingletonPartition />
         </StatelessService>
      </Service>
   </DefaultServices>
</ApplicationManifest>

因此,我使用ASP.NET 5 Web应用程序和相同的ValuesController.cs创建了一个新的解决方案。我确保我的无状态服务在我的本地群集上运行,而不是我启动了我的新Web应用程序。在我的控制器中调用GET-Method后,我得到以下异常:

Exception thrown: 'System.Fabric.FabricException' in mscorlib.dll
Microsoft.AspNet.Hosting.Internal.HostingEngine: Information: Request finished in 0,2593ms 500
Microsoft.AspNet.Server.Kestrel: Error: An unhandled exception was thrown by the application.
System.Fabric.FabricException: Invalid partition key/ID '{0}'  for selector {1}

我的无状态服务是SingletonPartition,所以我需要一个分区密钥吗?如果是,我如何获得密钥? Service Fabric Explorer不向我提供有关无状态服务的此信息。以下是我的无状态服务的ServiceManifest.xml

ServiceManifest.xml(MasterDataMService):

<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest Name="MasterDataMServicePkg"
                 Version="1.0.0"
                 xmlns="http://schemas.microsoft.com/2011/01/fabric"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ServiceTypes>
    <!-- This is the name of your ServiceType. 
         This name must match the string used in RegisterServiceType call in Program.cs. -->
    <StatelessServiceType ServiceTypeName="MasterDataMServiceType" />
  </ServiceTypes>

  <!-- Code package is your service executable. -->
  <CodePackage Name="Code" Version="1.0.0">
    <EntryPoint>
      <ExeHost>
        <Program>MasterDataMService.exe</Program>
      </ExeHost>
    </EntryPoint>
  </CodePackage>

  <!-- Config package is the contents of the Config directoy under PackageRoot that contains an 
       independently-updateable and versioned set of custom configuration settings for your service. -->
  <ConfigPackage Name="Config" Version="1.0.0" />

  <Resources>
    <Endpoints>
      <!-- This endpoint is used by the communication listener to obtain the port on which to 
           listen. Please note that if your service is partitioned, this port is shared with 
           replicas of different partitions that are placed in your code. -->
      <Endpoint Name="ServiceEndpoint" Type="Input" Protocol="http" Port="80"/>
    </Endpoints>
  </Resources>
</ServiceManifest>

之后我决定与OWIN建立服务沟通:

MasterDataMService.cs:

internal sealed class MasterDataMService : StatelessService, IMasterDataMService
{
  [...]      

  protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
  {
    return new[]
    {
      new ServiceInstanceListener(initParams => new OwinCommunicationListener("MasterDataMService", new StartUp(), initParams))
    };
  }
}

现在,我可以在HttpClient中使用DefaultController来访问我的微服务:

var client = new HttpClient();
var request = "http://localhost:80/MasterDataMService/api/values/query";
var result = string.Empty;
HttpResponseMessage response = await client.GetAsync(request);
if (response.IsSuccessStatusCode)
{
  result = await response.Content.ReadAsStringAsync();
}

但这不是我原本想要的。我不想在我的请求中指定服务端点。相反,我想通过ServiceProxy与我的无状态服务进行通信。我如何在这里实现这一目标?我错了什么?如何使用部署到我的服务结构集群中的ASP .NET 5应用程序解决此健康状态错误?

感谢您的时间。

修改

无效分区键异常的扩展堆栈跟踪:

Exception thrown: 'System.Fabric.FabricException' in mscorlib.dll
Microsoft.AspNet.Hosting.Internal.HostingEngine: Information: Request finished in 1,35ms 500
Microsoft.AspNet.Server.WebListener.MessagePump: Error: ProcessRequestAsync
System.Fabric.FabricException: Invalid partition key/ID '{0}'  for selector {1} ---> System.Runtime.InteropServices.COMException: exception of HRESULT: 0x80071BBF
   at System.Fabric.Interop.NativeClient.IFabricServiceManagementClient4.EndResolveServicePartition(IFabricAsyncOperationContext context)
   at System.Fabric.FabricClient.ServiceManagementClient.ResolveServicePartitionEndWrapper(IFabricAsyncOperationContext context)
   at System.Fabric.Interop.AsyncCallOutAdapter2`1.Finish(IFabricAsyncOperationContext context, Boolean expectedCompletedSynchronously)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.ServiceFabric.Services.Client.ServicePartitionResolver.<ResolveAsyncHelper>d__2a.MoveNext()
--- End of stack trace from the previous location where the exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.ServiceFabric.Services.Communication.Client.CommunicationClientFactoryBase`1.<GetClientAsync>d__a.MoveNext()

如果您需要更多信息,请与我联系。 (完整堆栈跟踪长度为82行)

无效的方案异常堆栈跟踪:

Exception thrown: 'System.ArgumentException' in mscorlib.dll
Microsoft.AspNet.Hosting.Internal.HostingEngine: Information: Request finished in 1,45ms 500
Microsoft.AspNet.Server.WebListener.MessagePump: Error: ProcessRequestAsync
System.ArgumentException: the provided uri scheme 'http' is invalid; expected 'net.tcp'.
Parametername: via
   at System.ServiceModel.Channels.TransportChannelFactory`1.ValidateScheme(Uri via)
   at System.ServiceModel.Channels.ConnectionOrientedTransportChannelFactory`1.OnCreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.Channels.ChannelFactoryBase`1.InternalCreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.Channels.ServiceChannelFactory.ServiceChannelFactoryOverDuplexSession.CreateInnerChannelBinder(EndpointAddress to, Uri via)
   at System.ServiceModel.Channels.ServiceChannelFactory.CreateServiceChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.Channels.ServiceChannelFactory.CreateChannel(Type channelType, EndpointAddress address, Uri via)
   at System.ServiceModel.DuplexChannelFactory`1.CreateChannel(InstanceContext callbackInstance, EndpointAddress address, Uri via)
   at System.ServiceModel.DuplexChannelFactory`1.CreateChannel(InstanceContext callbackInstance, Binding binding, EndpointAddress endpointAddress)
   at Microsoft.ServiceFabric.Services.Communication.Wcf.Client.WcfCommunicationClientFactory`1.<CreateClientAsync>d__2.MoveNext()
--- End of stack trace from the previous location where the exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.ServiceFabric.Services.Communication.Client.CommunicationClientFactoryBase`1.<CreateClientWithRetriesAsync>d__1e.MoveNext()

6 个答案:

答案 0 :(得分:8)

我在学习Service Fabric时遇到了同样的问题。原来只提供URI是不够的 - 我还必须将分区键指定为一个魔术值:

IHelloService service = ServiceProxy.Create<IHelloService>(new Uri("fabric:/Application1/HelloService"), new ServicePartitionKey(1));

感谢this thread on disq.us。微软工程师Oana Platon还提供了更深入的解释,说明为什么1的值有效:

  

Diogo,看一下解释分区的这篇文章:   link具体来说,看看远程   分区(也称为UniformInt64Partition):&#34;使用它   指定一个整数范围(由低位键和高位键标识)和   多个分区(n)。它创建了n个分区,每个分区负责   对于整个分区键范围的非重叠子范围。对于   例如,低密钥为0,高密钥的远程分区方案   99,计数4将创建四个分区,如下所示。&#34;   然后查看服务清单并找出它的配置方式    - 分区数和范围(低调 - 高键)。如果你有一个分区,那个范围内的任何键都会转到(一个)   分区,因此您指定的密钥并不重要。如果你有   不止一个分区,你需要找出你的哪一个   客户需要与之交谈。指定范围内的分区键   分区正在服务。

我必须承认,我自己必须更深入地研究分区以理解这种解释。

答案 1 :(得分:3)

在入门示例中,有一个ServiceUriBuilder类。通过在构造函数中传递服务名来初始化此类。

var proxyLocation = new ServiceUriBuilder("MasterDataMService");
var masterDataService = ServiceProxy.Create<IMasterDataMService>(proxyLocation.ToUri());

var result = await masterDataService.GetMasterDataByName(interfaceName);

同样在您的MasterDataMService CreateServiceInstanceListeners方法中,确保它看起来像这样:

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new[] { new ServiceInstanceListener(context => new FabricTransportServiceRemotingListener(context, this)) };
    }

答案 2 :(得分:2)

我使用ServiceProxy和无状态服务遇到了完全相同的问题。我有一个运作良好的有状态服务,但无国籍人正在接受:

System.Fabric.FabricException: Invalid partition key/ID '{0}'  for selector {1}

正如Allan T上面提到的,你还需要为ServiceProxy.Create()使用第三个构造函数重载:

而不是:

ServiceProxy.Create<IMasterDataMService>(0, new Uri("fabric:/DataServiceFabric/MasterDataMService"));

使用:

ServiceProxy.Create<IMasterDataMService>(new Uri("fabric:/DataServiceFabric/MasterDataMService"));

...简单地说,不要指定分区。这让错误消失了。

https://msdn.microsoft.com/en-us/library/mt628402.aspx

答案 3 :(得分:0)

我自己遇到了同样令人沮丧的问题并解决了它们。

要检查的几件事

1)确保您的ASP.NET 5 web api项目没有引用任何X64库。

2)Port Clash ..在将ASPNET 5 web api部署到群集时,请确保没有2个网站使用相同的端口。您可以将[ASP.NET 5项目] /PackageRoot/ServiceManifest.xml中的端口更改为底部

** 3)&#34; wrap&#34;文件夹疯狂!验证您的wrap文件夹(在根解决方案文件夹中)仅包含.net 4.5.1类库。当我删除&#34; Newtonsoft.Json&#34;我的问题消失了。包装文件夹内的文件夹。然后,您必须在解决方案上运行dnu restore,以便重新创建project.lock.json文件

4)确保startup.cs类中没有任何内容爆炸。在本地运行web api以确保其加载;正常的服务代理类将无法加载。

答案 4 :(得分:0)

根据您的第二个堆栈跟踪判断:

System.ArgumentException: the provided uri scheme 'http' is invalid; expected 'net.tcp'.

看起来您正在尝试使用ServiceProxy连接到正在侦听HTTP端点的服务。 ServiceProxy需要一个使用二进制协议而不是HTTP的Service Remoting Listener。

第一个例外仍然有点神秘:

System.Fabric.FabricException: Invalid partition key/ID '{0}'  for selector {1}

仅当您尝试在不提供分区键的情况下解析统一的int64或命名分区服务时,才会发生这种情况。在您的配置中,您似乎将目标服务设置为单个分区,在这种情况下您不应该看到此错误,因此您可以仔细检查您尝试连接的服务实际上是否创建为单个分区服务?

答案 5 :(得分:0)

就我而言,以上都没有奏效。或者更准确地说,我的解决方案是上述解决方案的组合/变体

我需要做两件事:首先,确保我已经注册了我的服务:

  protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return new[] { new ServiceReplicaListener(context => this.CreateServiceRemotingListener(context)) };}

第二,参考第一个服务(“魔术”值为0)

        ServiceProxy.Create<IMasterDataMService>(new Uri("fabric:/DataServiceFabric/MasterDataMService"), new ServicePartitionKey(0));

我希望这会有所帮助