WCF/WPF Connection via ServiceReference
Hello, somehow i cant add a servicereference to my wpf from my wcf ... please help ... it says something with my metadata isnt working properly
WCF
ISERVICE1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
List<WeatherDataDTO> GetWeatherData(RangeFilter rangeFilter);
}
[DataContract]
public class RangeFilter
{
[DataMember]
public string From { get; set; }
[DataMember]
public string To { get; set; }
public RangeFilter() { }
public RangeFilter(string From, string To)
{
this.From = From;
this.To = To;
}
}
[DataContract]
public class WeatherDataDTO
{
[DataMember]
public int Id { get; set; }
// send as string because serialization error with DateTime
[DataMember]
public string Date { get; set; }
[DataMember]
public string Time { get; set; }
[DataMember]
public double Temperature { get; set; }
[DataMember]
public int Pressure { get; set; }
[DataMember]
public int Rain { get; set; }
[DataMember]
public double Wind { get; set; }
[DataMember]
public int Direction { get; set; }
[DataMember]
public int Humidity { get; set; }
}
---------------------------------------------------------------
Service1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
private TestEntities context = new TestEntities();
public List<WeatherDataDTO> GetWeatherData(RangeFilter rangeFilter)
{
List<WeatherDataDTO> weatherDataDTOs = new List<WeatherDataDTO>();
var start = parseDateTime(rangeFilter.From);
var end = parseDateTime(rangeFilter.To);
var query =
from weatherData in context.WeatherData
where weatherData.Datum >= start && weatherData.Datum <= end
select weatherData;
var i = 0;
foreach (var weatherData in query)
{
weatherDataDTOs.Add(new WeatherDataDTO
{
Id = weatherData.Id,
Date = weatherData.Datum.ToString(),
Direction = weatherData.Richtung.Value,
Humidity = weatherData.Feuchtigkeit.Value,
Pressure = weatherData.Luftdruck.Value,
Rain = weatherData.Regen.Value,
Temperature = weatherData.Temperatur.Value,
Time = weatherData.Zeit.Value.ToString(),
Wind = weatherData.Wind.Value
});
// Limit because of error: The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.
if (i == 100)
{
break;
}
i++;
}
return weatherDataDTOs;
}
// parse back string (send as string because serialization error with DateTime)
private DateTime parseDateTime(string dateString)
{
return DateTime.Parse(dateString);
//var args = datestring.split('.');
//return new datetime(
// convert.toint32(args[0]),
// convert.toint32(args[1]),
// convert.toint32(args[2])
//);
}
}
}
---------------------------------------------------------------
WPF
<Grid>
<DataGrid x:Name="grid" HorizontalAlignment="Left" Height="288" Margin="10,121,0,0" VerticalAlignment="Top" Width="772" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" ClipboardContentBinding="{x:Null}" Header="Id"/>
<DataGridTextColumn Binding="{Binding Date}" ClipboardContentBinding="{x:Null}" Header="Date"/>
<DataGridTextColumn Binding="{Binding Time}" ClipboardContentBinding="{x:Null}" Header="Time"/>
<DataGridTextColumn Binding="{Binding Temperature}" ClipboardContentBinding="{x:Null}" Header="Temperature"/>
<DataGridTextColumn Binding="{Binding Pressure}" ClipboardContentBinding="{x:Null}" Header="Pressure"/>
<DataGridTextColumn Binding="{Binding Rain}" ClipboardContentBinding="{x:Null}" Header="Rain"/>
<DataGridTextColumn Binding="{Binding Wind}" ClipboardContentBinding="{x:Null}" Header="Wind"/>
<DataGridTextColumn Binding="{Binding Direction}" ClipboardContentBinding="{x:Null}" Header="Direction"/>
<DataGridTextColumn Binding="{Binding Humidity}" ClipboardContentBinding="{x:Null}" Header="Humidity"/>
</DataGrid.Columns>
</DataGrid>
<Label Content="FROM:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<DatePicker x:Name="fromPicker" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="58,11,0,0"/>
<Label Content="TO:" HorizontalAlignment="Left" Margin="28,41,0,0" VerticalAlignment="Top"/>
<DatePicker x:Name="toPicker" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="58,40,0,0"/>
<Button Content="Search" HorizontalAlignment="Left" Margin="10,72,0,0" VerticalAlignment="Top" Width="75" Click="Search_Button_Click"/>
</Grid>
---------------------------------------------------------------
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ServiceReference1.IService1 client = new ServiceReference1.Service1Client();
public MainWindow()
{
InitializeComponent();
init();
}
public void init()
{
this.fromPicker.SelectedDate = new DateTime(2013, 1, 1);
this.toPicker.SelectedDate = new DateTime(2013, 1, 1);
update();
}
public void update()
{
var from = this.fromPicker.SelectedDate.ToString().Split(' ')[0];
var to = this.fromPicker.SelectedDate.ToString().Split(' ')[0];
var filter = new RangeFilter
{
From = from,
To = to
};
var data = this.client.GetWeatherData(filter);
this.grid.ItemsSource = data;
}
private void Search_Button_Click(object sender, RoutedEventArgs e)
{
update();
}
}
}
---------------------------------------------------------------
Java
DataStore
public class DataStore {
private static DataStore instance;
private Set<Pupil> pupils = new HashSet<>();
private DataStore() {
// Todo: put test data here
Pupil pupil1 = new Pupil(UUID.randomUUID(), "Tobias", "Emhofer", LocalDate.parse("2002-10-28"), "20170005");
Pupil pupil2 = new Pupil(UUID.randomUUID(), "Franz", "Fruehwirth", LocalDate.parse("2003-07-09"),"20170000");
Absence absence1 = new Absence(UUID.randomUUID(), LocalDate.parse("2022-03-10"), LocalDate.parse("2022-03-11"), "Zaz nd",false);
pupil1.addAbsence(absence1);
Absence absence2 = new Absence(UUID.randomUUID(), LocalDate.parse("2022-03-13"), LocalDate.parse("2022-03-14"), "a wuascht",false);
pupil2.addAbsence(absence2);
pupils.add(pupil1);
pupils.add(pupil2);
}
public void setPupils(Set<Pupil> pupils) {
this.pupils.addAll(pupils);
}
public static DataStore getInstance() {
if (instance == null) {
instance = new DataStore();
}
return instance;
}
public Set<Pupil> getPupils(){
return pupils;
}
public Optional<Pupil> getPupilById2(UUID id) {
return pupils.stream()
.filter(pupil -> pupil.getId().equals(id))
.findFirst();
}
public Pupil getPupilById(UUID id) {
for (Pupil pupil : pupils) {
if (pupil.getId().equals(id)) {
return pupil;
}
}
return null;
}
}
---------------------------------------------------------------
Pupil
public class Pupil {
private UUID id;
private String firstName;
private String lastName;
private LocalDate dateOfBirth;
private String studentNumber;
private Set<Absence> absences = new HashSet<>();
public Pupil(UUID id, String firstName, String lastName, LocalDate dateOfBirth, String studentNumber) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.studentNumber = studentNumber;
}
public Pupil() {
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
public Set<Absence> getAbsences() {
return absences;
}
public void setAbsences(Set<Absence> absences) {
this.absences = absences;
}
public void addAbsence(Absence absence) {
absences.add(absence);
}
public Absence getAbsenceById(UUID id) {
for (Absence absence : absences) {
if (absence.getId().equals(id)) {
return absence;
}
}
return null;
}
@Override
public String toString() {
return "Pupil{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", dateOfBirth=" + dateOfBirth +
", studentNumber='" + studentNumber + '\'' +
", absences=" + absences +
'}';
}
}
---------------------------------------------------------------
Absence
public class Absence {
private UUID id;
private LocalDate from;
private LocalDate to;
private String reason;
private boolean excused;
public Absence() {
id = UUID.randomUUID();
}
public Absence(UUID id, LocalDate from, LocalDate to, String reason, boolean excused) {
this.id = id;
this.from = from;
this.to = to;
this.reason = reason;
this.excused = excused;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public LocalDate getFrom() {
return from;
}
public void setFrom(LocalDate from) {
this.from = from;
}
public LocalDate getTo() {
return to;
}
public void setTo(LocalDate to) {
this.to = to;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public boolean isExcused() {
return excused;
}
public void setExcused(boolean excused) {
this.excused = excused;
}
@Override
public String toString() {
return "Absence{" +
"id=" + id +
", from=" + from +
", to=" + to +
", reason='" + reason + '\'' +
", excused=" + excused +
'}';
}
}
---------------------------------------------------------------
HelloApp
@ApplicationPath("/api")
public class HelloApplication extends Application {
}
---------------------------------------------------------------
PupilResource
@mutia keyza ("/pupil")
public class PupilResource implements IPupilResource {
DataStore dataStore = DataStore.getInstance();
@Override
public Set<Pupil> getAll() {
return dataStore.getPupils();
}
@Override
public Optional<Pupil> getOnePupil(UUID id) {
return dataStore.getPupilById2(id);
}
@Override
public Absence newAbsence(String id, Absence absence) {
DataStore.getInstance().getPupilById(UUID.fromString(id)).addAbsence(absence);
return DataStore.getInstance().getPupilById(UUID.fromString(id)).getAbsenceById(absence.getId());
}
@Override
public Absence getAbsenceById(String stId, String abId) {
return DataStore.getInstance().getPupilById(UUID.fromString(stId)).getAbsenceById(UUID.fromString(abId));
}
@Override
public boolean excuseAbsence(String stId, String abId) {
DataStore.getInstance().getPupilById(UUID.fromString(stId)).getAbsenceById(UUID.fromString(abId)).setExcused(true);
return DataStore.getInstance().getPupilById(UUID.fromString(stId)).getAbsenceById(UUID.fromString(abId)).isExcused();
}
}
---------------------------------------------------------------
ImportData
public class ImportData implements IImportData {
@Override
public Set<Pupil> importDataFromJSON(Set<Pupil> pupils) {
DataStore.getInstance().setPupils(pupils);
return DataStore.getInstance().getPupils();
}
}
---------------------------------------------------------------
IPUPILREs
@mutia keyza ("/pupil")
public interface IPupilResource {
@get
@Produces(MediaType.APPLICATION_JSON)
Set<Pupil> getAll();
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{pupil_id}")
Optional<Pupil> getOnePupil(@PathParam("pupil_id") UUID id);
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{pupil_id}/absence")
Absence newAbsence(@PathParam("pupil_id") String id,
Absence absence);
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{pupil_id}/absence/{absence_id}")
Absence getAbsenceById(@PathParam("pupil_id") String stId,
@PathParam("absence_id") String abId);
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{pupil_id}/absence/{absence_id}/excuse")
boolean excuseAbsence(@PathParam("pupil_id") String stId,
@PathParam("absence_id") String abId);
}
---------------------------------------------------------------
IImportData
@mutia keyza ("/importData")
public interface IImportData {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Set<Pupil> importDataFromJSON(Set<Pupil> pupils);
}
---------------------------------------------------------------
index.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<br/>
<a href="${pageContext.request.contextPath}/PupilListServlet">Pupil List</a>
</body>
</html>
---------------------------------------------------------------
PupilServlet
@WebServlet(name = "PupilListServlet", value = "/PupilListServlet")
public class PupilServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("pupils", new TrackingClient(DataStore.getInstance().getPupils()));
request.getRequestDispatcher("pupillist.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
---------------------------------------------------------------
pupillist.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="pupils" class="com.example.demo.TrackingClient" scope="request"/>
<html>
<head>
<title>Pupil list</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/AbsenceServlet" method="get">
<select name="pupilselect">
<% for (Pupil pupil : pupils.getPupils()) {%>
<option value="<%=pupil.getId()%>"><%=pupil.getFirstName() + " " + pupil.getLastName()%></option>
<%}%>
</select>
<input type="submit" name="Submit">
</form>
</body>
</html>
---------------------------------------------------------------
AbsenceServlet
@WebServlet(name = "AbsenceServlet", value = "/AbsenceServlet")
public class AbsenceServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s = request.getParameter("pupilselect");
request.setAttribute("a", DataStore.getInstance().getPupilById(UUID.fromString(s)));
request.getRequestDispatcher("absencelist.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
---------------------------------------------------------------
absencelist.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="a" class="model.Pupil" scope="request"/>
<html>
<head>
<title>Absence List</title>
</head>
<body>
<table>
<% for (Absence absence : a.getAbsences()) {%>
<tr>
<td><%=absence.toString()%>
</td>
</tr>
<%}%>
</table>
</body>
</html>
---------------------------------------------------------------
Trackingclient
public class TrackingClient {
private Set<Pupil> pupils = new HashSet<>();
public TrackingClient() {
}
public TrackingClient(Set<Pupil> pupils){
this.pupils = pupils;
}
public Set<Pupil> getPupils() {
return pupils;
}
public void setPupils(Set<Pupil> pupils) {
this.pupils = pupils;
}
}