C# 自定义类在 WCF 服务中不起作用

Rao*_*ain 2 c# wcf

问题
我有一个新WCF服务。其中有一个Interface相关的类,它只有两个方法,如下面的代码所示。我CompanyDetail在其中一种方法中使用自定义类,但出现问题,我在我的客户端项目中引用了此服务,但没有任何效果。服务已连接但无法在代码中使用。

我试过的

namespace IntelliWcfService
{
    [ServiceContract]
    public interface IIntelliService
    {
        [OperationContract]
        CompanyDetail GetCompanyDetails();

        [OperationContract]
        string Get(int value);
    }
}  
Run Code Online (Sandbox Code Playgroud)

接口实现

namespace IntelliWcfService
{
    public class IntelliService : IIntelliService
    {
        public CompanyDetail GetCompanyDetails()
        {
            try
            {
                var company = new CompanyDetail
                {
                    CompanyName = "Visual Labs Pakistan",
                    City = "Multan",
                    Contact1 = "0306-8513103",
                    Contact2 = "",
                    Country = "Pakistan",
                    CountryCode = "+92",
                    Email = "VisualLabs@gmail.com",
                    NTN = "0007923-7",
                    PostalCode = "60000",
                    Street = "Street 2 Near Bypass Multan",
                    Type = "Software Technology"
                };

                return company;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public string Get(int value)
        {
            return "Connected";
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)

公司详情类

using System.Runtime.Serialization;

namespace IntelliWcfService.Models
{
    [DataContract] 
    public class CompanyDetail
    {
        private string _companyName;
        private string _ntn;
        private string _type;
        private string _country;
        private string _countryCode;
        private string _city;
        private string _street;
        private string _postalCode;
        private string _contact1;
        private string _contact2;
        private string _email;

        [DataMember]
        public string CompanyName
        {
            get => _companyName;
            set => _companyName = value;
        }

        [DataMember]
        public string NTN
        {
            get => _ntn;
            set => _ntn = value;
        }

        [DataMember]
        public string Type
        {
            get => _type;
            set => _type = value;
        }

        [DataMember]
        public string Country
        {
            get => _country;
            set => _country = value;
        }

        [DataMember]
        public string CountryCode
        {
            get => _countryCode;
            set => _countryCode = value;
        }

        [DataMember]
        public string City
        {
            get => _city;
            set => _city = value;
        }

        [DataMember]
        public string Street
        {
            get => _street;
            set => _street = value;
        }

        [DataMember]
        public string PostalCode
        {
            get => _postalCode;
            set => _postalCode = value;
        }

        [DataMember]
        public string Contact1
        {
            get => _contact1;
            set => _contact1 = value;
        }

        [DataMember]
        public string Contact2
        {
            get => _contact2;
            set => _contact2 = value;
        }

        [DataMember]
        public string Email
        {
            get => _email;
            set => _email = value;
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)

我已经尝试过带有[DataMember]装饰器的自动属性,并且喜欢这个完整的道具。但仍然没有任何作用。并在我的客户端中获取此信息。

结果

在此处输入图片说明

我已经在这里寻求帮助,但似乎我没有做错任何事情。

将类添加到 WCF 服务库
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts

观察
当我删除此方法时,GetCompnyDetails一切正常,我可以在我的客户端中使用其他方法。同样在过去,我曾经EntityFramework在服务中使用自动生成的模型,并且过去工作正常。

预期行为
显然我应该能够在我的客户端项目中使用所有方法。这个自定义类应该可以工作,这会导致问题。

添加服务引用对话框

在此处输入图片说明

Din*_*eng 5

我用你的代码创建了一个 WCF 服务,它运行成功。

namespace ConsoleApp37
{
    [ServiceContract]
    public interface IIntelliService
    {
        [OperationContract]
        CompanyDetail GetCompanyDetails();

        [OperationContract]
        string Get(int value);
    }
    [DataContract]
    public class CompanyDetail
    {
        private string _companyName;
        private string _ntn;
        private string _type;
        private string _country;
        private string _countryCode;
        private string _city;
        private string _street;
        private string _postalCode;
        private string _contact1;
        private string _contact2;
        private string _email;

        [DataMember]
        public string CompanyName
        {
            get => _companyName;
            set => _companyName = value;
        }

        [DataMember]
        public string NTN
        {
            get => _ntn;
            set => _ntn = value;
        }

        [DataMember]
        public string Type
        {
            get => _type;
            set => _type = value;
        }

        [DataMember]
        public string Country
        {
            get => _country;
            set => _country = value;
        }

        [DataMember]
        public string CountryCode
        {
            get => _countryCode;
            set => _countryCode = value;
        }

        [DataMember]
        public string City
        {
            get => _city;
            set => _city = value;
        }

        [DataMember]
        public string Street
        {
            get => _street;
            set => _street = value;
        }

        [DataMember]
        public string PostalCode
        {
            get => _postalCode;
            set => _postalCode = value;
        }

        [DataMember]
        public string Contact1
        {
            get => _contact1;
            set => _contact1 = value;
        }

        [DataMember]
        public string Contact2
        {
            get => _contact2;
            set => _contact2 = value;
        }

        [DataMember]
        public string Email
        {
            get => _email;
            set => _email = value;
        }
    }
    public class IntelliService : IIntelliService
    {
        public CompanyDetail GetCompanyDetails()
        {
            try
            {
                var company = new CompanyDetail
                {
                    CompanyName = "Visual Labs Pakistan",
                    City = "Multan",
                    Contact1 = "0306-8513103",
                    Contact2 = "",
                    Country = "Pakistan",
                    CountryCode = "+92",
                    Email = "VisualLabs@gmail.com",
                    NTN = "0007923-7",
                    PostalCode = "60000",
                    Street = "Street 2 Near Bypass Multan",
                    Type = "Software Technology"
                };

                return company;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public string Get(int value)
        {
            return "Connected";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1: Create a URI to serve as the base address.
            Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");

            // Step 2: Create a ServiceHost instance.
            ServiceHost selfHost = new ServiceHost(typeof(IntelliService), baseAddress);

            try
            {
                // Step 3: Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(IIntelliService), new WSHttpBinding(), "CalculatorService");

                // Step 4: Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5: Start the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");

                // Close the ServiceHost to stop the service.
                Console.WriteLine("Press <Enter> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是服务器端代码。

在此处输入图片说明

客户端根据服务端的端点生成代理类来调用服务。

在此处输入图片说明

我成功调用了 GetCompanyDetails 方法并打印了 City 属性的值。

更新

根据您的描述,我创建了一个 UWP 程序来调用 WCF,如下所示:

在此处输入图片说明

这是 ButtonBase_OnClick:

private async void ButtonBase_OnClick(object sender, RoutedEventArgs e) {
            BasicHttpBinding binding = new BasicHttpBinding();
            EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8000/GettingStarted/CalculatorService");
            ServiceReference1.IntelliServiceClient intelliServiceClient = new ServiceReference1.IntelliServiceClient(binding,endpointAddress);
            ServiceReference1.CompanyDetail companyDetail= await intelliServiceClient.GetCompanyDetailsAsync();
            MessageDialog message = new MessageDialog(companyDetail.City);
            await message.ShowAsync();
        }
Run Code Online (Sandbox Code Playgroud)