I want to "simulate" the Right click/Update service reference command in a VS2010 addin. I have a reference to the containing (Silverlight...) project, I know the name of the service reference and the url of the service.
I've found this: http://dedjo.blogspot.com/2007/03/adding-web-references-to-your-vs.html , but it only works for asmx (it uses System.Web.Services instead of System.ServiceModel), not wcf.
Is there any choice but call svcutil from code? if so, how? (do I use svcutil or slsvcutil? How do I call it from inside the addin?)
thanks
I believe the visual studio command for this is "Project.UpdateServiceReference". So I guess you can try to select the node you're interested in, and run this command, like this:
envDTE.Windows.Item(vsWindowKindSolutionExplorer).Activate();
envDTE.ActiveWindow.Object.GetItem(#"MyProject\Service References\Proxy").Select(vsUISelectionType.vsUISelectionTypeSelect);
envDTE.ExecuteCommand("Project.UpdateServiceReference");
If you're looking for the more programmatic way to do this, you can do something like the following. This approach does not require using the DTE automation layer which will change the user's selection and execute a command. Note that this is within the context of a VSPackage with an IServiceProvider so that it can get instances to the core Visual Studio interfaces, etc...
You may also be able to do this from within an Addin, but you'd need to get an IServiceProvider and add references to (at least) Microsoft.VisualStudio.Shell.Interop.dll and Microsoft.VisualStudio.WCFReference.Interop. Reference assemblies for these binaries are available in the Visual Studio 2010 SDK.
IVsSolution solution = GetService(typeof(SVsSolution)) as IVsSolution;
if (solution != null)
{
IVsHierarchy solutionHierarchy = solution as IVsHierarchy;
if (null != solutionHierarchy)
{
IEnumHierarchies enumHierarchies;
Guid nullGuid = Guid.Empty;
ErrorHandler.ThrowOnFailure(solution.GetProjectEnum((uint)__VSENUMPROJFLAGS.EPF_ALLINSOLUTION, ref nullGuid, out enumHierarchies));
if (enumHierarchies != null)
{
uint fetched;
IVsHierarchy[] hierarchies = new IVsHierarchy[1];
IVsWCFReferenceManagerFactory wcfReferenceManagerFactory = GetService(typeof(SVsWCFReferenceManagerFactory)) as IVsWCFReferenceManagerFactory;
if (wcfReferenceManagerFactory != null)
{
while (enumHierarchies.Next(1, hierarchies, out fetched) == 0 && fetched == 1)
{
if (wcfReferenceManagerFactory.IsReferenceManagerSupported(hierarchies[0]) == 1)
{
IVsWCFReferenceManager referenceManager = wcfReferenceManagerFactory.GetReferenceManager(hierarchies[0]);
var referenceGroupCollection = referenceManager.GetReferenceGroupCollection();
referenceGroupCollection.UpdateAll(null);
}
}
}
}
}
}
I'd also recommend looking at the WCF Service Consumption Tools samples for the Visual Studio 2010 SDK.
Related
I am in the progress of rewriting some code to work with ASP 5.
The old code does the following:
string Local_IP=Request.ServerVariables["LOCAL_ADDR"];
string HTTP_reverse_VIA = Request.ServerVariables["HTTP_REVERSE_VIA"];
How do I get the corresponding information from ASP 5?
HttpContext has the GetFeature method, using this method we can get feature information.
Here we are want to get Server Variables of IIS; check project.json "Microsoft.AspNet.Server.IIS" is used for running ASP.NET 5.
We have to use GetFeature of 'Microsoft.AspNet.Server.IIS' which contains Server variables feature. use the below code
var varibleFeature = Context.GetFeature<Microsoft.AspNet.Server.IIS.Features.IServerVariablesFeature>();
if (varibleFeature != null)
{
var valuesList = varibleFeature.ServerVariables;
//read through valuesList dictionary for Server Variables
}
Since I was running on IIS Express, it gave few variables but not the one mentioned in your question.
Please deploy it on IIS and explore more.
In my test Winforms app (in which I'm targeting .NET 3.5, to simulate the Windows CE / Compact Framework 3.5 app that this is a first-line test for as much as possible), I added some JSON.NET code to deserialize json returned from WebAPI methods:
try
{
const string uri = "http://localhost:48614/api/departments";
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
{
var reader = new StreamReader(webResponse.GetResponseStream());
string s = reader.ReadToEnd();
MessageBox.Show(string.Format("Content from HttpWebRequest is {0}", s));
var arr = JsonConvert.DeserializeObject<JArray>(s);
int i = 1;
foreach (JObject obj in arr)
{
var id = (string)obj["Id"];
var accountId = (double)obj["AccountId"];
var departmentName = (string)obj["DeptName"];
MessageBox.Show(string.Format("Object {0} in JSON array: id == {1}, accountId == {2}, deptName == {3}", i, id, accountId, departmentName));
i++;
}
}
else
{
MessageBox.Show(string.Format("Status code == {0}", webResponse.StatusCode));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
...This runs fine in the .NET 3.5 Winforms app, but when I copied it over to the Windows CE-targetted app, the code wouldn't run, with the following errors spilling forth:
The type 'System.ComponentModel.IBindingList' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
The type 'System.ComponentModel.ITypedList' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=2.0.0.0...
The type 'System.ComponentModel.INotifyPropertyChanging' is defined in an assembly that is not referenced....
The type 'System.ComponentModel.ICustomTypeDescriptor' is defined in an assembly...
The type 'System.ComponentModel.INotifyPropertyChanged' ...
The type 'System.Uri'...
I saw that in the Winforms (testbed) app, I'm using version 2.0.0.0 of the "regular" (or "deluxe" when compared to CF) System.dll. In the Windows CE app, though, I was using the CF flavor of version 3.5 found here:
C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\System.dll
I tried using version 2 CF from C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.dll, but that failed, too - so it's apparently not really the version (3.5 vs. 2.0), but the "flavor" (CF vs "deluxe"/regular System.dll).
SO...I replaced the CF-flavored System.dll[s] with the one successfully used by the Winforms test app, explicitly the one in C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll (I have no System.dll in C:\Windows\Microsoft.NET\Framework\v3.5, anyway).
It no longer gives those same err msgs as listed above, but there is another compile error that may (or may not be) related (Can I give an emulator more disk space?) now.
Whether it is or not (related), it brings up the intriguing question: Will using a regular System.dll in a Windows CE project cause a problem?
If it will -- or there's a good chance that it will -- cause a problem, since it was apparently the JSON.NET code that required the change to an "off-colored" version of System.dll, is there a CF-ready / CF-specific version of JSON.NET? Will I have to create my own CF-targeted version of an assembly from the JSON.NET source?
UPDATE
In the JSON.NET readme, it states:
For a Compact Framework 3.5 build download Json.NET 3.5.
Which I assumed meant the .DLL in \Json50r7\Bin\Net35
Am I wrong about that?
UPDATE 2
When I attempt to open Newtonsoft.Json.Net35.sln in Windows 2008, with the intention of creating a CE-targeted assembly, it doesn't allow me, saying, "The selected file is a solution file, but was created by a newer version of this appllication and cannot be opened*"
It also says in the JSON.NET read me:
Microsoft stopped support for the Compact Framework in Visual Studio 2010.
...so I don't think I can open it in a newer version of VS2008 and create a CF-friendly DLL, either...
UPDATE 3
Looking for a "Compact" folder in the download from http://json.codeplex.com/releases/view/113546, but I see no such folder:
It's not the "Portable" folder, is it?
As Robert Harvey suggests, the tile and the actual question here don't match. You probably should fix that.
The answer to the current title "Can I use a regular System.dll in a Compact Framework Project?" is absolutely, definitively no. You cannot mix and match. Full-framework assemblies cannot run under the Compact Framework. There's no way to make them work. Period. Stop trying this.
The answer to "How do I use JSON.NET is a Compact Framework Project" is that you should go to the JSON.NET project site on GitHub and specifically look at the last JSON.NET 3.5 release (it was Release 8) and download it. Inside that zip file is a folder named "Compact" that contains an assembly named Newtonsoft.Json.Compact.dll. Add a reference to that DLL to your Compact Framework 3.5 project.
I've been doing some work recently that analyzes relationships between various projects in source control. So far I've been using PowerShell and XPath through the Select-Xml cmdlet to process our csproj files, however this relies on my tenuous knowledge of how MSBuild uses the ProjectReference and Reference elements in the project files. It dawned on me that it would be much better if I could use MSBuild itself to resolve the references and then somehow inspect the results of the reference resolution process.
MSBuild experts: does this seem possible? Would this entail writing a custom targets file or something? Would I be forced into also building the projects as well since csproj files also import Microsoft.CSharp.targets?
Any insight would be nice. Thanks!
It is really quite easy. First reference these assemblies:
Microsoft.Build
Microsoft.Build.Engine
Microsoft.Build.Framework
Microsoft.Build.Utilities.v4.0
...and you can create some tooling around the MSBuild object model. I've got a custom MSBuild task that does this analysis right in the build, snippet below:
private bool CheckReferences(string projectFullPath)
{
var project = new Project(projectFullPath);
var items = project.GetItems("Reference");
if (items == null)
return true;
foreach (var item in items)
{
if (item == null)
continue;
if (string.IsNullOrWhiteSpace(item.UnevaluatedInclude))
continue;
if (!item.HasMetadata("HintPath"))
continue;
string include = item.UnevaluatedInclude;
string hintPath = item.GetMetadata("HintPath").UnevaluatedValue;
if (!string.IsNullOrWhiteSpace(hintPath))
if (hintPath.Contains(#"C:\") || hintPath.Contains("C:/"))
LogWarning("Absolute path Reference in project {0}", projectFullPath);
}
return true;
}
I got a code to create custom SharePoint list using Visual Studio 2010. But where can I place this code in Visual Studio 2010 was not mentioned anywhere. Can somebody help me please? I am really struggling. Here is the code:
using (SPSite oSPsite = new SPSite("http://Web URL"))
{
oSPsite.AllowUnsafeUpdates = true;
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
/* 1. create list from custom ListTemplate present within ListTemplateGalery */
SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
SPListTemplate template = lstTemp["custom template name"];
oSPWeb.Lists.Add("List Name", "Description", template);
/* 2. create list from sharepoint list content type (e.g. Links) */
oSPWeb.Lists.Add("List Name", "Description", SPListTemplateType.Links);
oSPWeb.AllowUnsafeUpdates = false;
}
oSPsite.AllowUnsafeUpdates = false;
}
You could put this in a client application (Console, WinForms, WPF). The only restriction would be that the app will only work if it is executed on the SharePoint server. It will not work remotely.
Another way would be to create a SharePoint Feature and include the code in the Feature Receiver. Chapter 3 of Inside Microsoft SharePoint 2010 describes the process of building a Feature and attaching a Feature Receiver.
http://msdn.microsoft.com/en-us/library/ff872401.aspx
This code can be executed from all the places where you can execute code in sharepoint depending on the requirement , here are some
A webpart i.e. the code behind for the webpart
It can be a part of a sharepoint page that run with code behind (http://blogs.msdn.com/b/kaevans/archive/2010/06/28/creating-a-sharepoint-site-page-with-code-behind-using-visual-studio-2010.aspx)
A SharePoint Timer Job http://blogs.msdn.com/b/guruketepalli/archive/2013/02/12/10259696.aspx
It can be a part of a list event handler/receiver or a web event receiver ( http://msdn.microsoft.com/en-us/library/ff407274(v=office.14).aspx )
Run from some client side application
In my project properties I go to publish, options, and file associations and enter ".cms", "Contact manager File" "pqcms" and "1icon.ico", but when I publish and install it does not appear to associate the files...I want to be able to double click on the file and have it open the program but it does not appear to do so.
I believe there are ways to edit the registry if you run your program as an administrator, but I really need clickonce to be happy with me because I am maximizing the features. Isn't clickonce supposed to set up the file association for me? Why isn't it?
and final question: what can I do without elevating privileges to administrator?
Have you added the code required to handle the user double-clicking on the file?
//Get the ActivationArguments from the SetupInformation property of the domain.
string[] activationData =
AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;
if (activationData != null)
{
Uri uri = new Uri(activationData[0]);
string fileNamePassedIn = uri.LocalPath.ToString();
//now you have the file name and you can handle it
}
One other thing to beware of. I originally converted this code (provided by RobinDotNet) to vb.net. Now I've converted the project to c# and ran into something interesting. When debugging (and I'd imagine if you chose to have the exe accessible as opposed to the click once reference app) "AppDomain.CurrentDomain.SetupInformation.ActivationArguments" is null (no activation arguments were assigned) so I modified the code slightly to trap this error.
//Get the ActivationArguments from the SetupInformation property of the domain if any are set.
if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments != null)
{
string[] activationData =
AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;
if (activationData != null)
{
Uri uri = new Uri(activationData[0]);
string fileNamePassedIn = uri.LocalPath.ToString();
//now you have the file name and you can handle it
}
}