0

Working with WCF service I met the following problem: calling the service from a client (simple console application), I can not access the method of a class marked with [DataContract] and [ServiceContract], even if this method is marked with [OperationContract] (actually, I've tried all the possible combinations of attributes so far :D ). Is there a way to resolve it? I'm missing some points here, I guess, but still can't handle it, need one's help ^^ Here is the code of a class:

[ServiceContract]
[DataContract]
public class AmountSpecification : IOrderSpecification
{
    [DataMember]
    public int Amount {get ; set;}

    public AmountSpecification(int amount)
    {
        Amount = amount;
    }
    public bool IsSatisfiedBy(Order o)
    {
        return o.Amount >= Amount;
    }

    [OperationContract]
    public IOrderSpecification And(IOrderSpecification specification)
    {
        return new AndSpecification(this, specification);
    }

}

2 Answers2

0

You're mixing up service contract and data contract in one class. You only need the DataContract and DataMember attributes to create a data contract, and those aren't even required.

However, when you generate a service reference from a service using this data contract, and the project you're adding the service reference to doesn't have a reference to the assembly where AmountSpecification is defined, it will generate a data contract with only the given name and properties.

If you want to use the class in its enitrety, you have to reference the assembly that class is defined in in your client appliaction, and check "Reuse types in referenced assemblies" in your service reference configuration. See Service reference complex types.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thank you for the comment, but I've already did that :( Doesn't work for me unfortunately – Galina Bezobrazova Dec 24 '13 at 13:27
  • "Doesn't work" is not an error. Remove the service reference, add a reference to the assembly where `AmountSpecification` is defined and recreate the service reference with the "Reuse types from referenced assemblies" checked. – CodeCaster Dec 24 '13 at 13:30
  • It is already done :) Getting "...AmountSpecification doesn't contain the definition for 'And'..." – Galina Bezobrazova Dec 24 '13 at 13:35
  • Where do you go when you right-click `AmountSpecification` and select Go To Definition (F12)? Did you rebuild the project before updating the service reference? – CodeCaster Dec 24 '13 at 13:37
  • I go to the auto-generated file serialized within the console project. And yes, sure I rebuilded the project – Galina Bezobrazova Dec 24 '13 at 14:00
  • @Galina that means you haven't configured it, or that the reference was not made or could not be loaded. Are you sure you referenced the correct assembly or project? Do you have any warnings in the error list? – CodeCaster Dec 24 '13 at 14:16
  • no warnings, simply nothing :( Other methods of the service work like a charm, that's the only headache I've got – Galina Bezobrazova Dec 24 '13 at 14:20
0

Your method returns IOrderSpecification.

So, on the client side, there is no indication on which implementation is used, (AmountSpecification , AndSpecification, etc) and so the client will not create the correct instance type.

You need to add Well Known Types attribute on your interface. This will allow the wsdl to contains definitions for the implementation, and so the client will have the information.

Fabske
  • 2,106
  • 18
  • 33