none
Problem w/WCF Service for an ASP.NET MVC4 Web Application RRS feed

  • Question

  • I have created the WCF Service that will be used in conjunction w/an ASP.NET MVC 4 Web Application.

    In the WCF Service I have the following code (in IService.vb file):

    <ServiceContract()>
    Public Interface IService
    
        ' TODO: Add your service operations here
        <OperationContract()>
        Function GetProjects() As List(Of Project)
    End Interface

    as well as (in the Service.vb file):

    Public Class Service
        Implements IService
    
        Private db As New VEASPNET38Entities
    
        Public Sub New()
        End Sub
    
        '
        ' GET: /Project/
        Function GetProjects() As List(Of Project) Implements IService.GetProjects
    
            Return db.Projects.ToList()
    
        End Function
    
    End Class

    The WCF Service Web.config file contains:

    <protocolMapping>
          <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>

    In the MVC 4 Web Application I have the following code (in the ProjectController.vb file that will ultimately contain all the functions for the Project Class):

    Public Class ProjectController
        Inherits System.Web.Mvc.Controller
    
        '
        ' GET: /Project/
        Function Index() As ActionResult
            Dim someobj As New ServiceReference1.ServiceClient()
    
            Return View(someobj.GetProjects())
    
        End Function
    
    End Class

    The MVC 4 Web Application Web.config file contains:

    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IService" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://localhost:49471/Service.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
            name="BasicHttpBinding_IService" />
        </client>
      </system.serviceModel>

    When I run (CTRL-PF5) the WCF Service and i debug (PF5) the MVC 4 Web Application I see the following results when I go to http://localhost:50564/Project (which is the MVC 4 Web Application)

    * The View for Project/Index loads up nicely when there is NO DATA in the DB:

    Or

    * An error when THERE IS DATA in the DB:

    What am I failing to specify as part of the service that causes the error to occur when there is actually data that should be displayed?

    Sunday, June 22, 2014 9:56 PM

Answers

  • http://msdn.microsoft.com/en-us/library/bb514135.aspx

    The true error message is being swallowed by WCF on the service side, and all you are getting is a generic error. Setting WCF service up for debugging to expose the error message in the config file setting can be done. But in general, you should read articles on how to throw error messages across WCF boundaries.

    Secondly, you are making calls to a typed-client, which should have a try/catch around the code similar to the try/catch you see in the link. Forget about what it is talking about with the 'Using" statement and look at the try/catches surrounding the code that makes the call. It's just FYI, but it should be done on all your code making the call to the service similar to the try/catch you see in the link. 

    http://msdn.microsoft.com/en-us/library/aa355056(v=vs.110).aspx

    And how does the client code even know what Project is if the object can't be seen by the client? The client has no reference to it? If that object is to be seen on both sides, then it needs to be in its own classlib project call it Entities and the client and service need to set reference to it so it can be seen by both sides. 

    You have gotten yourself into a mess, and if it was me and for right now, I would dump WCF out of the solution and just go to using the Entity Framework up at the MVC controller, if this solution is not using Access, because you have a lot to learn about using WCF. WCF is not the slam, bam thank you ma'am for you right now.

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

    There is probably some VB version of the above link somewhere. 

    Thursday, June 26, 2014 8:59 PM

All replies

  • Did you set the DataContract and DataMember attributes on your Project class?

    Deltazon

    Sunday, June 22, 2014 11:30 PM
  • Interestingly enough I do not have a Project Class in either the WCF Service or the Web Application.

    When I created the Project Controller in the Web Application I created an Empty Controller.  I modified the Index() Function to fit my needs.

    The Project/Index view expects the following: 

    @ModelType IEnumerable(Of MvcWcfApplication.ServiceReference1.Project)

    Let me note that I created an ADO.NET Entity Data Model in the WCF Service to add a "Model" (under the "App_Code" Folder).  I created this Model based on my DB (I included ALL the Tables in the DB).

    But nowhere do I have any "Class"es defined.  So what/how should I add the "Project" Class?

    I tried creating a CRUD Controller specifying Model Class "Project (MvcWcfApplication.ServiceReference1)" but I am not able to use the Data context class as I am not able to see the Model I created in the WCF Service.

    Sunday, June 22, 2014 11:54 PM
  • I take it back : I do have a "Model.vb" under the WCF Service's "App_Code".  It contains "Class"es for ALL of the Tables in the DB.

    Example:

    Partial Public Class Project
        Public Property ProjID As Integer
        Public Property Name As String
    End Class

    However, I am still unsure how to set the DataContract and DataMember attributes on your Project class?

    Monday, June 23, 2014 12:13 AM
  • But nowhere do I have any "Class"es defined.  So what/how should I add the "Project" Class?

    I tried creating a CRUD Controller specifying Model Class "Project (MvcWcfApplication.ServiceReference1)" but I am not able to use the Data context class as I am not able to see the Model I created in the WCF Service.

    You are using the wrong type of architecture here to be working with ASP.NET, MVC and WCF.

    You need to be using WCF with a classlib project call it DAL that has classes/objects that do CRUD operations with the database.

    You need a classlib project call it Service layer that is the layer the sits between the MVC controller and the WCF service. The single Service layer class/object is the client to the WCF service that calls the various CRUD operations the WCF service has in reference to the DAL that the WCF project has reference to. 

    The MVC controller calls upon the Service layer class/object to do CRUD operations with the WCF service that in turn calls upon the DAL to do CRUD operations with the database.

    DTO(s) must be used that must be marked as a datacontract with datamemeber attributes so the DTO(s) can pass through WCF. 

    http://en.wikipedia.org/wiki/Data_transfer_object

    The DTO is what you send through the layers to the MVC controller,  and the DTO must be sent back from the MVC controller through the Service layer all the way to the DAL to be persisted to the database.

    The DTO(s) are kept in a classlib project call it Entities,  and the MVC, Service Layer, WCF and DAL projects have reference to the Entities.

    Of course data has to be map to the DTO and DTO mapped back to the source in the DAL to persist data to the database.

    Monday, June 23, 2014 3:12 AM
  • So the WCF Service and Web Application do not have a trivially simple mechanism to interact.

    The Web Application has been developed (and ready to be deployed to the user community).  I was hoping to be able to add the WCF Service painlessly.  However, it sounds like there is (likely) a significant amount of work/change required.  Work/Change that could have possibly been avoided if the WCF Service framework was better understood.

    Let me then ask the obvious question then : what does the Web Application gain from having a WCF Service?

    The Web Application is a data collaboration tool that allows the user community to participate in the lifecycle of a Project to be carried out (say : we want to manufacture a better dining table. Whatever "better" means. This is a purely hypothetical example).  The Project is created, users are asked to participate by providing ideas that would make a better dining table.  The tool is really nothing more than a CRUD framework/GUI that allows such ideas to be tracked by the DB.

    Any and all functionality provided by the controllers associated w/the classes can only be reached if the user is authorized to access that particular controller.  Care and design has been put in place to ensure that level of authorization.  It is my understanding that WCF provides a layer of authorization/safety/security mechanism, hence the request to include it as part of the Web Application's technology.  Or does WCF Service provide some additional functionality/security otherwise not provided by simple Website authentication/authorization support?

    Monday, June 23, 2014 5:28 AM
  • So the WCF Service and Web Application do not have a trivially simple mechanism to interact.

    Well, one has to know what he or she is doing well before he or she gets to the point that you are at. And yes WCF is pretty simple to use if one knows what he or she is doing to implement WCF in a solution.

    The Web Application has been developed (and ready to be deployed to the user community).  I was hoping to be able to add the WCF Service painlessly.  However, it sounds like there is (likely) a significant amount of work/change required.  Work/Change that could have possibly been avoided if the WCF Service framework was better understood.

    There are plenty of tutorials not only about WCF,  but also what do in developing a n-tier architectured solution using WCF All of this could have been avoided. You could have built the solution from the ground up starting with WCF and a possible DAL, tested WCF and the DAL with a test-harness and known everything was going to be OK once you implemeneted it at the UI layer with MVC.

    You did just the opposit starting with the UI and MVC and tried to implement WCF into it, and it became  painful for you.

    The Web Application is a data collaboration tool that allows the user community to participate in the lifecycle of a Project to be carried out (say : we want to manufacture a better dining table. Whatever "better" means. This is a purely hypothetical example).  The Project is created, users are asked to participate by providing ideas that would make a better dining table.  The tool is really nothing more than a CRUD framework/GUI that allows such ideas to be tracked by the DB.

    This doesn't seem like a WCF solution is warranted here. Usually a WCF Web service would come into play here when you have a front-end Web server and a back-end Web server with the back-end application Web server in communications with a DB server. Objects are passed between the fornt-end application and the back-end application thru a WCF Web service and the communication link between the two servers -- client/server.

    Any and all functionality provided by the controllers associated w/the classes can only be reached if the user is authorized to access that particular controller.  Care and design has been put in place to ensure that level of authorization.  It is my understanding that WCF provides a layer of authorization/safety/security mechanism, hence the request to include it as part of the Web Application's technology.  Or does WCF Service provide some additional functionality/security otherwise not provided by simple Website authentication/authorization support?

    Yes,  WCF has several ways to authinticate a WCF client's credentials to use the WCF service. Also IIS has serveral ways to autenticate a Web client trying to use a Web service as well legacy or WCF Web service. But the securty you are talking about with WCF is about a WCF service that was on a public facing machine on the Internet that provided services to clients in a SOA environment as an example, like a brokerage firm providing SOA services to its clients.

    WCF is about SOA.

    http://www.javaworld.com/article/2071889/soa/what-is-service-oriented-architecture.html

    <copied>

    Service-oriented architecture (SOA) is an evolution of distributed computing based on therequest/reply design paradigm for synchronous and asynchronous applications. An application's business logic or individual functions are modularized and presented as services for consumer/client applications. What's key to these services is their loosely coupled nature; i.e., the service interface is independent of the implementation.

    <end>

    http://www.codeproject.com/Articles/515253/Service-Oriented-Architecture-and-WCF

    Your MVC front-end application seems to be autenticating users. Maybe, you don't need to be using WCF at all for your solution. Maybe you would be better off just using MVC UI with a DAL Data Access Layer,  or using MVC and its ability to do CRUD operations with the ADO.NET Entiity Framework directly from the controller with the database.

    http://www.dofactory.com/framework/framework.aspx

    The VB version of the source code for the E-commence solutions are using the same back-end of BLL, DAL,  WCF,  EF or Linq-2-SQL. The DAL can be about any DB being used without the usage or an ORM. The UI's are interchangle with the backe-end. 

    The thing you need to understand is how to  architecture solutions and how are the objetcs the DTO(s) being passed between the fornt-end UI and the back-end using WCF with a BLL and DAL.

    Monday, June 23, 2014 2:07 PM
  • I hope you haven't given up on this yet.

    To use the attributes, place <DataContract()>_ above the Class declaration, and <DataMember()>_ above each Property. Try the call to the web service again. If it continues to fail, try capturing the entire error for us to review. The screen capture you provided is cutting off the important part.


    Deltazon

    Wednesday, June 25, 2014 12:56 PM
  • I added the attributes as you suggested.  Same failure.

    I am attaching the screenshots of the browser error in hope that it provides insight.

    Wednesday, June 25, 2014 10:05 PM
  • And here is the screenshot from the error as seen from VS2013 as well as a screenshot of the Class declaration after the DataContract and DataMember attributes were added.

    Wednesday, June 25, 2014 10:06 PM
  • http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx

    <copied>

    The default implementation of ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is null, its value is not included in the returned string

    <end>

    Wednesday, June 25, 2014 10:31 PM
  • darnold924 : I am not sure if you were suggesting that I add the Try/Catch code around the failing calls.  I did so just in case but no further insight into the failure or reason. 

    '
        ' GET: /Project/
        Function GetProjects() As List(Of Project) Implements IService.GetProjects
    
            Try
                Return db.Projects.ToList()
            Catch e As Exception
                Console.WriteLine("Error: {0}", e.ToString())
            End Try
        End Function

    and

    '
        ' GET: /Project/
        Function Index() As ActionResult
            Dim someobj As New ServiceReference1.ServiceClient()
    
            Try
                Return View(someobj.GetProjects())
            Catch e As Exception
                Console.WriteLine("Error: {0}", e.ToString())
            End Try
        End Function

    The exception that is returned does not look much different within VS (where the exception is caught) than what I see in the browser.

    Thursday, June 26, 2014 3:34 PM
  • http://msdn.microsoft.com/en-us/library/bb514135.aspx

    The true error message is being swallowed by WCF on the service side, and all you are getting is a generic error. Setting WCF service up for debugging to expose the error message in the config file setting can be done. But in general, you should read articles on how to throw error messages across WCF boundaries.

    Secondly, you are making calls to a typed-client, which should have a try/catch around the code similar to the try/catch you see in the link. Forget about what it is talking about with the 'Using" statement and look at the try/catches surrounding the code that makes the call. It's just FYI, but it should be done on all your code making the call to the service similar to the try/catch you see in the link. 

    http://msdn.microsoft.com/en-us/library/aa355056(v=vs.110).aspx

    And how does the client code even know what Project is if the object can't be seen by the client? The client has no reference to it? If that object is to be seen on both sides, then it needs to be in its own classlib project call it Entities and the client and service need to set reference to it so it can be seen by both sides. 

    You have gotten yourself into a mess, and if it was me and for right now, I would dump WCF out of the solution and just go to using the Entity Framework up at the MVC controller, if this solution is not using Access, because you have a lot to learn about using WCF. WCF is not the slam, bam thank you ma'am for you right now.

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

    There is probably some VB version of the above link somewhere. 

    Thursday, June 26, 2014 8:59 PM