Reputation: 1214
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
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
Upvotes: 2
Views: 670
Reputation: 4144
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
Upvotes: 1
Reputation: 3964
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.
The client generates a proxy class to call the service according to the endpoint of the server.
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:
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();
}
Upvotes: 5