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

标签 c# wcf

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

我尝试过的

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

        [OperationContract]
        string Get(int value);
    }
}  

接口(interface)实现

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";
        }
    }
}  

CompanyDetail 类

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;
        }
    }
}  

我已经尝试使用 [DataMember] 装饰器自动属性并且喜欢这个完整的 Prop 。但仍然没有任何效果。并在我的客户端中获取它。

结果

enter image description here

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

Adding classes to WCF service library
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts

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

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

添加服务引用对话框

enter image description here

最佳答案

我用你的代码创建了一个 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();
            }
        }
    }
}

这是服务器端代码。

enter image description here

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

enter image description here

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

更新

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

enter image description here

这是 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();
        }

关于C# 自定义类在 WCF 服务中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62738394/

相关文章:

c# - 事件处理程序和空条件运算符

c# - 十进制值的正则表达式

c# - 使用 WCF 在 .NET 3.5 中定义路由

wcf - 更改命名空间前缀 WCF 信封

wcf - 在 IIS 6.0 中运行时,PUT 和 DELETE 在 WCF REST 服务 .net 4 中获得 404

c# - 我如何加密任何用户在使用应用程序时都可以解密的存储密码?

c# - 服务器抛出异常。 (HRESULT : 0x80010105 (RPC_E_SERVERFAULT)) 的异常

C# Newtonsoft JSON - 使用未知对象集合反序列化对象

wcf - 需要帮助了解 Microsoft Service Trace Viewer 日志

wcf - 有没有办法同时使用 WCF 和 .NET Remoting?