Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB project referencing a C# DLL

Posted on 2010-11-08
32
Medium Priority
?
846 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:Jason Jones
  • 15
  • 9
  • 7
  • +1
32 Comments
 
LVL 7

Expert Comment

by:rogerard
ID: 34085925
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
ID: 34086474
@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:Jason Jones
ID: 34086479
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 52

Expert Comment

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

Expert Comment

by:rogerard
ID: 34086549
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:Jason Jones
ID: 34086550
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
ID: 34086565
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:Jason Jones
ID: 34086569
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
ID: 34086583
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
ID: 34086588
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:Jason Jones
ID: 34086771
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
ID: 34086870
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
ID: 34086883
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:Jason Jones
ID: 34087332
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
ID: 34087375
Look under the file menu, rather than right clicking on the solution name.

0
 
LVL 1

Author Comment

by:Jason Jones
ID: 34087535
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34087554
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:Jason Jones
ID: 34087567
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:Jason Jones
ID: 34087589
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:Jason Jones
ID: 34087594
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
ID: 34087616
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:Jason Jones
ID: 34087630
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 900 total points
ID: 34087650
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:Jason Jones
ID: 34087651
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
ID: 34087675
I will be at some point :)
0
 
LVL 1

Author Comment

by:Jason Jones
ID: 34087689
Thanks Guys
0
 
LVL 7

Assisted Solution

by:kovilpattiBalu
kovilpattiBalu earned 200 total points
ID: 34092565
hi,

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

Author Comment

by:Jason Jones
ID: 34093476
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 900 total points
ID: 34093532
try
Label1.text  = checkit.SENDIT.tostring
0
 
LVL 1

Author Comment

by:Jason Jones
ID: 34093670
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
ID: 34093691
I'm not sure what kovilpattiBalu's comment brought to the table, but it's up to you :)
0
 
LVL 1

Author Comment

by:Jason Jones
ID: 34093700
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

810 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