none
WCF Step by Step RRS feed

  • Question

  • Help!!  I have the book, WCF Step by Step, by John Sharp, and I am still learning WCF.  I really need some assistance with one particular implementation I am trying to do with a Postgresql database.  Can someone Please direct me to the chapters / topics in the book discussing the methodologies by which my Client application, developed in Visual Studio 2012 and WPF, C#, can use WCF to obtain / update data from a postgresql database Server on my desktop machine, connected through localhost, 127.0.0.1?  I am planning on also using Npgsql for data operations.    Postgresql typically uses port 5432.  Do I need to change that Postgres configuration to another port to allow message sharing or forwarding from, WCF?  Thank you.
    Monday, July 27, 2015 8:33 PM

All replies

  • WCF is about SOA.

    https://en.wikipedia.org/wiki/Service-oriented_architecture

    WCF has nothing to do what any database.

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

    However you understand SoC, then you will resort to having a classlib project called a DAL,  where WCF doesn't care what the database is about, which is the concern of the DAL that the WCF service has reference to the DAL.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    <copied>

    Layered designs in information systems are another embodiment of separation of concerns (e.g., presentation layer, business logic layer, data access layer, persistence layer.

    <end>

    You can forget about an ASP.NET WEB UI, because the UI can be about any UI --  desktop, ASP.NET or otherwise.

    http://dotnetdaily.net/tutorials/n-tier-architecture-asp-net/

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

    http://www.mindscapehq.com/documentation/lightspeed/Building-Distributed-Applications-/Building-WCF-Services-using-Data-Transfer-Objects

    Presentation tier, BLL, WCF and DAL projects have reference to a project called Entities that contain the DTO(s) and you send the DTO through the tiers.

    Forget about the datasets and datatables in the link below and learn how to use the DTO(s) objects and List<T>.

    http://www.codeproject.com/Articles/36847/Three-Layer-Architecture-in-C-NET

    http://www.codeproject.com/Articles/18850/Web-Service-via-Data-Access-Object-Pattern

    http://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

    You can use MVP with Windows forms UI in n-tier.

    Watch the show and learn how to use MVP.

    http://polymorphicpodcast.com/shows/mv-patterns/

    Monday, July 27, 2015 11:16 PM
  • Yes I have a background in MVVM and dependency injection. 
    Tuesday, July 28, 2015 2:39 AM
  • And I understand about Entities and DAL, repository pattern, etc.
    Tuesday, July 28, 2015 2:45 AM
  • I think my question is more about the particular WCF configuration as there are many ideas out there on how to configure endpoints, proxies, behaviors, etc. 

    For starters, is this app.config shown immediately below anything like what it should look like?  This is from the WCF host module in my Prism Unity application.  The WCF library is in another separate module/ assembly. Below the app.config is the WCFHostModule.cs, which is instantiated when the application starts.  As shown, the OnStart method of the WCFHostModule instantiates MyDataService.  Right now, that class just has a method that connects to the database.  Later on I will add DAL, etc.

    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
    
       <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="WcfLibraryModuleBehavior">
              <serviceDebug includeExceptionDetailInFaults="true" />
              <serviceMetadata />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="WcfLibraryModuleBehavior" name="WcfLibraryModule.MyDataService">
    
            <endpoint binding="netTcpBinding" bindingConfiguration="" name="WcfLibraryModule.Endpoint" contract="WcfLibraryModule.IMyDataService" />
            <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" name="WcfLibraryModuleEndpoint2" contract="IMetadataExchange" />
            </service>
        </services>
      </system.serviceModel>
      
    </configuration>
    using System.ServiceProcess;
    using System.ServiceModel;
    using WcfLibraryModule;
    
    namespace WcfSvcHostModule
    {
        public partial class WcfHostService : ServiceBase
        {
            internal static ServiceHost myServiceHost = null;
    
            public WcfHostService()
            {
                InitializeComponent();
    
            }
    
            protected override void OnStart(string[] args)
            {
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                }
                myServiceHost = new ServiceHost(typeof(MyDataService));
                myServiceHost.Open();
            }
    
            protected override void OnStop()
            {
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                    myServiceHost = null;
                }
            }
        }
    }




    • Edited by Harpagornis Tuesday, July 28, 2015 4:12 AM code provided
    Tuesday, July 28, 2015 2:49 AM
  •  If you know all of this, then why are you here asking questions if you are this sharp? You should be able just to open up a WCF book and just put it altogether without being here asking questions.

    I know that's what I did back in year 2008 when I first used WCF for DoD and hooked it up together  with using Windows Work Flow and WCF together  all for the first time, just opened up the WCF and WF books and used MS SQL Server too all sitting behind the WCF services. There is no mystery about it.

    Any coding monkey can open up a book and write some code. But can you architecture a solution from the front-end to the back-end,  and that may be what you are missing, because you should be able to do this with any DB sitting behind WCF.

    There is no secret sauce on WCF. It is just a SOA solution that allows communications between the WCF client and WCF Service client/server and that is it.

    Come on man......

    Tuesday, July 28, 2015 3:30 AM
  • Not helpful (or nice.)
    Tuesday, July 28, 2015 4:47 AM
  • http://www.codeproject.com/Articles/653493/WCF-Hosting-with-Windows-Service

    Have you even tried to look at examples?  There are plenty of them. And do you know about the programname.exe.config, right? .NET is only using an app.config in debug mode. At runtime, .NET is looking at programname.exe.config.

    You are talking about you can't get things to work, and I had to assume that you don't know what you are doing at all. 

    Then what is the problem?  You get any exceptions or what? You want help? You won't explain what the problem is about other than you can't get a database to work with WCF. That's not the purpose of WCF.

    Tuesday, July 28, 2015 5:02 AM
  • And I'll make another suggestion to you. Get the database stuff into a DAL, use MS Unit Test harness with Nunit,  a classlib  project,  a functional test project,  against the DAL,  and get all aspects of the DAL to work.

    Then you should set reference to the DAL from WCF project and call methods on the DAL with no problems. WCF should only be looked at in the aspects of communications only.

    Tuesday, July 28, 2015 5:19 AM
  • One error message I got was that shown below.  That occurs when I right click the WCFLibraryModule project and select Debug / Start New Instance.

    System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:5432. This could happen if there is another application already listening on this endpoint or if you have multiple service endpoints in your service host with the same IP endpoint but with incompatible binding configurations. ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted

    My OS is Windows 7 and Postgresql is a Network service already "listening" on port 5432.  When I got that error, this was the App.config for the WCFLibraryModule.

    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
    
      <entityFramework>
        <providers>
          <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
        </providers>
      </entityFramework>
    
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="WcfLibraryModuleBehavior">
              <serviceDebug includeExceptionDetailInFaults="true" />
              <serviceMetadata />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service name="WcfLibraryModule.MyDataService">
            <endpoint address="net.tcp://localhost:5432/MyDataSvc" binding="netTcpBinding"
              bindingConfiguration="" contract="WcfLibraryModule.IMyDataService" />
            </service>
        </services>
      </system.serviceModel>
    </configuration>

    Tuesday, July 28, 2015 5:42 AM
  • Well, you have to change the port number that the service is listening on, which is in conflict with another program that is already listening on the port that is running on the machine.

    You have to use a port that is not a well known port. You shouldn't use any low ports which are 0-1023, and you can't use any ports above 1023 that are high ports that are well known high ports. You have 66,635 ports to choose from pick one that is not a well known port that the service can listen on, or pick a port that doesn't have the potential or you know in fact that no program running on the machine is going to use the port.

    https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

    http://helpdeskgeek.com/windows-7/use-netstat-to-see-listening-ports-and-pid-in-windows/

    http://download.cnet.com/CurrPorts/3000-2085_4-10448760.html

    Tuesday, July 28, 2015 11:37 AM
  • So, what you are saying is that I need to establish the connection to my Postgresql database as usual on port 5432, using Npgsql, and create the DAL and only THEN I can use WCF?
    Tuesday, July 28, 2015 3:23 PM
  • So, what you are saying is that I need to establish the connection to my Postgresql database as usual on port 5432, using Npgsql, and create the DAL and only THEN I can use WCF?

    No, what I am saying is Postgressql DB and your WCF service cannot be listening on the same inbound port.

    http://www.adminsub.net/tcp-udp-port-finder/5432

    If the Postgressql DB is running on the computer using TCP 5432 a well-known TCP port number, then you can't have the WCF service running and listening on the same TCP port on the machine.  It is a conflict the port is already in use by another program running on the machine that is/will cause your WCF service to be terminated due to it not being able to get exclusive control of the port.

    If MS SQL Server is running on the computer, MS SQL Server listens on TCP ports 1432 and 1433 well-known ports, then no other program running on the computer such as a WCF service can be listening on the port too. You have to pick another port for your WCF service  to listen on for inbound traffic from the WCF client. 

    You must understand that in client/service applications in communications with each other, it is the WCF client that must make the initial contact with the WCF service on the inbound port that the WCF service is listening on before communications will take place between the two.

    I gave you the link of the TCP and UDP ports that are well known throughout the IT industry, and you have to adhere to that list. This endpoint has to be changed to some other port that the inbound port the service is listening on, if nothing other program is running on the machine that is not already listening on the inbound port.

    Endpoint address="net.tcp://localhost:5432/MyDataSvc" binding="netTcpBinding"    it has to be changed to some other port and you have to figure out how to tell the WCF service what port it is to listen on, and you tell the client through the endpoint were the WCF service is located and what port the WCF service is listening on.  Capiche

    You should still use a DAL, and I don't care how you do it use a console application to test the DAL or something and then set reference to the DAL in the WCF service. All issues with the DAL should be out of the way before you set reference to the DAL in the WCF service project to eliminate any issues with the DAL prior to using it with the WCF service.

    You should do some kind of Hello World thing with your WCF client and service to make sure you understand how client/server applications work before you proceed to do something with a DB in the solution. Small dives before big dives and stop going SPLAT into the pool's cement. :) 

    Tuesday, July 28, 2015 4:27 PM
  • I already did a simple Client app that connects to the Postgresql Server.  From what you are telling me, WCF has no relevance in that Client / Server connection / messaging process.
    Tuesday, July 28, 2015 4:39 PM
  • I already did a simple Client app that connects to the Postgresql Server.  From what you are telling me, WCF has no relevance in that Client / Server connection / messaging process.

    Yeah it WCF does it absolutely does between the WCF client and  service that messages are sent between the two.

    https://msdn.microsoft.com/en-us/library/ms731082%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    <copy>

    Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application. An endpoint can be a client of a service that requests data from a service endpoint. The messages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data. A few sample scenarios include:

    <end>

    It seems to me for whatever reasons, you are having a hard time about how communcations work on a network Intranet or Internet and with computers in general.

    http://www.bleepingcomputer.com/tutorials/tcp-and-udp-ports-explained/

    net.tcp://localhost do you know what localhost means? How it works? 

    https://en.wikipedia.org/wiki/Localhost

    http://www.techopedia.com/definition/2440/loopback-address

    How can you have two different programs the DB and the WCF client and service programs using the same TCP port number? You can is some situations but this is not one of those situations, which is a more advanced WCF topic.

    I suggest that you stop, use MS SQL Server Express and find out what you are suppose to be doing here.

    http://www.codeproject.com/Articles/127395/Implementing-a-WCF-Service-with-Entity-Framework

    http://blogs.microsoft.co.il/bursteg/2008/02/10/how-to-build-an-n-tier-application-with-wcf-and-datasets-in-visual-studio-2008/

    If you don't understand how to use WCF, then how can use it effectively? How can use WCF in any programming scenario if you don't really understand how to use it? But I wouldn't get into using WCF with datasets and datatables too much.  

    Tuesday, July 28, 2015 6:02 PM
  • So WCF cannot be used to "wrap" or handle any of the data WHILE it is being transported from the Postgres Server to the Postgres Client?  WCF just comes into play after the data is sent by the Postgres Server?

    Tuesday, July 28, 2015 6:26 PM
  • WCF just comes into play after the data is sent by the Postgres Server?

    You know what a DAL is about. The DAL is the client of the database server any DB server it doesn't matter if the DB server is MS SQL Server, Oracle, Postgres, Access or whatever. It is the DAL's responsibility to work with the database for CRUD operations (Create, Read, Update and Delete) operations with the database.  

    And I understand about Entities and DAL, repository pattern, etc.

    The above statement is yours. A DAL is a design pattern, repository pattern is a design pattern. A DAL can have a repository being used within it,  it could be the Data Access Object pattern, or it could be the Unit Of Work pattern. DTO(s) Data Transfer Objects are created by the DAL and populated by DAL objects used to populate DTO(s)  that are marked as DataContracts. The DTO(s)  are sent from the DAL so that the WCF service can serialize them and send then to the WCF client, the messages in the form of XML serialize objects. The WCF client can make new DTO(s) or use the ones  sent to the client by the service  and manipulated them.

    The client then sends the DTO(s) back to the WCF services that in turn sends DTO(s) to the DAL so that objects in the DAL like repository, DAO or UoW objects,  work with the data in the DTO(s) and the DAL objects persist the data to the database. The WCF service hosts the classlib project called the DAL.

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

    <copied>

    Data transfer object (DTO) is an object that carries data between processes. The motivation for its use has to do with the fact that communication between processes is usually done resorting to remote interfaces (e.g. web services), where each call is an expensive operation.<sup> </sup>Because the majority of the cost of each call is related to the round-trip time between the client and the server, one way of reducing the number of calls is to use an object (the DTO) that aggregates the data that would have been transferred by the several calls, but that is served by one call only.

    <end>

    The two processes that are in play here are the WCF service and the WCF client.

    I have given you examples on how to do it, and it is up to you to figure it all out.

    http://www.dofactory.com/products/net-design-pattern-framework

     3-Tier / N-Tier Pattern and Practices based Architectures

    • Reference Application:  ASP.NET MVC Patterns
    • Reference Application:  ASP.NET Web Forms Patterns
    • Reference Application:  Windows Forms Patterns
    • Reference Application:  WPF Patterns

    Each one of the application front-ends with the source code are using WCF and a database on the backend you get the source code on all of it. They are using the same back-end with each front-end being interchangeable with the back end.  And they are using everything that I have been talking about. You see it all in action when you run the solutions in VS Debug mode.

    You have to see someone else doing it and see the picture before you can take off and do your own thing. It's plain and simple. If you don't know how to  architect then you are lost on all aspects of developing .NET solutions effectively.

    Good luck to you man as I am out of here on this. :)

    Tuesday, July 28, 2015 8:19 PM
  • I will continue looking for an answer to my question about whether or not the WCF service can monitor the data transport to and from the Database Server, separate and apart from the DAL methods.  Thank you for all your help.  I assure you that I have done a lot of research on most if not all of the topics you mentioned.  As you know, no matter how much reading and studying one does, there is no substitute for actually doing and trying.  I also downloaded many related demo apps, but there are very few Postgres (or Access or MySql or whatever) demo apps out there, especially using all the architectural design methods discussed. 


    Tuesday, July 28, 2015 8:37 PM
  • I will continue looking for an answer to my question about whether or not the WCF service can monitor the data transport to and from the Database Server

    The answer is no. It is not the job of a WCF solution.

    https://en.wikipedia.org/wiki/Service-oriented_architecture

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

    What is SOA?

      SOA or Service oriented architecture is an architecture style for building business applications by means of services. Application comprises collection of services which communicate through messages.

    Tuesday, July 28, 2015 9:03 PM