Avatar of Kristen Jones
Kristen Jones
Flag for United States of America asked on

VB project referencing a C# DLL

First I have to tell you that I am VERY novice to ASP.NET programing.. I am taking a course and am having a problem.  I am just now learning about Classes..  so just keep that in mind with the replies.

My project is  a SMS application for cell phones. There was public code written in C# that I took and compiled using Visual Studio 2010. It built just fine no errors.  I then made a simple function in a new project (VB) calling the referenced DLL created before. I built with no errors, but when I run the code, I get the Error "Could not load type 'TwilioRestNameSpace.Account' from assembly 'TWILIO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

I have attached the un-compiled Csharp class and the VB function I am calling



"


CsharpClass.txt
VBFunction.txt
ASP.NETVisual Basic.NETVisual C++.NET

Avatar of undefined
Last Comment
Kristen Jones

8/22/2022 - Mon
rogerard

To place a dll into the GAC the following steps need to be done:

1. Generating a public key or a strong name
2.  Adding the public key to the AssemblyInfo.vb file of the DLL. This step  is called Signing the        Assembly with a public key.
3. Build the assembly
4. Placing the dll into GAC .

1. Generating the public key:
    go to visualStudio.Net command prompt and type the following command:

     sn -k keyfile1.snk
  Here keyfile1.snk is the keyfile we are generating.
 
    You can store this key file in the bin folder of the assembly. In  order to do this u have to go to the bin folder. For example:
   C:\foldername\assemblyname\bin sn-k keyfile1.snk

 By doing this the keyfile is stored in the bin folder.

2. Placing the keyfile into the Assembly.
   Go to AssemblyInfo.vb file:
   <Assembly: AssemblyKeyFile("C:\foldername\assemblyname\\keyfile1.snk")>
3. Build the Assembly.

4. Placing the assembly into GAC

      There are two way in which u can place the assembly into GAC.
    1. Using the utility gacutil -i
        Here you should give the complete path of the dll

   c:\>gactutil -i c:\foldername\assemblyname\bin\assemblyname.dll

    2. The second method is u can drag the .dll file from the bin folder  of the assembly and drop it into the folder C:\winnt\Assembly

    Doing this is same ie placing assembly into GAC.

Carl Tawn

@rogerard - Not quite sure how the instructions for installing something into the GAC comes into it.

What you have should work fine, I had to add a reference to the System.Web assembly, but other than that it was fine.

Have you added a reference to the C# DLL to your VB project?
Kristen Jones

ASKER
wow that is a lot of steps to use a DLL..  so I need to find out how to do your step #1 is this in the VB solution or before compiling the C# dll?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Carl Tawn

It's a lot of steps that are pretty much irrelevant in your scenario.
rogerard

A simpler possibility would be to include both projects into one solution and add a reference to the c# project in the vb project.  Then when you compile, vb knows where to find the correct .dll.  Remember that in order to debug the cs project, it will need to be compiled in debug mode.
Kristen Jones

ASKER
Yes I added the reference, and the code compiles fine but only give me the error when I try to call the function that is using the DLL
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
rogerard

You might want to check the reference to the cs dll and set the "copy local" flag to true
Kristen Jones

ASKER
rogerard: how do I use the C# Class using two different code bases (VB and C#)? I tried once before just crating a new class in the solution and I wan't able to build the DLL, which I why I created it in a new solution
rogerard

You create a solution then add each project to the solution.  That way you can have a vb project (for the mobile app) and a cs project (for the controls or other classes).  When you add the reference to the cs project, you select the tab for existing projects.  then on the reference tab of the vb project page, make sure the copy local flag is set to true. (this is necessary when the .dll has not been given a strong name and added to the gac.

Your help has saved me hundreds of hours of internet surfing.
fblack61
Carl Tawn

Basically what you need is a Class Library project, into which you put the class for your Twilio stuff. You then compile it.

The from your VB project you add a reference to the DLL and you should then be able to create a new object like:


Dim acc As TwilioRestNameSpace.Account = New TwilioRestNameSpace.Account("", "")

Open in new window

Kristen Jones

ASKER
carl_tawn:  that seems simple to at least try first. and PLEASE forgive my ignorance since VS is still very new to me. I can see my Solution in the "Solution Explorer"  It is called TWILIO and I have under this "My Project"  as well as References, Bin Folder, Obj folder, and my basic pages that call the functions.

I am not quite clear as to how to create a new Class project, as I "Thought" I was doing this by creating the

TWILIOCLASS.csproj and importing that CS code and compiling the DLL..  how can I get this project into the VB project?

rogerard:  Where do I set the "Copy Local Flag"?  I don't see this option under the properties of the DLL in Solution Explorer?
Carl Tawn

If you start from scratch and follow these instructions:
1) Open Visual Studio and select New Project > Visual C# > Class Library
2) Name the project TwilioTest
3) This will create a new class library project with a single class called Class1
4) Open Class1.cs and replace the contents with you C# code, and then compile it.

5) Right-click the solution and choose Add New Project. The select Visual Basic > Console Application
6) Right-click on the Console App and choose Add Reference. In the dialog that appears choose the Projects tab and select the class library project (it should be the only option)
7) In the Module1.vb file change the contents to the following:

    Module Module1
        Sub Main()

            Dim acc As TwilioRestNameSpace.Account = New TwilioRestNameSpace.Account("", "")

        End Sub
    End Module

8) Right-click the Console Application and choose "Set As StartUp Project".
9) Run the project.

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
rogerard

Ok..  To clarify.  You're going to have multiple projects in one solution.  The first is going to be a control library project for the cs code.  The other will be a device application for the vb code.  From a new project, you can go to the file menu and add a new or existing project (see example3.gif).  You should something like example2.gif after you've added two (or more) projects to the solution.  Once the projects have been added to the solution, go to the project properties window of the vb project and select the references tab.  Once you've added a reference to the control library project, make sure the copy local is set to true.  (see the example.gif).

example3.gif
example2.gif
example.gif
Kristen Jones

ASKER
Carl,

Ok here is my steps:

1) Open Visual Studio and select New Project > Visual C# > Class Library
DID THIS ALREADY

2) Name the project TwilioTest
DID THIS ALREADY TOO..  CALLED IT TWILIOCLASS

3) This will create a new class library project with a single class called Class1
DID THIS ALREADY TOO..  CALLED IT TWILIOREST.CS
4) Open Class1.cs and replace the contents with you C# code, and then compile it.
YEP DID THIS TOO...  CREATED THE TWILIO.DLL in the OBJ/DEBUG and the BIN/DEBUG folders

5) Right-click the solution and choose Add New Project. The select Visual Basic > Console Application
OK ...  HERE is where I have the problem..  RIght Click on the TWILLIOCLASS SOLUTION and I do not have the ADD NEW PROJECT option. I only get Add New its, Existing Item, new folder, windows form etc..

However I created a New VB project, Added the reference to the TWILIO.DLL in the BIN/DEBUG folder which is where I am at now...
rogerard

Look under the file menu, rather than right clicking on the solution name.

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Kristen Jones

ASKER
are  you both suggesting the same thing, one solution with two projects?

I was able to add this with the help of your screenshots...  ok I now have ONE soluton with Two Projects  (TWILIO and TWILIOCLASS)

I deleted the old reference in the TWILIO Project. I re-built the TWILIOCLASS and it created the TWIOLIO.DLL in the TWILIO/OBJ/DEBUG folder.  I re-Add the Reference pointing to this DLL and no errors.

I rebuild the TWILIO Project but I still get the same error..

Is this where I add Carls?:  Dim acc As TwilioRestNameSpace.Account = New TwilioRestNameSpace.Account("", "")





Carl Tawn

Yes. The part you need to pay attention to is the part in the C# class after the keyword "namespace". The name of the project and class files are not important. All that you are interested in in code is the namespace and the class name.
Kristen Jones

ASKER
I think I already have a variation of that line..  I have


        account = New TwilioRestNameSpace.Account(ACCOUNT_SID, ACCOUNT_TOKEN)

so could this still be the " "copy local" flag to true???  Thanks guy for your patience..  I'm trying very hard to learn..  

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kristen Jones

ASKER
If it makes any difference, I am not getting any build errors or Intelisence indicators, I can call my function with no errors...  it is only when I launch the ASPX page and try to activate the function.
Kristen Jones

ASKER
Here is my Function code..
Imports System
Imports System.Collections
Imports TwilioRestNameSpace




Public Class SendSMSCLASS
    Const API_VERSION As String = "2010-04-01"
    Const ACCOUNT_SID As String = "xxx8"
    Const ACCOUNT_TOKEN As String = "xxx"

    'OUtgoing Caler ID previously validated with Twilio
    Const CALLER_ID As String = "XXXXXXXXXXXXXXX"

    'Shared Sub SENDIT(ByVal args() As String)

    Public Function SENDIT()

        Dim account As TwilioRestNameSpace.Account
        Dim h As Hashtable
        ' Dim theresponse As String


        ' Create Twilio REST account object using Twilio account ID and token

        account = New TwilioRestNameSpace.Account(ACCOUNT_SID, ACCOUNT_TOKEN)

        h = New Hashtable()
        h.Add("From", "xxxx")
        h.Add("To", "xxxx")
        h.Add("Body", "xxxx Sending SMS")
        Try
            ' Console.WriteLine(account.request([String].Format("/{0}/Accounts/{1}/SMS/Messages", API_VERSION, ACCOUNT_SID), "POST", h))

            Return (account.request([String].Format("/{0}/Accounts/{1}/SMS/Messages", API_VERSION, ACCOUNT_SID), "POST", h))
        Catch e As TwilioRestException
            '  Console.WriteLine("An error occurred: {0}", e.Message)
            Return "An error occurred: {0}" & e.Message
        End Try

    End Function
End Class

Open in new window

Carl Tawn

Ok, then that suggests a problem with the config for the website. What type of project did you create WebSite or Web Application? How are you running your site, through Visual Studio, or are you publishing it and running it directly from a browser?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Kristen Jones

ASKER
Web Application and I am using the Debug, which I think uses the LOCAL host's IIS to run it.
ASKER CERTIFIED SOLUTION
Carl Tawn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kristen Jones

ASKER
I'm going to have to leave in about 15 min..  will you gentleman be around tomorrow to pick up, If I can't reply?  Thanks again for your Patience and help.
Carl Tawn

I will be at some point :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kristen Jones

ASKER
Thanks Guys
SOLUTION
kovilpattiBalu

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kristen Jones

ASKER
Morning Gentlemen,  I think I may have found my problem this morning My TwilioClass solution assembly name was TWILIO my VB solution was called TWILIO (also Assembly called TWILIO) I think that when it compiled, they would both create a TWILIO.DLL ..thus over writing one or the other.  This morning I renamed the Assembly on the TWILIOCLASS to TWILIOREST thus after building created TWO DLLs  TWILIO.DLL and TWILIOREST.DLL  

I ran the Debug locally however on my ASPX page that called my function I think I am getting another NEWBIE error. I am not sure if the original error will come back, but I need to get past this one to see...

Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.Label'

The line calling the function to update the Label1 on the page is failing..
Code below..  but I tried also:

Label1 = checkit.SENDIT.ToString() but I get the error:
Value of type 'String' cannot be converted to 'System.Web.UI.WebControls.Label'

is this an easy fix?





Public Class WebForm1
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click



        Dim checkit As New SendSMSCLASS
              
        Label1 = checkit.SENDIT

    End Sub
End Class

Open in new window

SOLUTION
rogerard

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kristen Jones

ASKER
that was it..  I am all working now..!!!!!
  Ok you all three have been very helpful, if it is all the same I hope you don't mind if I just equally distribute the points?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Carl Tawn

I'm not sure what kovilpattiBalu's comment brought to the table, but it's up to you :)
Kristen Jones

ASKER
Well I didn't equally distribute, but his "register the DLL" comment lead me to the discovery of the DLL's being the same