Microsoft Orleans — Code Generation issue?
Tags: dotnetcore, microsoftorleans, csharp, troubleshooting
Published: 2018-11-03 14:27:00

---
title: Microsoft Orleans — Code Generation issue?
published: true
tags: dotnet-core,microsoft-orleans,csharp,troubleshooting
canonical_url: https://medium.com/@kritner/microsoft-orleans-code-generation-issue-c294bd17b65a
series: Microsoft Orleans
---

### Microsoft Orleans — Code Generation issue?

While working on the post “[Microsoft Orleans — Reporting Dashboard](https://medium.com/@kritner/microsoft-orleans-reporting-dashboard-16465d255199?source=friends_link&sk=42db313c0775bef7cd9d8a55865bf033)”, I ran into an issue where code generation seemingly stopped “generating”.

Here’s a compare of when Code Generation was working (pre the start of previously stated post), and when it stopped working while writing the post:

[Kritner-Blogs/OrleansGettingStarted](https://github.com/Kritner-Blogs/OrleansGettingStarted/compare/2ebd478cba2f5a6a6c679b8b6e6c56caf5518f11...fe51806c602273afcd29ae3b126e9b964ad21659)

As you can see, not much had changed between the two, but for whatever reason, the SiloHost stopped being able to instantiate instances of my grains, or at a minimum the Client was saying that the SiloHost couldn’t.

When the issue is not present (like in [commit](https://github.com/kritner-blogs/OrleansGettingStarted/commits/2ebd478cba2f5a6a6c679b8b6e6c56caf5518f11)), running the application looks like:

![](https://cdn-images-1.medium.com/max/1024/0*TLWQpPccjkhIX_kZ.png)

When It is present, (like in [commit](https://github.com/kritner-blogs/OrleansGettingStarted/commits/fe51806c602273afcd29ae3b126e9b964ad21659)) it looks like:

![](https://cdn-images-1.medium.com/max/1024/0*OtkzzvNwD8ywyiVw.png)

I have opened a [GitHub issue](https://github.com/dotnet/orleans/issues/5154) to try to get clarification on what I’m experiencing; but in the meantime, there is a workaround.

### The Workaround

I was under the impression that projects containing the Microsoft.Orleans.OrleansCodeGenerator.Build NuGet package would automatically run code generation, though that didn’t seem to always be the case as per the github issue I submit above.

In order to get around the code generation not firing, I took a few different steps. (Note, I’m guessing some of these steps could be omitted, but I just mostly tried to brute force it, and once it was working left it alone):

- Utilize the .ConfigureApplicationParts method on both the ClientBuilder and SiloBuilder.
- Have grain interfaces implement an empty interface, for registration within the ClientBuilder.
- Have grain implementations implement an empty interface, for registration within the SiloBuilder.

#### Grains/Grain Interface changes

The interface I used for the grain interfaces, was created under Kritner.OrleansGettingStarted.GrainInterfaces:

```cs
public interface IGrainInterfaceMarker { }
```

The grain implementation interface was created under Kritner.OrleansGettingStarted.Grains:

```cs
public interface IGrainMarker { }
```

The grain interfaces I have — IHelloWorld, and IVisitTracker were updated to additionally implement IGrainInterfaceMarker. The same was done for the grain implementations, but implement IGrainMarker.

#### ClientBuilder and SiloHostBuilder Updates

Now that our grains and interfaces are implementing their new respective interface markers, we just need to register those interface types on the ClientBuilder and SiloHostBuilder as so:

ClientBuilder:

```cs
.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(IGrainInterfaceMarker).Assembly).WithReferences())
```

SiloHostBuilder:

```cs
.ConfigureApplicationParts(parts =>
{
 parts.AddApplicationPart(typeof(IGrainMarker).Assembly).WithReferences();
})
```

Now that these changes have been made, the grains are being successfully called on the cluster from the client! Hooray!

The commit where this correction was put in place is located here: [https://github.com/Kritner-Blogs/OrleansGettingStarted/pull/6/commits/9c271f085d22a66f8e8d3c3165eda863e5269508](https://github.com/Kritner-Blogs/OrleansGettingStarted/pull/6/commits/9c271f085d22a66f8e8d3c3165eda863e5269508)

#### GitHub Issue

I heard back from Sergey in the GitHub Issue:

[Question: What triggers code generation? · Issue #5154 · dotnet/orleans](https://github.com/dotnet/orleans/issues/5154#issuecomment-435214896)

Basically putting in the Orleans Dashboard, effectively turned off the “automatic scanner” I was taking advantage of previously. The act of adding the Orleans Dashboard called into AddApplicationPart, under the covers, which in turn turned off automatic scanning. I guess the reasoning behind this was “the developer is registering something (even though it was Orleans Dashboard), so there’s no need to do automatic scanning for the grains to be picked up.

Related:

- [GitHub Issue](https://github.com/dotnet/orleans/issues/5154)
- [Getting Started with Microsoft Orleans](https://medium.com/@kritner/getting-started-with-microsoft-orleans-882cdac4307f?source=friends_link&sk=1fc3451d71a19dcb49f2c8bbeb6b079e)
- [Microsoft Orleans — Reusing Grains and Grain State](https://medium.com/@kritner/microsoft-orleans-reusing-grains-and-grain-state-136977facd42?source=friends_link&sk=f19cfa3f17665c3d700bfe0df56e27a9)
- [Updating Orleans Project to be more ready for new Orleans Examples!](https://medium.com/@kritner/updating-orleans-project-to-be-more-ready-for-new-orleans-examples-2105b29a46fd?source=friends_link&sk=61d1f591e5b5c498688439db50ad310e)
- [Microsoft Orleans — Reporting Dashboard](https://medium.com/@kritner/microsoft-orleans-reporting-dashboard-16465d255199?source=friends_link&sk=42db313c0775bef7cd9d8a55865bf033)
- Workaround commit — [https://github.com/Kritner-Blogs/OrleansGettingStarted/pull/6/commits/9c271f085d22a66f8e8d3c3165eda863e5269508](https://github.com/Kritner-Blogs/OrleansGettingStarted/pull/6/commits/9c271f085d22a66f8e8d3c3165eda863e5269508)