Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:56:00+00:00 2026-05-15T23:56:00+00:00

I’m trying to implement a service contract that contains a method which takes a

  • 0

I’m trying to implement a service contract that contains a method which takes a generic interface, and that generic interface itself is given an interface parameter. I’ve decorated the service interface with ServiceKnownType, I have decorated the service implementation with regular KnownType, and I have decorated the datacontract implementation with regular KnownType:

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallbacks))]
[ServiceKnownType(typeof(Batch<object>))]
[ServiceKnownType(typeof(Command))]
public interface IActions
{
    [OperationContract]
    IResponse TakeAction(IBatch<ICommand> commands);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
[KnownType(typeof(Batch<object>))]
[KnownType(typeof(Command))]
internal class Actions : IActions
{
}

[DataContract]
[KnownType(typeof(Command))]
public class Batch<T> : IBatch<T>
{
}

For the record, I have Batch there because it seems that you can only express a knowntype for a generic type once–it appears to emit BatchOfanyType, but I’m not sure how to handle this.

The exception I’m getting is “Add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.”

Is there anything obvious I’m doing wrong? Are generic interfaces of interfaces just not supported? For the record I’m on C# 2.0 and .NET 3.0 for this project.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:56:01+00:00Added an answer on May 15, 2026 at 11:56 pm

    You can use interfaces in service contract definitions if you really want to, as long as you’re including the known types as you are doing (with a slight adjustment, see below).

    Apparently, using an interface as the generic type parameter is taking it a bridge too far for C# 3.0. I changed the known type attribute to

    [ServiceKnownType(typeof(Batch<Command>))]
    public interface IActions
    {
    }
    

    Which makes it work, to a point. Serialization and deserialization itself will work, but then you’re faced with this exception:

    Unable to cast object of type ‘Batch`1[Command]’
    to type ‘IBatch`1[ICommand]’.

    For that cast to work, you need language support for generic type covariance, something that’s introduced in C# 4.0. For it to work in C# 4.0 though, you’d need to add a variance modifier:

    public interface IBatch<out T>
    {
    }
    

    Then it works perfectly… unfortunately you’re not using C# 4.0.

    One last thing about using interfaces in your service contract: if you’re generating a service reference from them, it will type all the interface parameters as object, becase the original interface type isn’t part of the metadata. You can share contracts through an assembly reference, or manually refactor the generated proxy to fix it, but all in all, using interfaces with WCF is probably more trouble than it’s worth.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.