icreated WCF service. My project name is SERVICE_ABC. In solution i created Service_A.svc, Service_B.svc, Service_C.svc and PersonelDTO.cs. All service using PersonelDTO.cs. Her is the code
public interface IService_A
{
[OperationContract]
void DoWork();
[OperationContract]
PersonelDTO GetMyData();
}
public interface IService_B
{
[OperationContract]
void DoWork();
[OperationContract]
PersonelDTO GetMyData();
}
public interface IService_C
{
[OperationContract]
void DoWork();
[OperationContract]
PersonelDTO GetMyData();
}
I read service WEB_ABC .net solution. In the HomeController.cs i have got Deneme() controller. how can i use PersonelDTO without ServiceReference_A service reference name
public string Deneme()
{
Service_AClient clientA = new Service_AClient();
Service_BClient clientB = new Service_BClient();
Service_CClient clientC = new Service_CClient();
/*Below these codes working properly
var DataA = clientA.GetMyData();
ServiceReference_B.PersonelDTO DataB = clientB.GetMyData();
ServiceReference_C.PersonelDTO DataC = clientC.GetMyData();
*/
//I want to use PersonelDTO without using ServiceReference_A service reference name
PersonelDTO DataA = clientA.GetMyData();
PersonelDTO DataB = clientB.GetMyData();
PersonelDTO DataC = clientC.GetMyData();
If you are talking about the only way you can use the DTO is through a service reference, then you need to make a classlib project named Entities. In Entities, you will put all of the DTO classes. All projects that need to use the DTO(s) will have project
reference to Entities and know what the DTO(s) are about. In this way, you can instance a DTO as
new without needing the service reference to the DTO.
And IMO, you don't reference the service in the controller and make calls to the service from the controller.
An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic.
<end>
An example and DTO(s) are being used too sending them through the tiers of the ntier solution that has WCF on the backend, which has the WebAPI sitting in front of the WCF service. The WebAPI using Json is to accommodate the client-side of a Web MVC solution
with the browsers and using JavaScript.
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Entities
{
[DataContract]
public class DTOStudent
{
private DTOResponse dtor = new DTOResponse();
[DataMember]
public Int32 StudentID { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public DateTime? EnrollmentDate { get; set; }
[DataMember]
public virtual ICollection<DTOEnrollandCourse> EnrollsandCourses { get; set; }
[DataMember]
public DTOResponse DtoResponse
{
get { return dtor; }
set { dtor = value; }
}
}
}
-------------------------------------------------------------------
using System.Web.Mvc;
using MVC.Models;
namespace MVC.Controllers
{
[Authorize]
public class StudentsController : BaseController
{
private IStudentModels studmods;
public StudentsController(IStudentModels studentModels)
{
studmods = studentModels;
}
// GET: Students
[AllowAnonymous]
public ActionResult Index()
{
return View(studmods.GetStudents());
}
//[AllowAnonymous]
public ActionResult Details(int id = 0)
{
return id == 0 ? null : View(studmods.GetStudentById(id));
}
public ActionResult Create()
{
return View(studmods.Create());
}
[HttpPost]
public ActionResult Create(StudentViewModels.Student student)
{
if (ModelState.IsValid)
{
studmods.Create(student);
return RedirectToAction("Index");
}
return View(student);
}
public ActionResult Edit(int id = 0)
{
return id == 0 ? null : View(studmods.Edit(id));
}
[HttpPost]
public ActionResult Edit(StudentViewModels.Student student)
{
if (ModelState.IsValid)
{
studmods.Edit(student);
return RedirectToAction("Index");
}
return View(student);
}
public ActionResult Delete(int id = 0 )
{
if (id > 0) studmods.Delete(id);
return RedirectToAction("Index");
}
}
}
-----------------------------------------------------------
using System.Collections.Generic;
using Entities;
using WebAPI.Controllers;
namespace MVC.Models
{
public class StudentModels : IStudentModels
{
private IStudentControllerAPI studapi;
public StudentModels(IStudentControllerAPI studentControllerApi)
{
studapi = studentControllerApi;
}
public StudentViewModels GetStudents()
{
var dtos = studapi.GetStudents();
var vm = new StudentViewModels {Students = new List<StudentViewModels.Student>()};
foreach (var dto in dtos)
{
var student = new StudentViewModels.Student
{
StudentID = dto.StudentID,
LastName = dto.LastName,
FirstName = dto.FirstName,
EnrollmentDate = dto.EnrollmentDate
};
vm.Students.Add(student);
}
return vm;
}
public StudentViewModels.Student GetStudentById(int id)
{
var dto = studapi.GetStudentById(id);
var student = new StudentViewModels.Student
{
StudentID = dto.StudentID,
FirstName = dto.FirstName,
LastName = dto.LastName,
EnrollmentDate = dto.EnrollmentDate,
EnrollsandCourses = new List<EnrollandCourseViewModel.EnrollandCourse>()
};
foreach (var dtoec in dto.EnrollsandCourses)
{
var ec = new EnrollandCourseViewModel.EnrollandCourse
{
Credits = dtoec.Credits,
Grade = dtoec.Grade,
Title = dtoec.Title
};
student.EnrollsandCourses.Add(ec);
}
return student;
}
public StudentViewModels.Student Create()
{
var student = new StudentViewModels.Student();
return student;
}
public void Create(StudentViewModels.Student student)
{
var dto = new DTOStudent
{
StudentID = student.StudentID,
FirstName = student.FirstName,
LastName = student.LastName,
EnrollmentDate = student.EnrollmentDate
};
studapi.CreateStudent(dto);
}
public StudentViewModels.Student Edit(int id)
{
var dto = studapi.GetStudentById(id);
var student = new StudentViewModels.Student
{
StudentID = dto.StudentID,
FirstName = dto.FirstName,
LastName = dto.LastName,
EnrollmentDate = dto.EnrollmentDate
};
return student;
}
public void Edit(StudentViewModels.Student student)
{
var dto = new DTOStudent
{
StudentID = student.StudentID,
FirstName = student.FirstName,
LastName = student.LastName,
EnrollmentDate = student.EnrollmentDate
};
studapi.UpdateStudent(dto);
}
public void Delete(int id)
{
studapi.DeleteStudent(id);
}
}
}
If you find the post has answered your issue, then please mark post as 'answered'.
As I stated in my first post, you must implement a WCF channel factory to shared DTO objects between WCF and MVC. Otherwise you must convert the service reference proxy back to the DTO on the MVC side.
I place the WCF interface and data contracts in a separate project that is shared by the WCF service and consumer project.
You might want to look into Web API as Web API is much easier to use.
i dont understand this code. İ share what i want to do
I gave it to you on what to do. Your DTO(s) are defined in the WCF service and are tightly coupled to the WCF service. You need to remove them out of the WCF service project placing them into an independent classlib project, and you set reference to the
independent classlib project. The MVC project and the WCF service project set project reference to the Entities project where the DTO(s) are kept.
You see the using statement being used in Model object Entities.
usingEntities;
You seeing Entities being used by the WCF service.
sing System;
using System.Collections.Generic;
using System.ServiceModel;
using Entities;
namespace WcfService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
DTOStudent GetStudentById(Int32 id);
[OperationContract]
List<DTOStudent> GetStudents();
[OperationContract]
DTOStudent CreateStudent(DTOStudent dto);
[OperationContract]
DTOStudent UpdateStudent(DTOStudent dto);
[OperationContract]
DTOStudent DeleteStudent(Int32 id);
[OperationContract]
DTOEnrollment GetEnrollmentById(Int32 id);
[OperationContract]
List<DTOEnrollment> GetEnrollments();
[OperationContract]
void CreateEnrollment(DTOEnrollment dto);
[OperationContract]
void UpdateEnrollment(DTOEnrollment dto);
[OperationContract]
void DeleteEnrollment(Int32 id);
}
}
--------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using Entities;
using Repository;
namespace WcfService
{
public class Service1 : IService1
{
private IStudentRepo _studentRepo;
private IEnrollmentRepo _enrollmentRepo;
private DTOStudent dto;
public Service1(IStudentRepo studentRepo, IEnrollmentRepo enrollmentRepo)
{
_studentRepo = studentRepo;
_enrollmentRepo = enrollmentRepo;
}
public DTOStudent GetStudentById(Int32 id)
{
try
{
return _studentRepo.GetStudentById(id);
}
catch (Exception e)
{
dto = new DTOStudent();
dto.DtoResponse.Message = e.Message;
if (e.InnerException != null) dto.DtoResponse.InnerException = e.InnerException.Message;
dto.DtoResponse.StackTrace = e.StackTrace;
return dto;
}
}
public List<DTOStudent> GetStudents()
{
try
{
return _studentRepo.GetStudents();
}
catch (Exception e)
{
var dtos = new List<DTOStudent>();
dto = new DTOStudent();
dto.DtoResponse.Message = e.Message;
if (e.InnerException != null) dto.DtoResponse.InnerException = e.InnerException.Message;
dto.DtoResponse.StackTrace = e.StackTrace;
dtos.Add(dto);
return dtos;
}
}
public DTOStudent CreateStudent(DTOStudent dto)
{
try
{
_studentRepo.CreateStudent(dto);
return null;
}
catch (Exception e)
{
dto = new DTOStudent();
dto.DtoResponse.Message = e.Message;
if (e.InnerException != null) dto.DtoResponse.InnerException = e.InnerException.Message;
dto.DtoResponse.StackTrace = e.StackTrace;
return dto;
}
}
public DTOStudent UpdateStudent(DTOStudent dto)
{
try
{
_studentRepo.UpdateStudent(dto);
return null;
}
catch (Exception e)
{
dto = new DTOStudent();
dto.DtoResponse.Message = e.Message;
if (e.InnerException != null) dto.DtoResponse.InnerException = e.InnerException.Message;
dto.DtoResponse.StackTrace = e.StackTrace;
return dto;
}
}
public DTOStudent DeleteStudent(Int32 id)
{
try
{
_studentRepo.DeleteStudent(id);
return null;
}
catch (Exception e)
{
dto.DtoResponse.Message = e.Message;
if (e.InnerException != null) dto.DtoResponse.InnerException = e.InnerException.Message;
dto.DtoResponse.StackTrace = e.StackTrace;
return dto;
}
}
public DTOEnrollment GetEnrollmentById(Int32 id)
{
return _enrollmentRepo.GetEnrollmentById(id);
}
public List<DTOEnrollment> GetEnrollments()
{
return _enrollmentRepo.GetEnrollments();
}
public void CreateEnrollment(DTOEnrollment dto)
{
_enrollmentRepo.CreateEnrollment(dto);
}
public void UpdateEnrollment(DTOEnrollment dto)
{
_enrollmentRepo.UpdateEnrollment(dto);
}
public void DeleteEnrollment(Int32 id)
{
_enrollmentRepo.DeleteEnrollment(id);
}
}
}
If you find the post has answered your issue, then please mark post as 'answered'.
You CANNOT assign the service reference types to shared types. They are two different types. You must either build a factory that converts the service reference type to the shared type on the client. Or use the channel factory to communicate with the
service and do NOT use a service reference.
thanks i add my DTO class to classlib. Its ok but my WEB is created different solution. I send you
There is nothing stopping from copying the DLL for the classlib project copying it to the Bin folder of the Web project and setting reference to the DLL that has the DTO.
If you find the post has answered your issue, then please mark post as 'answered'.
thanks i add my DTO class to classlib. Its ok but my WEB is created different solution. I send you
There is nothing stopping from copying the DLL for the classlib project copying it to the Bin folder of the Web project and setting reference to the DLL that has the DTO.
The issue is not how to create a project reference. The issue is the OP is trying to assign a proxy type to a shared type as shown in the screenshot.
thanks i add my DTO class to classlib. Its ok but my WEB is created different solution. I send you
There is nothing stopping from copying the DLL for the classlib project copying it to the Bin folder of the Web project and setting reference to the DLL that has the DTO.
The issue is not how to create a project reference. The issue is the OP is trying to assign a proxy type to a shared type as shown in the screenshot.
The issue as I see it is the OP is trying to use the DTO by service reference, and he can't do it. The simple thing to do is decouple the DTO from the service by using a classlib DLL to hold the DTO to eliminate the dependency. The OP should picked up
on the simple OO 101 concept..
If you find the post has answered your issue, then please mark post as 'answered'.
Member
2 Points
9 Posts
Sharing data contracts between wcf services without use ServiceReference name
Jan 30, 2018 06:58 AM|renegadecommendant|LINK
Hi
icreated WCF service. My project name is SERVICE_ABC. In solution i created Service_A.svc, Service_B.svc, Service_C.svc and PersonelDTO.cs. All service using PersonelDTO.cs. Her is the code
public interface IService_A
{
[OperationContract]
void DoWork();
[OperationContract]
PersonelDTO GetMyData();
}
public interface IService_B
{
[OperationContract]
void DoWork();
[OperationContract]
PersonelDTO GetMyData();
}
public interface IService_C
{
[OperationContract]
void DoWork();
[OperationContract]
PersonelDTO GetMyData();
}
I read service WEB_ABC .net solution. In the HomeController.cs i have got Deneme() controller. how can i use PersonelDTO without ServiceReference_A service reference name
public string Deneme()
{
Service_AClient clientA = new Service_AClient();
Service_BClient clientB = new Service_BClient();
Service_CClient clientC = new Service_CClient();
/*Below these codes working properly
var DataA = clientA.GetMyData();
ServiceReference_B.PersonelDTO DataB = clientB.GetMyData();
ServiceReference_C.PersonelDTO DataC = clientC.GetMyData();
*/
//I want to use PersonelDTO without using ServiceReference_A service reference name
PersonelDTO DataA = clientA.GetMyData();
PersonelDTO DataB = clientB.GetMyData();
PersonelDTO DataC = clientC.GetMyData();
return "Ok";
}
Thanks. You can download project this link
All-Star
52221 Points
23295 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 30, 2018 11:03 AM|mgebhard|LINK
You are asking how to connect to WCF without a service reference? If so, you can use the WCF channel factory.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
https://msdn.microsoft.com/en-us/library/ms734681(v=vs.100).aspx
Contributor
4873 Points
4124 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 30, 2018 11:16 AM|DA924|LINK
If you are talking about the only way you can use the DTO is through a service reference, then you need to make a classlib project named Entities. In Entities, you will put all of the DTO classes. All projects that need to use the DTO(s) will have project reference to Entities and know what the DTO(s) are about. In this way, you can instance a DTO as new without needing the service reference to the DTO.
And IMO, you don't reference the service in the controller and make calls to the service from the controller.
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs#understanding-models
<copied>
An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic.
<end>
An example and DTO(s) are being used too sending them through the tiers of the ntier solution that has WCF on the backend, which has the WebAPI sitting in front of the WCF service. The WebAPI using Json is to accommodate the client-side of a Web MVC solution with the browsers and using JavaScript.
Member
2 Points
9 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 30, 2018 11:42 AM|renegadecommendant|LINK
i dont understand this code. İ share what i want to do
All-Star
52221 Points
23295 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 30, 2018 11:52 AM|mgebhard|LINK
As I stated in my first post, you must implement a WCF channel factory to shared DTO objects between WCF and MVC. Otherwise you must convert the service reference proxy back to the DTO on the MVC side.
I place the WCF interface and data contracts in a separate project that is shared by the WCF service and consumer project.
You might want to look into Web API as Web API is much easier to use.
Contributor
4873 Points
4124 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 30, 2018 01:13 PM|DA924|LINK
i dont understand this code. İ share what i want to do
I gave it to you on what to do. Your DTO(s) are defined in the WCF service and are tightly coupled to the WCF service. You need to remove them out of the WCF service project placing them into an independent classlib project, and you set reference to the independent classlib project. The MVC project and the WCF service project set project reference to the Entities project where the DTO(s) are kept.
You see the using statement being used in Model object Entities.
using Entities;
You seeing Entities being used by the WCF service.
Member
2 Points
9 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 31, 2018 08:42 AM|renegadecommendant|LINK
thanks i add my DTO class to classlib. Its ok but my WEB is created different solution. I send you pictures.
Member
2 Points
9 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 31, 2018 11:32 AM|renegadecommendant|LINK
thanks. i clean and rebuild all solution and close all visual studio. Its work fine.
All-Star
52221 Points
23295 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 31, 2018 11:34 AM|mgebhard|LINK
For the third and final time!
You CANNOT assign the service reference types to shared types. They are two different types. You must either build a factory that converts the service reference type to the shared type on the client. Or use the channel factory to communicate with the service and do NOT use a service reference.
See the docs.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
https://msdn.microsoft.com/en-us/library/ms734681(v=vs.100).aspx
Contributor
4873 Points
4124 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 31, 2018 11:44 AM|DA924|LINK
thanks i add my DTO class to classlib. Its ok but my WEB is created different solution. I send you
There is nothing stopping from copying the DLL for the classlib project copying it to the Bin folder of the Web project and setting reference to the DLL that has the DTO.
All-Star
52221 Points
23295 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 31, 2018 11:58 AM|mgebhard|LINK
The issue is not how to create a project reference. The issue is the OP is trying to assign a proxy type to a shared type as shown in the screenshot.
Contributor
4873 Points
4124 Posts
Re: Sharing data contracts between wcf services without use ServiceReference name
Jan 31, 2018 01:53 PM|DA924|LINK
The issue as I see it is the OP is trying to use the DTO by service reference, and he can't do it. The simple thing to do is decouple the DTO from the service by using a classlib DLL to hold the DTO to eliminate the dependency. The OP should picked up on the simple OO 101 concept..