---
lang: ja-jp
breaks: true
---
# gRPC MagicOnion `ServiceBase<TServiceInterface>` のインターフェイスにジェネリックパラメータを使いたい 2021-09-16
## `ServiceBase<TServiceInterface>`のインターフェイスにジェネリックパラメータを使用すると、サービス起動時の`endpoints.MapMagicOnionService()`にて、`ArgumentException: Type XXXXXXX contains generic parameters Arg_ParamName_Name` が出力される。
残念。。何か方法はないのか。。。
:::info
インターフェイスメソッドにジェネリックパラメータを使用することもできないし、サービスクラスにジェネリックパラメータを使用する事も出来ない。。。
:::
## これは、`protobuf-net.Grpc`でも同様にジェネリックパラメータを使用できない。
## MagicOnionのソースを確認したところ、クライアント・サービス側共に対応していない模様。
Expresson Tree でゴリゴリ処理している部分で ジェネリック型引数を元に パラメータ式を作成できずにエラーとなっていた。
## gibhub issues には、ジェネリックサービスクラスを`abstract`に設定し、非ジェネリッククラスで継承した`sealed`クラスで対応したとの書き込みあり。
> Support generic types in IService<> #146
> https://github.com/Cysharp/MagicOnion/issues/146
> https://github.com/Cysharp/MagicOnion/issues/146#issuecomment-492660334
```csharp=
public sealed class Resolver : FuncResolver<Message>
{
public override async UnaryResult<Task> ProcessRemotely(Message message, NodeMetrics nodeMetrics)
{
return Task.Delay(125);
}
}
public abstract class FuncResolver<T> : ServiceBase<IRemoteContract<T>>, IRemoteContract<T>
{
public virtual UnaryResult<Task> ProcessRemotely(T item, NodeMetrics nodeMetrics)
{
throw new NotImplementedException();
}
}
public interface IRemoteContract<T>
{
}
public class NodeMetrics
{
}
```
```shell=
thanks, I should wrote discover service problems to ReadMe.
There is one problem with using Generics.
Currently, MagicOnion creates gRPC path from interface and does not resolve closed generics.
for example path will be .
This means can not create many service definitions in one generics service interface.IRemoteContract<T>.ProcessRemotely
I'll change to resolve path of generics interface(it will be , etc...).IRemoteContract_Message.ProcessRemotely
```
:::info
インターフェイスクラス及びメソッド名により、httpのURLが作成される為、実行時に動的に型が決定するような使い方は想定されていない模様。
:::
###### tags: `MagicOnion` `gRPC` `ServiceBase<TServiceInterface>` `ジェネリックパラメータ`