Mech0z
Mech0z

Reputation: 3647

Usage of custom data types on client and server wcf service

I have a custom datatype I put in a class Library SharedTypes

namespace SharedTypes
{
    public class District
    {
        public long Id { get; set; }
        public string Name { get; set; }
    }
}

I then have a WCF server with this service

using System.ServiceModel;
using SharedTypes;

namespace WCF.WCFInterfaces
{
    [ServiceContract]
    public interface IWcfService
    {
        [OperationContract]
        District GetDistrict(long id);

        [OperationContract]
        void CreateDistrict(District district);

        [OperationContract]
        List<District> GetDistricts();
     }
}

On the client side I have a Interface (So I inject the implementation)

using SharedTypes;

namespace WcfInterfaces
{
    public interface IDistrictManager
    {
        void CreateDistrict(District district);
        District GetDistrict(long id);
        List<District> GetDistricts();
    }
}

I finally have the implementation the client should use

public class DistrictManager : IDistrictManager
{
    private readonly WcfServiceClient _salesService;
    public DistrictManager()
    {
        _salesService = new WcfServiceClient();
    }

    public void CreateDistrict(District district)
    {
        _salesService.CreateDistrictAsync(district);
    }

    public District GetDistrict(long id)
    {
        return _salesService.GetDistrict(id);
    }

    public List<District> GetDistricts()
    {
        var list = _salesService.GetDistricts();
        return list.ToList();
    }
}

But here the problem arises, this implementation expects to use a version of District it gets from the service reference

WcfClientLibrary.SalesService.District

Instead of

SharedTypes.District

They are the same, but VS dont know that

So I get errors that the interface is not properly implemented because I have 2 different types of the District class.

How can I get the Service reference to use the SharedTypes.District instead? Or is it my way of implementing it that is way off?

Upvotes: 4

Views: 2072

Answers (2)

daryal
daryal

Reputation: 14929

Right click your service reference in client project and check "Reuse Types in Referenced Assemblies".

Be sure that you have added SharedTypes.District to your client service reference project.

Upvotes: 3

scartag
scartag

Reputation: 17680

When adding your WCF reference on the client side. Click on the advanced options. There is a setting that you can specify to tell it to re-use types from specified assembly(s). You'll be able to specify the assembly(s).

Upvotes: 2

Related Questions