簡體   English   中英

C# 自定義 Class 在 WCF 服務中不起作用

[英]C# Custom Class is not working in WCF service

問題
我有一個新的WCF服務。 其中有一個Interface和相關的 class 只有兩種方法,如以下代碼所示。 我在其中一種方法中使用自定義 class CompanyDetail但出現問題,我在我的客戶項目中引用了此服務,但沒有任何效果。 服務已連接但無法在代碼中使用。

我試過的

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

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

接口實現

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

公司詳情 Class

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]裝飾器的自動屬性,並且喜歡這個完整的道具。 但仍然沒有任何效果。 並在我的客戶中得到這個。

結果

在此處輸入圖像描述

我已經在這里尋求幫助,但似乎我沒有做錯任何事情。

向 WCF 服務庫添加類
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts

觀察
當我刪除GetCompnyDetails的這種方法時,一切正常,我可以在我的客戶端中使用其他方法。 同樣在過去,我曾經有EntityFramework自動生成的模型在服務中,並且以前可以正常工作。

預期行為
顯然我應該能夠在我的客戶端項目中使用所有方法。 這個自定義 class 應該可以工作,這會導致問題。

添加服務參考對話框

在此處輸入圖像描述

我用您的代碼創建了一個 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();
            }
        }
    }
}

這是服務器端代碼。

在此處輸入圖像描述

客戶端根據服務端的端點生成代理class調用服務。

在此處輸入圖像描述

我成功調用了 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();
        }

看起來自定義類型無法從您的客戶端項目中引用。

也許自定義類型是完整的。 NET 項目和 clinet 是。 網絡核心。

嘗試將引用配置為不重用引用類型

因此它們將在您的客戶端項目中生成並可以使用。

另一種選擇是將您的自定義類型和合同放在一個單獨的地方。 NET 標准 2.0 項目,並受到客戶和服務的尊重。 您可以選中“重用”復選框。

在大多數情況下,當您有解決方案中的參考時,重用是有意義的,這就是預先選擇它的原因。

這是官方文檔的鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM