Comments (7)
@ReubenBond Your code worked perfectly (you can see PR here if you are interested unifiedfx/UFX.Orleans.SignalRBackplane#13)
I think it would be good to have IGrainTypeProvider
and IGrainInterfaceTypeProvider
documented somewhere, as they are really useful!
from orleans.
This is possible today, but it's not ergonomic. The way you do it is by creating an implementation of Orleans.Metadata.IGrainTypeProvider
and registering that in the container, like this:
public interface IMyGrain : IGrainWithStringKey {}
public class MyGrain : IMyGrain { }
public class MyLibraryOptions
{
public string GrainTypeName { get; set; } = "my-default-grain-type";
}
internal class MyGrainTypeProvider(IOptions<MyLibraryOptions> options) : IGrainTypeProvider
{
public bool TryGetGrainType(Type type, out GrainType grainType)
{
if (type == typeof(MyGrain))
{
grainType = GrainType.Create(options.Value.GrainTypeName);
return true;
}
grainType = default;
return false;
}
}
Register it in DI:
services.AddSingleton<IGrainTypeProvider, MyGrainTypeProvider>();
Verify that it worked:
var grain = grainFactory.GetGrain<IMyGrain>("test");
var grainTypeName = grain.GetGrainId().Type.ToString();
Assert.Equal("my-default-grain-type", grainTypeName);
I have not tested this code, so please let us know if that works. I'm interested to learn more about your use case, too.
In the source, the responsible code is here:
from orleans.
Ah nice, thanks Reuben! I asked in the Discord yesterday and got an answer about manipulating source generated code, so I assumed it wasn't possible out of the box 😅
Use Case
I wrote a SignalR backplane library a while back with a UserGrain to track each connected user. I am writing a game that uses this backplane, which also has a UserGrain, for each game player.
When I run the game server, I get
System.InvalidOperationException: 'An entry with the key user is already present.
Existing: ["type-name": "UserGrain", "interface.0": "X.Grains.IUserGrain", "full-type-name": "X.Grains.UserGrain", "interface.1": "Orleans.IGrainWithStringKey", "diag.asm": "X", "diag.type": "X.Grains.UserGrain,X"]
Trying to add: ["interface.3": "TheLibrary.Abstractions.IUserGrain", "type-name": "UserGrain", "interface.0": "TheLibrary.Grains.ISignalrGrain", "full-type-name": "TheLibrary.Grains.UserGrain", "interface.1": "Orleans.IGrainWithStringKey", "interface.2": "Orleans.IRemindable", "diag.asm": "TheLibrary", "diag.type": "TheLibrary.Grains.UserGrain,TheLibrary"]
Consider using the [GrainType("name")] attribute to give these classes unique names.'
(X
is my game and TheLibrary
is the signal backplane library, I redacted yesterday in Discord out of habit)
I want the library to expose a way to set the grain names of its internal grains, so I can override it to signalrusergrain
from my game server.
Does this make sense or am I misunderstanding the error/going about the fix the wrong way?
from orleans.
@michaelmcneilnet in this case, you likely also want to make sure to override the interface type name (eg, IUserGrain
), so you'd do something very similar to the above for a IGrainInterfaceTypeProvider
implementation.
You could also consider namespacing your type names using the attributes, eg, [GrainType("my-lib.user")]
& [GrainInterfaceType("my-lib.IUserGrain")]
from orleans.
That's a good point about the interface, too.
I thought about setting them in the library directly with attributes, but I was worried about any possible breaking changes for existing library consumers. Can you think of any issues doing this?
from orleans.
You're right, this would be a breaking change if the references are persisted or if a rolling upgrade is performed (i.e, the usual upgrade strategy).
from orleans.
I think what I'll do is use your code above, and set the default going forward to be the fully namespaced name. That way, existing users can override this to keep the original name to avoid the breaking change.
I will update this thread when it's done to let you know if the code above worked as expected. If it does, might it be worth adding it to the docs in my original post as an alternative to the attribute?
from orleans.
Related Issues (20)
- Bug: Exclude inaccessible interface methods from codegen HOT 3
- `IGrainReferenceActivatorProvider` exception with interfaces written in F# HOT 1
- [Question] Orlean grain does not allow concurrent read HOT 2
- Typo in silo start log HOT 1
- Awaiting this.AsReference call causes deadlock in Orleans 7+ HOT 5
- [Question] How to connect Orleans Client to Orleans Server in Docker? HOT 3
- Issues with streams during rolling deploy (Orleans.Streams.QueueCacheMissException)
- In Orleans 8.1.0, after 90 seconds, the Silo and Gateway ports will change from the status LISTEN to CLOSED, which prevents further connections.
- Cannot serialize type inheriting from Dictionary HOT 3
- Pulling agents don't work properly with MemoryStreams on multiple silos
- Bug: Invalid code generated for interface with explicit implementations
- [Proposal] Let `AddSiloBuilderConfigurator` has an overload which accept an `Action` to configure cluster
- Serialization issues #2 - Orleans 8.0.0
- Serialization issues #3 - Orleans 8.0.0
- UnregisterReminder(string reminderName)
- Tracing: Recorded flag not propagated between activities HOT 6
- Graceful shutdown
- ObjectDisposedException at Orleans.Runtime.GrainTimer.ForwardToAsyncCallback HOT 1
- Add Roslyn PublicApiAnalyzers to all src projects
- PersistentStreamPullingAgent skips over the message under a certain condition
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from orleans.