Joshy
Joshy

Reputation: 657

Sharing WCF Service Contracts with Unity

I am fairly new with WCF, I'm looking to share a service contract and data contracts between service in one solution and a service consumer in another solution. I have created a seperate project which is shared between both solutions that contains both the service contracts and the data contracts. When i reference my service The data contracts are being shared but not the service contract. My contract looks like the following:

[ServiceContract]
public interface IDistributionService
{
    [OperationContract]
    [ServiceKnownType(typeof (Distribution))]
    bool AddDistributions(Distribution[] oDistributions);
}

When I look at the reference.cs file I is using the Distribution objects but it's generating the IDistributionService contract again, here is an excerpt of the reference.cs file:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="eDistributorService.IDistributionService")]
public interface IDistributionService {
   [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IDistributionService/AddDistributions", ReplyAction="http://tempuri.org/IDistributionService/AddDistributionsResponse")]
    bool AddDistributions(mhcb.syd.eDistribution.WebServices.Contracts.DTO.Distribution[] oDistributions);
    }

Is there any way of getting the consumer to use the IDistributionService interface without generating the concrete instance in the reference.cs file?

The main reason I want to know is so that I can use dependency injection (unity).

.RegisterType<IDistributionService, DistributionServiceClient>(
                new Interceptor<InterfaceInterceptor>(),
                new InterceptionBehavior(container.Resolve<IInterceptionBehavior>()))

Thanks again!

Upvotes: 0

Views: 1509

Answers (2)

Sebastian Weber
Sebastian Weber

Reputation: 6806

The code artefacts svcutil produces suck. But they are by far the easiest (i.e. cheapest) way to use the decoupling that WCF provides. Reusing contracts and entities is tight coupling of service and consumer.

If you want your container to generate client-side proxies for WCF services you can use an extension for Unity that does the trick. It supports the WCF4 discovery features as well.

Upvotes: 0

Kirk Broadhurst
Kirk Broadhurst

Reputation: 28698

Firstly check that you are instructing the service reference tool to 'reuse types'. When you go to 'Add Service Reference...', click 'Advanced' and confirm that the 'Reuse types in referenced assemblies' option is checked.

If this is checked (which I believe is the default behaviour), then you might not be able to do what you want by adding adding a 'service reference'.

Sharing a contract is the preferred WCF pattern (in my opinion), but the implementation I normally take is to use the ChannelFactory class to instantiate the client connection rather than use the generated proxy. The generated proxy is fine - it's quick and easy - but it is clunky and requires maintenance.

Review this MSDN link on Using ChannelFactory vs Proxies in WCF for a discussion, and this CodeProject link for sample usage.

Upvotes: 1

Related Questions