次の(例)インターフェイスを備えたWCFサービスがあります。
[ServiceContract]
[ServiceKnownType(typeof(Foo))]
[ServiceKnownType(typeof(Bar))]
[ServiceKnownType(typeof(Baz))]
public interface IMyInterface
{
[OperationContract]
void ProcessMessage(Message message);
[OperationContract]
void ProcessMessages(IEnumerable<Message> messages);
}
Foo
、Bar
およびBaz
はすべて のタイプですMessage
。
またはオブジェクトを使用して WCF クライアントから呼び出すことができ、すべて正常に動作しProcessMessage()
ます。ただし、これは失敗するため、配列(またはリストまたはその他)で呼び出すことはできません。Foo
Bar
Baz
ProcessMessages(...)
IEnumerable
パラメータhttp://tempuri.org/:messagesをシリアル化しようとしてエラーが発生しました 。InnerException メッセージは、「タイプ 'XXFoo' データ コントラクト名 'Foo: http://schemas.datacontract.org/2004/07/XX ' は想定されていません」でした。DataContractResolver の使用を検討するか、既知の型のリストに静的に認識されていない型を追加します。たとえば、KnownTypeAttribute 属性を使用するか、DataContractSerializer に渡される既知の型のリストにそれらを追加します。詳細については、InnerException を参照してください。
生成されたクライアント コードを見ると、次のように表示されreference.cs
ます。
...
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IMyInterface/ProcessMessage", ReplyAction="http://tempuri.org/IMyInterface/ProcessMessageResponse")]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(X.X.Foo))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(X.X.Bar))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(X.X.Baz))]
void ProcessMessage(X.X.Message message);
...
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IMyInterface/ProcessMessages", ReplyAction="http://tempuri.org/IMyInterface/ProcessMessagesResponse")]
void ProcessMessages(X.X.Message[] messages);
ServiceKnownTypeAttribute
s が に追加されていることに気付きましたProcessMessage
が、 ではありませんProcessMessages
。ServiceKnownTypeAttribute
メソッドに同じs のセットを手動で追加し、をProcessMessages
含む配列を使用してクライアントから呼び出すと、正常に動作します。Foo
Bar
Baz
Visual Studio で 2 番目のメソッドでもこれらの属性を生成するにはどうすればよいですか? 私がIMyInterface
間違っていますか?[ServiceKnownType(typeof(...))]
間違った場所に追加しましたか?どこで私は間違えましたか?
編集
おそらく、Message
クラスが "外部" アセンブリ (幸いなことに制御できます) にあり、Nuget パッケージにパッケージ化されていることを言及する必要があります。これは、WCF サービスとクライアントの両方で "再利用types...」オプションがこのアセンブリに対して有効になっています。