CompanyDroneFromSector7G
CompanyDroneFromSector7G

Reputation: 4527

WCF: How to have implementation logic in a separate project to contract interface?

like the title says, I want to keep my logic in a separate project to the service contract(s) so this can stay separate and hidden. I've seen it said that this can be done, but I can't find any examples as to how.

I moved all the necessary code out to a different project, but when I test it only the types are visible to the client, not the methods.

All the methods are marked as [OperationContract] in the interface.

I'm guessing there must be a way of specifically connecting the implementation logic to the contract other than just Class : IInterface, otherwise, what would happen if there were 2 similar classes, e.g.: Class1 : IInterface and Class2 : IInterface? How would WCF know which class to use?

Upvotes: 3

Views: 6255

Answers (2)

AakashM
AakashM

Reputation: 63378

How would WCF know which class to use?

Because you tell it, in the various configuration files.


I can heartily recommend WCF the Manual Way…the Right Way for motivation and a complete walkthrough for what you are trying to do - what I have here is just the pertinent points of such an implementation.

You have three assemblies: Service, Client, ServiceContracts (interfaces only). Service and Client both reference ServiceContracts. Service contains classes implementing the interfaces. Client has proxy classes:

using System.ServiceModel;
using ServiceContracts;

public class ExampleServiceProxy : ClientBase<IExampleService>, IExampleService
{
    public string ExampleMethod()
    {
        return Channel.ExampleMethod();
    }
}

The client's config file contains an entry pointing at the service's svc file; the service's svc file looks like this:

<%@ ServiceHost Language="C#" Debug="true" Service="Service.ExampleService" 
    CodeBehind="ExampleService.svc.cs" %>

And the service .svc.cs file looks like:

using ServiceContracts;

public class ExampleService : IExampleService
{
    public string ExampleMethod()
    {
        return string.Empty;
    }
}

That's it!

Upvotes: 3

Giedrius
Giedrius

Reputation: 8550

I've done it this way (sorry for bad names, that's just for concept):

1) Created separate project for implementation
2) Created separate project for service contract (to avoid circular references)
3) Added service contract project as reference to implementation project
4) Added contract and implementation project to host project
5) Updated Service.svc with full type name

enter image description here

Service.svc body:

<%@ ServiceHost Language="C#" Debug="true" Service="Implementation.ServiceImplementation" %>

Upvotes: 11

Related Questions