ASP.NET Core Web api + WCF

36 views Asked by At

I'm trying to get the employee I added via Get and I just get a blank sheet instead of what I expected. I just started studying recently and I don't quite understand why I did any wrong actions. Anyone with experience, please help https://github.com/mistelltein/WebApplicationWithWCF Here's my code, if anyone solved this problem before, i need your incructions

I rewrote the methods, reconnected to the WCF application, but nothing worked and still get a blank sheet when searching for an employee

1

There are 1 answers

0
QI You On

Yes, you can. You can use Corewcf in ASP.NET Core.

1.Add Nuget package:

CoreWCF.HTTP

2.Add code:

Program.cs:

builder.Services.AddServiceModelServices();
builder.Services.AddServiceModelMetadata();
builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
app.UseServiceModel(serviceBuilder =>
{
    serviceBuilder.AddService<Service>();
    serviceBuilder.AddServiceEndpoint<Service, IService>(new BasicHttpBinding(BasicHttpSecurityMode.Transport), "/Service.svc");
    var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
    serviceMetadataBehavior.HttpsGetEnabled = true;
});

IService.cs:

[ServiceContract]
 public interface IService
 {
     [OperationContract]
     string GetData(int value);

     [OperationContract]
     CompositeType GetDataUsingDataContract(CompositeType composite);
 }

 public class Service : IService
 {
     public string GetData(int value)
     {
         return string.Format("You entered: {0}", value);
     }

     public CompositeType GetDataUsingDataContract(CompositeType composite)
     {
         if (composite == null)
         {
             throw new ArgumentNullException("composite");
         }
         if (composite.BoolValue)
         {
             composite.StringValue += "Suffix";
         }
         return composite;
     }
 }

 // Use a data contract as illustrated in the sample below to add composite types to service operations.
 [DataContract]
 public class CompositeType
 {
     bool boolValue = true;
     string stringValue = "Hello ";

     [DataMember]
     public bool BoolValue
     {
         get { return boolValue; }
         set { boolValue = value; }
     }

     [DataMember]
     public string StringValue
     {
         get { return stringValue; }
         set { stringValue = value; }
     }
 }