0

In simple form, I wrote a console application that (among many other things) makes two SOAP calls to different Web Services. It was a stand-alone solution, so I just put the two WSDL files in as Service References and created wrapper classes to invoke them and return data. I created two Service References, GetSession, and SubscriptionNotifications in the project and wrote two wrapper classes to instantiate the soap clients, initialize them, call them, and return data. Code is proprietary, so I can only show snippets.

Solution Explorer shows basically:

Solution 'MySolution'
  MySolution
    Properties
    References
    ServiceReferences (folder)
      GetSession
      SubscriptionNotifications
    Code (folder for .cs files)
      Program.cs
      EmailUtil.cs
      DataRepository.cs
      SOAP_GetSession.cs
      SOAP_SendSubscriptionNotifications.cs
    App.config
    packages.config

In SOAP_GetSession.cs (partial code) I wrote

using MyNamespace.GetSession;  // the autogenerated SOAP client

namespace MyNamespace.MySolution.Code
{
  class SOAP_GetSession {

    public static string GetAppToken() {

      string env = configFile.GetConfigValue("Environment");
      string userName = configFile.GetConfigValue("UserName");
      string password = configFile.GetConfigValue("Password");
      string appId = configFile.GetConfigValue("ApplicationId");

      string appToken = "";

      try {
        ServiceClient sc = new ServiceClient();
        SessionResponse sr = sc.GetSession(env, userName, password, appId);
        appToken = sr.ApplicationSession;
      }
      catch (...) {
       // do exception handling
      }
      return appToken;
    }
  }
}

I skipped most of the inessential code in the class, but this is the essence and it works fine. Now, I want to take the two SOAP calls and create separate projects as Class Libraries within the solution and move the SOAP functionality to these projects so the projects can be used intact in other solutions. I have been having endless referential and namespace problems all day. Even though the projects are meant to be stand-alone with no dependencies and each has its own ServiceReference WSDL client, I can't get the right combination of "using"s, references, namespaces, and dependencies to get either program to recognize its SOAP client's objects and methods in my own code. Everything I try brings up errors that the ServiceClient or the GetSession() or ServiceResponse reference can not be found in the namespace. I've qualified them with namespaces, inserted using statements, and everything else I can think of to no avail.

Can anybody sketch out a simple class and method like the one I showed above that will work in its own project and can be referenced by the main startup project?

inan
  • 178
  • 11
  • You can reference a console application in another project as you would any class library dll. You don't need to isolate your service references in their own projects. – Guillaume CR Oct 19 '16 at 20:20
  • I have been asked by my manager to isolate each service reference in its own project and create a wrapper class that executes it and returns the result. That way, the projects can be mixed and matched in multiple solutions with different console applications. So, in my sample above, the SOAP_GetSession class with its GetAppToken() method can be added to multiple solutions and used by multiple solutions. The method that works when included in a one-project solution doesn't build in a 2-project solution with the class in a class library dll. – MiddleAgedMutantNinjaProgrammer Oct 20 '16 at 14:57
  • I understand your need. I'm saying the executable that is generated from your project can be used as the class library for your other project. If your need really is reusability, then isolation is not a prerequisite. I have learned the hard way that the decision to move code to a separate project is not a cost you take lightly. Only create projects when you need physical code separation. Hint: You rarely need physical code separation. Logical code separation is what you need, and that is achieved through namespaces. – Guillaume CR Oct 20 '16 at 17:08

1 Answers1

0
  1. Create a new class library project.
  2. Add a service reference for your web services to it
  3. Move your SOAP_GetSession.cs and SOAP_SendSubscriptionNotifications.cs to that project
  4. Delete service reference from your console app project
  5. Add a reference to your new project to your console app
  6. In a file where you need to use your SOAP_GetSession in the console app make sure you add using MyNamespace.MySolution.Code

This is what it will look like:

Solution 'MySolution'
  MySolution
    Properties
    References
      MyClassLibrary
      All other references
    Code (folder for .cs files)
      Program.cs
      EmailUtil.cs
      DataRepository.cs
    App.config
    packages.config
  MyClassLibrary     
    Properties
    References
    ServiceReferences (folder)
      GetSession
      SubscriptionNotifications
    Code (folder for .cs files)
      SOAP_GetSession.cs
      SOAP_SendSubscriptionNotifications.cs
    App.config
    packages.config

You will then be able to reference MyClassLibrary in other projects that you will create. You will not need to add service references to every project. Remember that you may have to copy endpoint and other service configuration settings from your library app.config to the main app.config of every top level project that you create.

bib1257
  • 325
  • 2
  • 7