suedeuno
suedeuno

Reputation: 405

reference methods from interface through a WCF service

Say I have the following:

namespace SomeProject
{
    public interface ISomething
    {
        string SomeMethod();
    }
}

namespace SomeService
{
    [DataContract]
    public class CompositeType
    {
        [DataMember]
        public ISomething Something { get; set; }
    }
}

Now, in my calling code I want to be able to do this:

SomeService service = new SomeService();
service.Something.SomeMethod();

SomeMethod() is not available unless I return the DataMember as the implementation rather than the interface. Is there a way to do this?

Upvotes: 2

Views: 3171

Answers (4)

Manuel Roldan
Manuel Roldan

Reputation: 525

After the service reference been added, you have to use an instance of the client that implements the target interface for that service.

For example, lets say your Interface is called ICalculator. The auto generated code when its service reference is added, should look like this:

// Define a service contract.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);
    // Other methods are not shown here.
}

Now, in order to use its operations, you just create an instance of the class that implements that interface, which also has been autogenerated. Here is an example:

// Create a client object with the given client endpoint configuration.
CalculatorClient calcClient = new CalculatorClient("CalculatorEndpoint"));
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = calcClient.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

Upvotes: 0

Tad Donaghe
Tad Donaghe

Reputation: 6588

I strongly suggest you go through some basic WCF tutorials so that you can get a hang of what WCF actually does.

Check out the O'Reilly books, "Learning WCF" by Michele Bustamante and more importantly "Programming WCF Services 2nd Edition" by Juval Lowy.

WCF is a complicated beast and I suggest that you really need to get a firm foundation before you try writing code.

Upvotes: 0

Brian Genisio
Brian Genisio

Reputation: 48157

This is not how you want to use your WCF service. WCF is about transferring data, not implementation. You are confusing your client and your service layers by doing this.

However, if you really want to do this, you can tell the proxy generator in the client to re-use any existing types... this means that you can

  1. reference the "SomeProject" dll in your client
  2. add the service reference
  3. choose "Advanced"
  4. Select "Reuse types in referenced assemblies"
  5. Choose where you want to get the types from

Again, I do not recommend doing it this way.

Upvotes: 2

GalacticCowboy
GalacticCowboy

Reputation: 11759

Your WCF "DataContract" is exactly that - a data contract. On your client side you have a proxy object that implements all of the data members, but none of the methods. You'd have to have the original class/interface definitions and reconstruct the full object on the client side in order to do something like this.

Upvotes: 3

Related Questions