简体   繁体   English

在客户端和服务器wcf服务上使用自定义数据类型

[英]Usage of custom data types on client and server wcf service

I have a custom datatype I put in a class Library SharedTypes 我有一个自定义数据类型我放在类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 然后我有一个WCF服务器与此服务

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 他们是一样的,但VS不知道

So I get errors that the interface is not properly implemented because I have 2 different types of the District class. 所以我得到的错误是接口没有正确实现,因为我有2种不同类型的District类。

How can I get the Service reference to use the SharedTypes.District instead? 如何获取Service引用以使用SharedTypes.District? Or is it my way of implementing it that is way off? 或者是我实现它的方式是什么?

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. 确保已将SharedTypes.District添加到客户端服务引用项目中。

When adding your WCF reference on the client side. 在客户端添加WCF引用时。 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). 您将能够指定程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM