Send large stream to ServiceFabric Service

主宰稳场 提交于 2019-12-12 22:48:55

问题


I have a ServiceFabric service hosting WebAPI. On a controller, I receive, in my Request, a FileStream. I have no problem reading the FileStream there.

Then, I want this WebAPI service to call another SF service (stateful) - let's call it Service2, giving a MemoryStream in parameter.

try
{
    await _service2Proxy.MyService2Method(myMemoryStream, otherParameters);
    // Line after
}
catch
{
    // Error handling
}

And in the Service2

public Task MyService2Method(MemoryStream ms, string otherParam)
{
    // Log line
    // Do something
}

It works perfectly well with a File < 3 MB. Yet, with a file > 5 MB, the call doesn't work. We never go on // Line after, // Error handling or // Log line.

I did add [assembly: FabricTransportServiceRemotingProvider(MaxMessageSize = int.MaxValue)] on the controller assembly, the WebAPI service assembly and the Service2 assembly. The Service2 interface has the [OperationContract] and [ServiceContract] attributes.

I also tried sending a byte[] instead of a MemoryStream. The problem is still the same.


回答1:


If it's a StatefulService and you use some ReliableDictionary with huge data, it could lead to similar issues when SF replicates your dictionary data.

You can set two more settings to prevent this:

  • Set the MaxReplicationMessageSize when you create the service instance.
  • Init your ServiceReplicaListener with custom FabricTransportListenerSettings : MaxMessageSize

Code:

public MyStateFulService(StatefulServiceContext context) 
    : base(context, new ReliableStateManager(context, new ReliableStateManagerConfiguration(new ReliableStateManagerReplicatorSettings
    {
        MaxReplicationMessageSize = 1073741824
    }))){ }

protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
    var setting = new FabricTransportListenerSettings();
    setting.MaxMessageSize = 1073741824;
    return new[] { new ServiceReplicaListener(initParams => new FabricTransportServiceRemotingListener(initParams, this, setting), "RpcListener")};
}

Edit :

A highly better way to do this: In case you have authentication between replica, you should set these settings in Settings.xml.

    <?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <!-- This is used by the StateManager's replicator. -->
  <Section Name="ReplicatorConfig">
    <Parameter Name="ReplicatorEndpoint" Value="ReplicatorEndpoint" />
    <Parameter Name="MaxReplicationMessageSize" Value="1073741824" />
  </Section>
  <!-- This is used for securing StateManager's replication traffic. -->
  <Section Name="ReplicatorSecurityConfig">
    <Parameter Name="CredentialType" Value="Windows" />
    <Parameter Name="ProtectionLevel" Value="None" />
  </Section>

  <!-- Add your custom configuration sections and parameters here. -->
  <!--
  <Section Name="MyConfigSection">
    <Parameter Name="MyParameter" Value="Value1" />
  </Section>
  -->
</Settings>



回答2:


It works fine for us.

Makes sure you are setting the assembly attribute properly. https://msdn.microsoft.com/en-us/library/4w8c1y2s(v=vs.110).aspx

Here is what we are doing.

using Microsoft.ServiceFabric.Services.Remoting.FabricTransport; [assembly: FabricTransportServiceRemotingProvider(MaxMessageSize = 134217728)]

Again make sure this is available in the assembly that is creating the service remoting listener and the assembly that is calling it with ServiceProxy.

Alternatively, you can set the max message size programmatically when you create the listener, or in a settings.xml config file. See here for more info on that: https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-services-secure-communication/



来源:https://stackoverflow.com/questions/43124021/send-large-stream-to-servicefabric-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!