Search code examples
c#wcf

C# Custom Class is not working in WCF service


Problem
I've a new WCF service. Which have an Interface and related class that has only two methods as can be seen in following code. I'm using a custom class CompanyDetail in one of the method but problem occurs and i reference this service in my client project but nothing works. Service is connected but can't use in code.

What i've tried

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

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

Interface Implementation

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 = "[email protected]",
                    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 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;
        }
    }
}  

I've tried auto properties with [DataMember] decorators and like this full props. But still nothing works. and get this in my Client.

Result

enter image description here

I've already looked here for help but it seems i'm not doing anything wrong.

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

Observation
When i remove this method of GetCompnyDetails then things work fine and i can use other method in my client. Also in past i used to have EntityFramework auto generated models in service and that used to work fine.

Expected Behavior
Obviously i should be able to use all methods in my client proj. And this custom class should work which is causing the issue.

Add Service Reference Dialog

enter image description here


Solution

  • It look s like the custom type cannot be referenced from your client project.

    Maybe the custom type is in a Full. NET project and the clinet is. Net core.

    Try to configure the reference to not reuse referenced types

    So they will be generated in your client project and can be used.

    Another option is to put your custom types and contracts in a separate. Net standard 2.0 project, and reverence it from both client and service. The you can leave the 'reuse' checkbox checked.

    The reuse makes sense in most cases where you have an in-solution reference, that's why its pre-selected.

    Here is the link to the offical doc