2

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

19
  • can you post the IntelliServiceClient class please? Jul 5, 2020 at 9:09
  • @DurgaPrasad IntelliServiceClient from where do i get this ? this is auto generated by Service i guess. i only get this as an instance to call my service methods in my client program.
    – Hammas
    Jul 5, 2020 at 9:11
  • I was thinking in lines that you might have created a client class manually to connect to the service. Sorry. Jul 5, 2020 at 9:20
  • 1
    Your use of WCF declarative attributes is correct, and completely fine. WCF is not the cause of your problem. This is caused by some kind of visual studio issue. Jul 5, 2020 at 19:56
  • 1
    Good to hear, done below
    – Menahem
    Jul 8, 2020 at 19:51

2 Answers 2

5

I created a WCF service with your code and it ran successfully.

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 = "[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";
        }
    }
    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();
            }
        }
    }
}

This is the server-side code.

enter image description here

The client generates a proxy class to call the service according to the endpoint of the server.

enter image description here

I successfully called the GetCompanyDetails method and printed the value of the City property.

UPDATE

Based on your description I created a UWP program to call WCF as shown below:

enter image description here

This is 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();
        }
8
  • Interesting. And thanks for effort. But in my case when i remove that CompanyDetail class and it's method then it works. Also have you tried it other way ?
    – Hammas
    Jul 6, 2020 at 17:07
  • i mean my client is UWP app. And i'm using add connected service way to add service in my client and calling method like you also did.
    – Hammas
    Jul 6, 2020 at 17:11
  • but it doesn't work in my case ! could it be client UWP problem only ? or i've Dot Net 5 prev installed on my system could that be causing an issue (althoughy doesn't seem logical) but..
    – Hammas
    Jul 6, 2020 at 17:12
  • also i wrote Service in .Net 4.8 .
    – Hammas
    Jul 6, 2020 at 17:13
  • 1
    I created a UWP to call WCF service is also successfully called, you can refer to my method of calling WCF service with UWP,I have updated my reply.
    – Ding Peng
    Jul 7, 2020 at 6:59
1
+50

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

2
  • Just to tell you my client is a UWP app. So it might be a problem only with UWP cuz i remember it works fine in WPF. i'll test it more. But thanks for final thing that actually worked :)
    – Hammas
    Jul 8, 2020 at 19:55
  • 1
    Thats very similar to what is described in the answer, so now we know why for a fact. I recommend you move your contracts to a seperate .net standard 2.0 assembly
    – Menahem
    Jul 8, 2020 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.