I have successfully added a service reference to my Visual Studio 2013 project.
I cannot however reference it in code.
For example, given the project name "myproject", and the service reference name "thirdpartyasmx", I cannot use this using directive...
using myproject.thirdpartyasmx
I cannot instantiate the class directly either...
thirdpartyasmx test = new thirdpartyasmx();
I have tried building the solution before trying to reference the service reference.
This is not something I do regularly, so I'm afraid I'm missing a step.
UPDATED based on OP feedback
When you click on Add Service Reference, click on the Advanced button in the bottom left of the dialog box:
Clear the Reuse types in referenced assemblies checkbox (by default it's checked).
You don't need to use the using directive at the top of the class, you can just instantiate the service reference inside the class using the name you gave to the service when referencing it:
public class MyClassName
{
protected void SomeMethod()
{
thirdpartyasmx.ThirdpartyasmxServiceClient service = new thirdpartyasmx.ThirdpartyasmxServiceClient();
}
}
Related
I'm developing a Visual Studio Extension to replace text in the current active .cs file using a custom command that is invoked from the right click context menu in the Code Window.
Accessing the document works so far, but if I start more than one instance of VS2017, then changes which I expect to be done in the new instance are made in the first opened instance.
Is there a possibility to get the right instance to access only the current active Document no matter how many instances are open?
At the moment I get the instance with following code:
dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal
.GetActiveObject("VisualStudio.DTE.15.0");
Does anybody have an idea how to solve this?
You need to use in the class of your package (that inherits from the AsyncPackage base class):
EnvDTE.DTE dte = (EnvDTE.DTE) base.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));
The code that you were using returns some DTE instance running on your system, not necessarily the one where your extension is hosted.
As Carlos Quintero already said, you should get the DTE Object by using his example.
Lets say your extension name is YourExtension:
In my case, I added a Property in my YourExtension.cs
public EnvDTE.DTE DTEObject { get; set; }
Then in YourExtensionPackage.cs you can get the desired DTEObject right after your package got initialized:
protected override void Initialize ()
{
YourExtension.Initialize (this);
base.Initialize ();
YourExtension.Instance.DTEObject = (EnvDTE.DTE)base.GetService (typeof (Microsoft.VisualStudio.Shell.Interop.SDTE));
}
Now you can work with the DTEObject within your extension and get any Object via GetObject. In My case for example I'm getting the current instance of the VersionControlEx.
I am a newbie of C# and MS visual studio, and I want to use the C# class which defined in another file, but can't get it work.
Here is the program.cs(and why can't I rename that file ?)
using System;
namespace TestCSharp2
{
class Program
{
static void Main(string[] args)
{
Class2 class2 = new Class2();
// here the IDE will complain that cant find namespace or balabala..
class2.setValue(10);
Console.WriteLine(class2.getValue().ToString());
Console.ReadKey();
}
}
}
And here is the Class2 that I want to use in file Class2.cs:
namespace TestCSharp2
{
class Class2
{
int i;
public void setValue(int i)
{
this.i = i;
}
public int getValue()
{
return this.i;
}
}
}
Should I #include or something? isn't use namespace enough?
As some guys asked if they are in the same assembly/same project, I presume they were, because here is the procedure how they are created:
A new project using the template of Console C# Project, then the program.cs was created by default.
The Class2.cs was created with [File] -> [New] -> [File] -> [C# class] and saved in the same folder where program.cs lives.
To be honest, I don't know if they are in same assembly / same project, but I guess they were.
According to your explanation you haven't included your Class2.cs in your project. You have just created the required Class file but haven't included that in the project.
The Class2.cs was created with [File] -> [New] -> [File] -> [C# class] and saved in the same folder where program.cs lives.
Do the following to overcome this,
Simply Right click on your project then -> [Add] - > [Existing Item...] : Select Class2.cs and press OK
Problem should be solved now.
Furthermore, when adding new classes use this procedure,
Right click on project -> [Add] -> Select Required Item (ex - A class, Form etc.)
Yeah, I just made the same 'noob' error and found this thread.
I had in fact added the class to the solution and not to the project.
So it looked like this:
Just adding this in the hope to be of help to someone.
It would be more beneficial for us if we could see the actual project structure, as the classes alone do not say that much.
Assuming that both .cs files are in the same project (if they are in different projects inside the same solution, you'd have to add a reference to the project containing Class2.cs), you can click on the Class2 occurrence in your code that is underlined in red and press CTRL + . (period) or click on the blue bar that should be there. The first option appearing will then add the appropriate using statement automatically. If there is no such menu, it may indicate that there is something wrong with the project structure or a reference missing.
You could try making Class2 public, but it sounds like this can't be a problem here, since by default what you did is internal class Class2 and thus Class2 should be accessible if both are living in the same project/assembly. If you are referencing a different assembly or project wherein Class2 is contained, you have to make it public in order to access it, as internal classes can't be accessed from outside their assembly.
As for renaming: You can click Program.cs in the Solution Explorer and press F2 to rename it. It will then open up a dialog window asking you if the class Program itself and all references thereof should be renamed as well, which is usually what you want. Or you could just rename the class Program in the declaration and again open up the menu with the small blue bar (or, again, CTRL+.) and do the same, but it won't automatically rename the actual file accordingly.
Edit after your question edit: I have never used this option you used, but from quick checking I think that it's really not inside the same project then. Do the following when adding new classes to a project: In the Solution Explorer, right click the project you created and select [Add] -> [Class] or [Add] -> [New Item...] and then select 'Class'. This will automatically make the new class part of the project and thus the assembly (the assembly is basically the 'end product' after building the project). For me, there is also the shortcut Alt+Shift+C working to create a new class.
namespace TestCSharp2
{
**public** class Class2
{
int i;
public void setValue(int i)
{
this.i = i;
}
public int getValue()
{
return this.i;
}
}
}
Add the 'Public' declaration before 'class Class2'.
According to your example here it seems that they both reside in the same namespace. I conclude that they are both part of the same project (if you haven't created another project with the same namespace)
and all classes by default are defined as internal to the project they are defined in, if haven't declared otherwise, therefore I guess the problem is that your file is not included in your project.
You can include it by right clicking the file in the solution explorer window => Include in project, if you cannot see the file inside the project files in the solution explorer then click the show the upper menu button of the solution explorer called show all files (just hover your mouse cursor over the button there and you'll see the names of the buttons).
Just for basic knowledge:
If the file resides in a different project\ assembly then it has to be defined,
otherwise it has to be defined at least as internal or public.
In case your class is inheriting from that class that it can be protected as well.
I was having the same problem here. Found out that the problem was with an Advanced Property of the file. There is there an option with the name 'Compilation Action' (may be not with the exact words, I am translating - my VS is in Portuguese).
My Class1.cs file was there as "Content" and I just had to change it to "Compile" to make it work, and have the classes recognized by the others files in the same project.
Just make two projects in two different files then rename the "Program.cs" of one of the two files
and copy it then paste it next to the Program.cs of the other file and that's it.
In your project there will be a file with .csproj extension.
Double click on it to open the project in the Visual Studio. Otherwise, if you make a new class, it won't link with other classes.
When u diclare your , var
you , can use private , declarasion
using System;
private Class class;
I'm trying to code a console shell using Roslyn.Scripting.CSharp. Of course user is able to dynamically add references to the Session using methods of host object:
public void AddRef(string nameOrPath)
{
session.AddReference(nameOrPath);
}
public void AddRef(System.Reflection.Assembly asm)
{
session.AddReference(asm);
}
But I also want him to be able to list already loaded assemblies. How to do that?
Not working / bad solutions:
AppDomain.CurrentDomain.GetAssemblies() - yields all assemblies loaded to the AppDomain which are not necessarily avaible in Roslyn's scripting session
Using ScriptEngine's property GetReferences() - as I've understood, it returns session-wide list of references, and my AddRef method doesn't change it
Collecting own list of added references. But what if reference was added by display name? How to convert string nameOrPath to Assembly type? Using Roslyn.Compilers.MetadataReference does not help, because it hasn't got the reference to Assembly.
I am writing a custom scaffolder for our project. And this scaffolder should add links to DTO declarations for client side app.
I have a possibility to retrieve an instance of project item,
$folder = Get-ProjectFolder "Views\Shared"
and I already found that it is possible to add links using ProjectNode.AddNewFileNodeToHierarchy(string, string) method.
I can get a reference to the DTE service by simply accessing $DTE variable predefined in PowerConsole.
The question is how to get instance of ProjectNode I am interested in?
Ok, it was easier than I thought.
Here is a snippet I finished with:
$targetFolder = Get-ProjectFolder "Services" -Project "ServiceModel.Silverlight"
$sourceFile = Get-ProjectItem $ServiceInterfaceOutputPath -Project "ServiceModel"
$sourceFile.Open()
$targetFolder.AddFromFile($($sourceFile.Document.FullName))
In case you are not working with t4 scaffolder, this link can be helpfull : http://social.msdn.microsoft.com/Forums/en/vsx/thread/168d23c8-eee8-4486-a412-147b67673593
I have two projects : Menu and Module and they are both in the same namespace foobar.
I am currently referencing the module project from the Menu project to open up certain controls on a tab control in my menu. However I need to launch a new control from one of my controls which is located in the Module project.
When I try referencing the menu project, it does not show up in my intellisense when I try to reference it with a using. Am I doing something wrong logically here?
Here is an example of what it is :
Project Menu
Public Void LaunchWPFControl(string pHeader,string pPath)
{
//Code goes here to launch a WPF control in the browser
}
Project Module
//What I would love to do but doesn't work
Using Menu;
...
...
...
private void dgModule_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Menu.LaunchWPFControl("Accounts","AccountsControl");
}
If you are talking about seperate projects then what you are trying to do here is a circular reference, this is not allowed. If Project Menu references Project Module, then Project Module cannot reference Project Menu.
If you need a class in Project Module to trigger something in the Menu project you need to look for an alternative way of doing it. One possible technique for achieving this is to create an event in the class in the Module project that the Menu project can subscribe to and perform the required action.
For example in Project Module:
private void dgModule_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
OnMyEvent();
}
private void OnMyEvent()
{
EventHandler localEvent = MyEvent;
if(localEvent != null)
{
localEvent(this, EventArgs.Empty);
}
}
Then in Project Menu you can subscribe to this event and perform your action:
...
...
...
moduleClass.MyEvent += SomeHandler;
...
...
...
private void SomeHandler(Object sender, EventArgs e)
{
Menu.LaunchWPFControl("Accounts","AccountsControl");
}
As Ray Burns mentions (see comments) another way would be to define an interface of the Menu class in some shared location (either there referenced project, or some other shared project) and than you can pass implementations of that interface to the Module project.
Which way is better often depends on the abstraction you are trying to achieve with each project.
If they're both in namespace foobar then you need
using foobar;
instead of using Menu.
It's important that you understand the terminology involved - you're talking about "a different project in the same assembly" - that's nonsensical. Visual Studio creates one assembly per project. You then talk about the projects having the same namespace in the text of your question. You need to understand the difference.
To work out the namespace of a type, open the class containing the type and look for namespace declarations:
namespace MyProject
{
...
}
To work out the assembly of a type, look in the project properties for the project in which it's declared - or if you're using the same solution, just add a reference from the project which wants to use the type to the project which declares the type.
Note that you specify a namespace with a using directive; you need to add a reference to an assembly in solution explorer. They're not doing the same thing. A using directive just says, "Within the code affected by this using directive, I want to be able to use types within this namespace without fully qualifying the names."
Next you've got code like this:
Menu.LaunchWPFControl("Accounts","AccountsControl");
I thought Menu was either a project name or a namespace - but now you're trying to use it as a type name. Which is it?
If this doesn't sort you out, please post full code and a more coherent description of the projects and namespaces involved. Take a step back, work out the types, namespaces and assemblies involved, and then lay it all out clearly.
So Project Menu references Project Module. Then you want Project Module to reference Project Menu so a module can directly call Menu functionality?
This isn't possible, this is a circular reference. If you try to add the Menu reference to the Module project, Visual Studio won't let you.
You need to pull the stuff out of Menu that both Menu and Module want to use into a third project, and have them both reference it. Or combine Menu and Module into one project.