2

I have a DLL in .NET which uses a WebService (to send emails).

When I use the DLL in a project, it gives me an error unless I reference the webservice again.

I would like to not be forced to reference this in parent projects which use the DLL

To call the DLL, I simply do something like this:

Try
    Dim x As New clsMail
    Dim y As New clsMail.objMail
    x.SendEmail(y)
Catch ex As Exception
    e = ex.ToString
End Try

Is this possible since it is already referenced in the DLL?

Files

If I remove the reference in the main project, I get an error from the Common DLL. If I add the Service reference to main project, then the error goes away.

Error

I would like to be able to use this DLL and service by Only Importing the DLL without having to do any extra work. The point is to be able to import this DLL into many projects, without any extra steps in those projects.

Thanks in advance

Final Update:

Adding MailServer = New Mailserver.Service1SoapClient(New BasicHttpBinding(), New EndpointAddress("http://webmail/SendEmail.asmx") in the DLL itself Fixes this issue. I can now use in any project without having to reference the Web Service outside of the DLL

C# version:

 MailServer.Service1SoapClient MailServer = new MailServer.Service1SoapClient(new BasicHttpBinding(), new EndpointAddress("http://webmail/SendEmail.asmx"));

Thank you

Ess Kay
  • 598
  • 4
  • 20
  • You should post the exact error. Are you exposing types defined by the service through your 'service access' assembly? – user6144226 Sep 01 '17 at 13:24
  • Sure: If i remove service from snapshot i get *Could not find default endpoint element that references contract 'MailServer.Service1Soap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.* – Ess Kay Sep 01 '17 at 13:32
  • https://stackoverflow.com/questions/352654/could-not-find-default-endpoint-element/2737593#2737593 – user6144226 Sep 01 '17 at 13:40
  • Just a FYI, you are not using a WebService, you are using a WCF service. WebService is something else that has a similar but diffrent Refrence.vb file. – Scott Chamberlain Sep 01 '17 at 16:17
  • Perhaps, but the endpoint ends in ASMX. I think its not wcf – Ess Kay Sep 01 '17 at 17:23

2 Answers2

4

You likely don't need the entire service refrence added. The only thing beneficial adding the reference did was add settings in your App.config for the program for you, you did not need any of the files or classes it generated. All you need to do is copy the configuration settings in the app.config that where auto-generated in WizardCommunications and copy them in to the App.config in screenshot. It will then read the settings from the exe's config file and should work.

You also could manually hard code the settings (like the URL it connects to) from the app.config in to your library (by using constructors that let you pass in a URL) so it does not rely on the configuration for its settings.

UPDATE:

To hardcode the settings instead of using Dim MailServer as New MailServerClient and using the constructor Public Sub New() in Partial Public Class Service1SoapClient you need to use the Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress) constructor instead.

To do that you need to re-create the settings defined in your app.config file in the dll and pass that in as the two parameters. Here is a basic example of what one would look like, you will need to adjust it to fit your needs

Dim MailServer As New MailServer.Service1Client(New BasicHttpBinding(), New EndpointAddress("http://webmail/SendEmail.asmx"))

Once you do that you can just reference the dll of your project and not need anything in the app.config of the running exe.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • not sure how that will fix the issue of using this dll in multiple solutions without adding the service in each one – Ess Kay Sep 01 '17 at 13:15
  • It fixes the issue because the error *"Could not find default endpoint element that references contract 'MailServer.Service1Soap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element"* means it is missing it's settings in the App.config of the exe that is running. If you copied the configuration from the autogenerated one in the dll's app.config to the exe's app.config your program will work. – Scott Chamberlain Sep 01 '17 at 13:38
  • But i will need to perform this in every solution that uses the Communication dll. I would like to bypass all that and just need to add reference to DLL and be done with it. Its not a matter of this dll working, i have that done. i would like to just remove the extra steps. Mostly because We will be recycling this DLL in other projects, so adding these hidden references will be a nuisance – Ess Kay Sep 01 '17 at 13:40
  • Ok, in that case use the constructor that lets you pass in a URL instead of the default constructor when you create your service. – Scott Chamberlain Sep 01 '17 at 13:41
  • can you give an example – Ess Kay Sep 01 '17 at 13:42
  • Please update your question to include the generated code for the service and the code that creates the service so I can use the correct names. – Scott Chamberlain Sep 01 '17 at 13:44
  • @EssKay if you go in to the actual folder for your project, in the `MailServer` folder there should be a `Reference.vb` file in there that you can't normally see from inside Visual Studio. please update your question and put the code for that file in your question. What you want is easily doable, but I need to see the available constuctors for `Service1SoapClient` to give you the correct code to write. – Scott Chamberlain Sep 01 '17 at 14:57
  • Sure. Updated question – Ess Kay Sep 01 '17 at 15:56
  • Updated with a basic example. would need to see your app.conifg file to be able to make a more specific example. – Scott Chamberlain Sep 01 '17 at 16:27
  • Added the config file. from what i tried, it doesnt seem to work – Ess Kay Sep 01 '17 at 17:18
  • Can you update your question showing what you tried and what errors you got when it did not work? I also updated my answer to use the values from your config file. – Scott Chamberlain Sep 01 '17 at 18:04
  • Changed **Dim MailServer As New MailServer.Service1SoapClient** to have **(New BasicHttpBinding(), New EndpointAddress("http://webmail/SendEmail.asmx"))** --Error _Could not find default endpoint element that references contract 'MailServer.Service1Soap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element._ – Ess Kay Sep 01 '17 at 18:52
  • Looking at the image of your update you are creating the class twice, once in the declaration once in the `=`. Either get rid of the line `MailServer = New Mailserver.Service1SoapClient` or change it to `MailServer = New Mailserver.Service1SoapClient(New BasicHttpBinding(), New EndpointAddress("http://webmail/SendEmail.asmx"))` If you want no configuration you need to always call the overload that uses the two parameters. You need to find every `New Mailserver.Service1SoapClient` in your code and update it to the new version. – Scott Chamberlain Sep 01 '17 at 19:11
  • Thank you, that seemed to do it! – Ess Kay Sep 01 '17 at 19:37
0

I would suggest creating a layer with common code which is going to be shared through all the solution.

So for example, screenshot.common, then reference the service in common, so you only need to reference the service in a single place. Then wherever you need the common code just reference screenshot.common.

I hope it helps!

  • not sure how that will fix the issue of using this dll in multiple solutions without adding the service in each one – Ess Kay Sep 01 '17 at 13:14
  • Well, basically the idea is to have the service reference in a single place, then wherever you need it just reference the Dll where the service is referenced. From my point of view that will let you create a good decoupling because if it is a WCF for example, all classes will be in a single place, and you will only need to reference the DLL and you will have access to those classes and will only need add the required configurations in the project where you are going to use those classes and the service. – Oscar Eduardo Jimenez Carmona Sep 01 '17 at 13:45
  • if i start a new project say Test1. i will need to do that all over again? – Ess Kay Sep 01 '17 at 13:47
  • No, because if you start a new solution, you can add existing projects, so you will have say a common dll which you can reuse in every project you need. – Oscar Eduardo Jimenez Carmona Sep 01 '17 at 13:49
  • The common DLL is actually the communications DLL in the picture – Ess Kay Sep 01 '17 at 13:53
  • Did you copied the configuration generated in app.config in communications DLL to app.config in the screenshot project? – Oscar Eduardo Jimenez Carmona Sep 01 '17 at 13:56
  • No i am trying to avoid copying anything. The goal is to ONLY add reference to DLL and to have no further steps – Ess Kay Sep 01 '17 at 14:00
  • I see, well you can try coping config files as link, do whatever change you do in communications will be reflected in screenshot as well. – Oscar Eduardo Jimenez Carmona Sep 01 '17 at 14:02
  • Take a look at this post https://stackoverflow.com/q/426245/3975898 about sharing config files, may be that helps. – Oscar Eduardo Jimenez Carmona Sep 01 '17 at 14:04
  • im not interested in sharing files. I want the DLL to handle everything – Ess Kay Sep 01 '17 at 15:57