Solved

VB project referencing a C# DLL

Posted on 2010-11-08
32
819 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:upgraders
  • 15
  • 9
  • 7
  • +1
32 Comments
 
LVL 7

Expert Comment

by:rogerard
Comment Utility
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.

0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
@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?
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
It's a lot of steps that are pretty much irrelevant in your scenario.
0
 
LVL 7

Expert Comment

by:rogerard
Comment Utility
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.
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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
0
 
LVL 7

Expert Comment

by:rogerard
Comment Utility
You might want to check the reference to the cs dll and set the "copy local" flag to true
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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
0
 
LVL 7

Expert Comment

by:rogerard
Comment Utility
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.

0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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

0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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

0
 
LVL 7

Expert Comment

by:rogerard
Comment Utility
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
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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...
0
 
LVL 7

Expert Comment

by:rogerard
Comment Utility
Look under the file menu, rather than right clicking on the solution name.

0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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("", "")





0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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.
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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..  

0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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.
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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

0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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?
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
Web Application and I am using the Debug, which I think uses the LOCAL host's IIS to run it.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 225 total points
Comment Utility
Ok, as a test publish your site to a seperate folder. Then check its bin directory and see if your Twilio DLL is in there.
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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.
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
I will be at some point :)
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
Thanks Guys
0
 
LVL 7

Assisted Solution

by:kovilpattiBalu
kovilpattiBalu earned 50 total points
Comment Utility
hi,

register the .net dll in assembly, using RegASM.exe
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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

0
 
LVL 7

Assisted Solution

by:rogerard
rogerard earned 225 total points
Comment Utility
try
Label1.text  = checkit.SENDIT.tostring
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
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?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
I'm not sure what kovilpattiBalu's comment brought to the table, but it's up to you :)
0
 
LVL 1

Author Comment

by:upgraders
Comment Utility
Well I didn't equally distribute, but his "register the DLL" comment lead me to the discovery of the DLL's being the same
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Set form below another form 3 25
Round up to 100% in .NET 10 41
Showdialog 8 20
Closing all open child forms. 4 22
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now