Missing Published Classes on Service Reference C# - c#

First time here, never had the need to ask for anything due it was always easy to find the answer in other questions made.
The problem i'm facing:
I've just added the ServiceLayer which is going to publish services correspondant to BusinesLogicLayer.
So this is my code:
using Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace ServiceLayer
{
[ServiceContract]
public interface IServiceEmployees
{
[OperationContract]
void AddEmployee(Employee emp);
[OperationContract]
void DeleteEmployee(int id);
[OperationContract]
void UpdateEmployee(Employee emp);
[OperationContract]
List<Employee> GetAllEmployees();
[OperationContract]
Employee GetEmployee(int id);
[OperationContract]
double CalcPartTimeEmployeeSalary(int idEmployee, int hours);
}
}
Next file:
using BusinessLogicLayer;
using Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace ServiceLayer
{
public class ServiceEmployees : IServiceEmployees
{
private static IBLEmployees blHandler;
public ServiceEmployees()
{
blHandler = Program.blHandler;
}
public void AddEmployee(Employee emp)
{
blHandler.AddEmployee(emp);
}
public void DeleteEmployee(int id)
{
blHandler.DeleteEmployee(id);
}
public void UpdateEmployee(Employee emp)
{
blHandler.UpdateEmployee(emp);
}
public List<Employee> GetAllEmployees()
{
return blHandler.GetAllEmployees();
}
public Employee GetEmployee(int id)
{
return blHandler.GetEmployee(id);
}
public double CalcPartTimeEmployeeSalary(int idEmployee, int hours)
{
return blHandler.CalcPartTimeEmployeeSalary(idEmployee, hours);
}
}
}
Next file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Shared.Entities
{
[DataContract]
[KnownType(typeof(FullTimeEmployee))]
[KnownType(typeof(PartTimeEmployee))]
public abstract class Employee
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public DateTime StartDate { get; set; }
}
}
Next Files:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Shared.Entities
{
[DataContract]
public class FullTimeEmployee : Employee
{
[DataMember]
public int Salary { get; set; }
}
}
Last File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace Shared.Entities
{
[DataContract]
public class PartTimeEmployee : Employee
{
[DataMember]
public double HourlyRate { get; set; }
}
}
Allright, those are the ones i need to get published in a wsdl to consume later instead of accessing in PresentationLayer to BusinessLayer and DataLayer.
I've managed to get the published those OperationContracts without problems, and
when i add the url as an service reference on the PresentationLayerWinForm , i get the following.
wsdl description
I need to see the Employee.cs, PartTimeEmployee, FullTimeEmployee clases as well in that description but i dont get them although they are defined in the wsdl.
wsdl_2
What could possibly went wrong ?
Ive cleaned my proyect, rebuiled it several times and i still get the same thing, and i need those clases te get published so i errase the reference to shared.entities from PresentationLayerWinForm
Thanks 4 helping,

Problem Solved;
The issue was that my server-side DataContract class is named the same as a class i'm using local to the client of the web service, so Visual Studio re-uses types by default. Right-click on the Service Reference, Click on Configure and turn off Type re-use.
Thanks Anyway.

Related

public variable in App class can't get referenced in other class xamarin forms

I have this as my App.xaml.cs
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using System.Threading.Tasks;
using Xamarin.Forms.Xaml;
using Newtonsoft.Json;
using System.Net.Http;
namespace MyShop
{
public partial class App : Application
{
public string productsJSON = {{very large string}};
public List<Product> jsonDataCollection = new List<Product>();
public App()
{
InitializeComponent();
jsonDataCollection = JsonConvert.DeserializeObject<List<Product>>(productsJSON);
foreach (Product p in jsonDataCollection)
{
Application.Current.Properties[p.Id] = 0;
}
MainPage = new NavigationPage(new Page1());
}
...
In my ProductsPage.xaml.cs, I want to reference the public variable productsJSON so I call it as App.productsJSON and assigned to a string variable productsJSON.
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Newtonsoft.Json;
using System.Net.Http;
namespace MyShop
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ProductsPage : ContentPage
{
public ProductsPage(string title)
{
InitializeComponent();
Title = title;
string productsJSON = App.productsJSON;
displayProducts(productsJSON);
}
...
For that, I get the error:
CS0120: An object reference is required for the non-static field, method, or property 'App.productsJSON'
I'm confused because from the solutions online when referencing a property from another class, you just call the class then dot, and then the property name. I don't quite get the object reference error and it didn't work when I casted it to string like <string>App.productsJSON
you need a reference to the instance of the App class in order to access a non-static field or property
var json = ((App)Application.Current).productsJSON;

Getting error in Asp.net core when implementing TodoList service

I'm following on some tutorial which creates TodoList and uses multiple layer structure to handle different business logic. When I copy code from the book it shows me an error when I fire dotnet run command in windows cmd. The error I get is displayed bellow so I can specify where lies the problem but don't know how to fix this. What can I do to make this work ? I'm using dotnet 3.0.100 version.
Services\FakeTodoItemService.cs(21,17): error CS0117: 'FakeTodoItemService' does
not contain a definition for 'Title' [C:\dotnetproject\AspNetCoreTodo\AspNetCor
eTodo\AspNetCoreTodo.csproj]Services\FakeTodoItemService.cs(22,17): error CS0117: 'FakeTodoItemService' doesn not contain a definition for 'DueAt' [C:\dotnetproject\AspNetCoreTodo\AspNetCor
eTodo\AspNetCoreTodo.csproj]
And here is a code which I think may be responsible for this error. Basically I have a service that implements interface which uses a model.
FakeTodoItemService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AspNetCoreTodo.Data;
using AspNetCoreTodo.Models;
namespace AspNetCoreTodo.Services
{
public class FakeTodoItemService : ITodoItemService
{
public Task<TodoItem[]> GetIncompleteItemsAsync()
{
var item1 = new TodoItem
{
Title = "Learn ASP.NET Core",
DueAt = DateTimeOffset.Now.AddDays(1)
};
var item2 = new FakeTodoItemService
{
Title = "Build awesome apps",
DueAt = DateTimeOffset.Now.AddDays(2)
};
return Task.FromResult(new[] {item1, item2});
}
}
}
TodoItem.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace AspNetCoreTodo.Models
{
public class TodoItem
{
public Guid Id {get; set;}
public bool IsDone {get; set;}
[Required]
public string Title {get; set;}
public DateTimeOffset? DueAt {get; set;}
}
}
ITodoItemService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AspNetCoreTodo.Models;
namespace AspNetCoreTodo.Services
{
public interface ITodoItemService
{
Task<TodoItem[]> GetIncompleteItemsAsync();
}
}
In FakeTodoItemService.GetIncompleteItemsAsync you have this line var item2 = new FakeTodoItemService. FakeTodoItemService should be TodoItem.

C# - serializing list works, but deserializing returns empty list

I have an odd problem with XML deserialization in C#. Serializing the object works as expected, but deserializing makes the list-valued attribute Files empty.
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Text;
namespace Converter
{
[Serializable]
[XmlRoot("userinput")]
public class Input
{
[XmlArray("files")]
[XmlArrayItem(Type = typeof(FilePair), ElementName = "filepair")]
public List<FilePair> Files;
public Input()
{ }
}
}
and the element:
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace Converter
{
[Serializable]
public class FilePair
{
[XmlElement("file1")]
public string File1;
[XmlElement("file2")]
public string File2;
public FilePair() { }
}
public enum FileSource { Foo, Bar };
}
and a sample XML:
<userinput>
<files>
<filepair>
<file1>foo</file1>
<file2>bar</file2>
</filepair>
<filepair>
<file1>foo</file1>
<file2>bar</file2>
</filepair>
</files>
</userinput>
If I take your code, fix the [XmlElement("file2")], and use:
var xml = #"<userinput>
<files>
<filepair>
<file1>foo</file1>
<file2>bar</file2>
</filepair>
<filepair>
<file1>foo</file1>
<file2>bar</file2>
</filepair>
</files>
</userinput>";
using (var reader = new StringReader(xml))
{
var obj = (Input)new XmlSerializer(typeof(Input)).Deserialize(reader);
foreach(var file in obj.Files)
{
Console.WriteLine($"{file.File1}, {file.File2}");
}
}
then: it works. The output is:
foo, bar
foo, bar
So: whatever the problem is: you've fixed it when creating your minimal sample.
So now the question becomes: what is different between your minimal sample and your real code? Answer that, and you'll answer the question for yourself.

Migrations.cs is not created successfully

I am creating new module called "sms" in orchard cms using webmatrix. I create it successfully but when i generate "migrateions.cs", it doesn't generated successfully.
my sms.cs class in Model is given below
using System.Collections.Generic;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
namespace SMSs.Model{
public class smsrecord:ContentPartRecord
{
public virtual int ID{get;set;}
public virtual string Name{get;set;}
public virtual char is_deleted{get;set;}
}
public class smspart:ContentPart<smsrecord>
{
[Required]
public int ID
{
get{return ID=Record.ID;}
set{Record.ID=value;}
}
public string Name
{
get{return Name=Record.Name;}
set{Record.Name=value;}
}
public char is_deleted
{
get{return is_deleted=Record.is_deleted;}
set{Record.is_deleted=value;}
}
}
and the generated Migrations.cs class is as follow
using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
namespace sms {
public class Migrations : DataMigrationImpl {
public int Create() {
return 1;
}
}
}
the "migrations.cs" is not generated successfully why?? Please help
Class itself is generated properly, although it lacks code for creating appropriate tables because you didn't adhere to naming conventions for your record class.
Data migration code generation requires you to follow several conventions in order for it to work properly. I.e.:
Namespace of a record must end with .Models or .Records
There has to exist a public property named Id
All properties have to be virtual (required by NHibernate anyway)
Class cannot be sealed
Class cannot be abstract
Class has to implement IContent or be a subclass of ContentPartRecord
In your case, the namespace (should end with .Models) and incorrect casing of ID (should be Id) are the culprits.

How do I reference an input field in another project in WCF asp.net?

I have 2 projects in a solution. One called RentalService and one called RentalClient.
I'm trying to use the RentalClient to input data (Rate, Days) which will be sent to RentalService to be processed and return Price by multiplying rate by days.
Here is the code behind for the service:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace RentalService
{
[ServiceContract]
public interface ICalcPrice
{
[OperationContract]
CalcPrice CalculatePrice(double price);
}
[DataContract]
public class CalcPrice
{
[DataMember]
public double Rate {get; set;}
[DataMember]
public int Days {get; set;}
[DataMember]
public double price {get; set;}
}
}
and here is the service code: I didn't complete it because I'm stumped :/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using RentalClient;
namespace RentalService
{
public class CalcPrice : ICalcPrice
{
public txtRate.text
}
}
I'm trying to use the RentalClient to input data (Rate, Days) which
will be sent to RentalService to be processed and return Price by
multiplying rate by days
You need to define a method which accepts Rate and Days as parameter. Define that method in your interface ICalcPrice and the implement in your class CalcPrice
[ServiceContract]
public interface ICalcPrice
{
[OperationContract]
CalcPrice CalculatePrice(double price);
[OperationContract]
CalcPrice CalculatePrice(double price, int days);
}
Then in your class:
public class CalcPrice : ICalcPrice
{
public CalcPrice CalculatePrice(double price, int days)
{
//your logic
}
Each time you make changes on your contract on your server-side, don't forget to update your references on your client side.
This will build you a new wsdl, which will contain the updates of your Contract. After this you will be able to use your CalculatePrice method with 2 params.

Categories

Resources